diff --git a/src/server/mcp.test.ts b/src/server/mcp.test.ts index 49f852d6..0f715137 100644 --- a/src/server/mcp.test.ts +++ b/src/server/mcp.test.ts @@ -484,8 +484,7 @@ describe("tool()", () => { ], })); - const [clientTransport, serverTransport] = - InMemoryTransport.createLinkedPair(); + const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); await Promise.all([ client.connect(clientTransport), @@ -2937,7 +2936,7 @@ describe("prompt()", () => { result = await client.request( { method: "prompts/list" }, ListPromptsResultSchema, - ); + ); expect(result.prompts).toHaveLength(1); expect(result.prompts[0].name).toBe("prompt2"); @@ -3500,3 +3499,36 @@ describe("prompt()", () => { expect(result.messages[0].content.text).toContain("Received request ID:"); }); }); + +describe("registration checks", () => { + /** + * Test: hasTool, hasResource, hasPrompt + */ + test("should check tool/resource/prompt registration", () => { + const mcpServer = new McpServer({ + name: "test server", + version: "1.0", + }); + + // Register tool, resource, prompt + mcpServer.tool("tool1", async () => ({ content: [] })); + mcpServer.resource("resource1", "test://resource1", async () => ({ contents: [] })); + // For prompt, return a valid prompt result (with messages) + mcpServer.prompt("prompt1", async () => ({ + messages: [ + { + role: "assistant", + content: { type: "text", text: "dummy" }, + }, + ], + })); + + // Check registration + expect(mcpServer.hasTool("tool1")).toBe(true); + expect(mcpServer.hasTool("not-exist")).toBe(false); + expect(mcpServer.hasResource("test://resource1")).toBe(true); + expect(mcpServer.hasResource("not-exist://uri")).toBe(false); + expect(mcpServer.hasPrompt("prompt1")).toBe(true); + expect(mcpServer.hasPrompt("not-exist")).toBe(false); + }); +}); diff --git a/src/server/mcp.ts b/src/server/mcp.ts index 5b864b8b..50341040 100644 --- a/src/server/mcp.ts +++ b/src/server/mcp.ts @@ -880,6 +880,33 @@ export class McpServer { return registeredPrompt } + /** + * Checks if a tool with the given name is registered. + * @param name Tool name + * @returns True if the tool is registered + */ + hasTool(name: string): boolean { + return !!this._registeredTools[name]; + } + + /** + * Checks if a resource with the given URI is registered. + * @param uri Resource URI + * @returns True if the resource is registered + */ + hasResource(uri: string): boolean { + return !!this._registeredResources[uri]; + } + + /** + * Checks if a prompt with the given name is registered. + * @param name Prompt name + * @returns True if the prompt is registered + */ + hasPrompt(name: string): boolean { + return !!this._registeredPrompts[name]; + } + /** * Checks if the server is connected to a transport. * @returns True if the server is connected