Skip to content

Fix registering multiple tools/resources/prompts #126

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

Merged
merged 2 commits into from
Jan 21, 2025
Merged
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@modelcontextprotocol/sdk",
"version": "1.3.0",
"version": "1.3.1",
"description": "Model Context Protocol implementation for TypeScript",
"license": "MIT",
"author": "Anthropic, PBC (https://anthropic.com)",
Expand Down
73 changes: 73 additions & 0 deletions src/server/mcp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,19 @@ describe("tool()", () => {
}).toThrow(/already registered/);
});

test("should allow registering multiple tools", () => {
const mcpServer = new McpServer({
name: "test server",
version: "1.0",
});

// This should succeed
mcpServer.tool("tool1", () => ({ content: [] }));

// This should also succeed and not throw about request handlers
mcpServer.tool("tool2", () => ({ content: [] }));
});

test("should allow client to call server tools", async () => {
const mcpServer = new McpServer({
name: "test server",
Expand Down Expand Up @@ -734,6 +747,33 @@ describe("resource()", () => {
}).toThrow(/already registered/);
});

test("should allow registering multiple resources", () => {
const mcpServer = new McpServer({
name: "test server",
version: "1.0",
});

// This should succeed
mcpServer.resource("resource1", "test://resource1", async () => ({
contents: [
{
uri: "test://resource1",
text: "Test content 1",
},
],
}));

// This should also succeed and not throw about request handlers
mcpServer.resource("resource2", "test://resource2", async () => ({
contents: [
{
uri: "test://resource2",
text: "Test content 2",
},
],
}));
});

test("should prevent duplicate resource template registration", () => {
const mcpServer = new McpServer({
name: "test server",
Expand Down Expand Up @@ -1210,6 +1250,39 @@ describe("prompt()", () => {
}).toThrow(/already registered/);
});

test("should allow registering multiple prompts", () => {
const mcpServer = new McpServer({
name: "test server",
version: "1.0",
});

// This should succeed
mcpServer.prompt("prompt1", async () => ({
messages: [
{
role: "assistant",
content: {
type: "text",
text: "Test response 1",
},
},
],
}));

// This should also succeed and not throw about request handlers
mcpServer.prompt("prompt2", async () => ({
messages: [
{
role: "assistant",
content: {
type: "text",
text: "Test response 2",
},
},
],
}));
});

test("should throw McpError for invalid prompt name", async () => {
const mcpServer = new McpServer({
name: "test server",
Expand Down
24 changes: 24 additions & 0 deletions src/server/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,13 @@ export class McpServer {
await this.server.close();
}

private _toolHandlersInitialized = false;

private setToolRequestHandlers() {
if (this._toolHandlersInitialized) {
return;
}

this.server.assertCanSetRequestHandler(
ListToolsRequestSchema.shape.method.value,
);
Expand Down Expand Up @@ -165,6 +171,8 @@ export class McpServer {
}
},
);

this._toolHandlersInitialized = true;
}

private setCompletionRequestHandler() {
Expand Down Expand Up @@ -249,7 +257,13 @@ export class McpServer {
return createCompletionResult(suggestions);
}

private _resourceHandlersInitialized = false;

private setResourceRequestHandlers() {
if (this._resourceHandlersInitialized) {
return;
}

this.server.assertCanSetRequestHandler(
ListResourcesRequestSchema.shape.method.value,
);
Expand Down Expand Up @@ -342,9 +356,17 @@ export class McpServer {
);

this.setCompletionRequestHandler();

this._resourceHandlersInitialized = true;
}

private _promptHandlersInitialized = false;

private setPromptRequestHandlers() {
if (this._promptHandlersInitialized) {
return;
}

this.server.assertCanSetRequestHandler(
ListPromptsRequestSchema.shape.method.value,
);
Expand Down Expand Up @@ -406,6 +428,8 @@ export class McpServer {
);

this.setCompletionRequestHandler();

this._promptHandlersInitialized = true;
}

/**
Expand Down
Loading