Skip to content

Introduced utility methods to check if a tool, resource, or prompt is registered #568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions src/server/mcp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,7 @@ describe("tool()", () => {
],
}));

const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();

await Promise.all([
client.connect(clientTransport),
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
});
});
27 changes: 27 additions & 0 deletions src/server/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down