Skip to content

Commit

Permalink
Merge pull request #131 from modelcontextprotocol/justin/fix-competin…
Browse files Browse the repository at this point in the history
…g-completions

Fix attempted double registration of completion handlers
  • Loading branch information
jspahrsummers authored Jan 22, 2025
2 parents 1fb33e8 + 01574e1 commit cf4c592
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
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.1",
"version": "1.3.2",
"description": "Model Context Protocol implementation for TypeScript",
"license": "MIT",
"author": "Anthropic, PBC (https://anthropic.com)",
Expand Down
63 changes: 63 additions & 0 deletions src/server/mcp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,69 @@ describe("prompt()", () => {
}));
});

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

// This should succeed
mcpServer.prompt(
"echo",
{ message: z.string() },
({ message }) => ({
messages: [{
role: "user",
content: {
type: "text",
text: `Please process this message: ${message}`
}
}]
})
);
});

test("should allow registering both resources and prompts with completion handlers", () => {
const mcpServer = new McpServer({
name: "test server",
version: "1.0",
});

// Register a resource with completion
mcpServer.resource(
"test",
new ResourceTemplate("test://resource/{category}", {
list: undefined,
complete: {
category: () => ["books", "movies", "music"],
},
}),
async () => ({
contents: [
{
uri: "test://resource/test",
text: "Test content",
},
],
}),
);

// Register a prompt with completion
mcpServer.prompt(
"echo",
{ message: completable(z.string(), () => ["hello", "world"]) },
({ message }) => ({
messages: [{
role: "user",
content: {
type: "text",
text: `Please process this message: ${message}`
}
}]
})
);
});

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

private _completionHandlerInitialized = false;

private setCompletionRequestHandler() {
if (this._completionHandlerInitialized) {
return;
}

this.server.assertCanSetRequestHandler(
CompleteRequestSchema.shape.method.value,
);
Expand All @@ -198,6 +204,8 @@ export class McpServer {
}
},
);

this._completionHandlerInitialized = true;
}

private async handlePromptCompletion(
Expand Down

0 comments on commit cf4c592

Please sign in to comment.