Skip to content

feat(server): support json schema to define tool parameters #423

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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,5 @@ out

.DS_Store
dist/

.vscode
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to the PR, but I had to stop my vscode from trying to format files with Prettier and removing random trailing whitespace.
Happy to remove that change and keep it for a separate PR!

19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,25 @@ server.tool(
};
}
);

// Tool defined using JSON Schema
server.tool({
name: "count-fingers",
paramsSchema: {
type: "object",
properties: {
hands: { type: "number" }
},
additionalProperties: false
},
cb: async (args) => {
return {
content: [
{ type: "text", text: String(args.hands * 5) }
]
};
}
})
```

### Prompts
Expand Down
107 changes: 91 additions & 16 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"client": "tsx src/cli.ts client"
},
"dependencies": {
"ajv": "^8.17.1",
"content-type": "^1.0.5",
"cors": "^2.8.5",
"cross-spawn": "^7.0.3",
Expand Down
134 changes: 134 additions & 0 deletions src/server/mcp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,140 @@ describe("tool()", () => {
});
});

describe('with options argument', () => {
test('should register tool with options', async () => {
const mcpServer = new McpServer({
name: "test server",
version: "1.0",
});
const client = new Client({
name: "test client",
version: "1.0",
});

mcpServer.tool({
name: "test",
description: "A tool with everything",
paramsSchema: {
type: "object",
properties: { name: { type: "string" } },
additionalProperties: false
},
annotations: { title: "Complete Test Tool", readOnlyHint: true, openWorldHint: false },
cb: async (args) => ({
// @ts-expect-error - no type inference when using a JSON Schema object
content: [{ type: "text", text: `Hello, ${args.name}!` }]
})
});

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

await Promise.all([
client.connect(clientTransport),
mcpServer.server.connect(serverTransport),
]);

const result = await client.request(
{ method: "tools/list" },
ListToolsResultSchema,
);

expect(result.tools).toHaveLength(1);
expect(result.tools[0].name).toBe("test");
expect(result.tools[0].description).toBe("A tool with everything");
expect(result.tools[0].inputSchema).toMatchObject({
type: "object",
properties: { name: { type: "string" } }
});
expect(result.tools[0].annotations).toEqual({
title: "Complete Test Tool",
readOnlyHint: true,
openWorldHint: false
});
});

test("should validate tool args", async () => {
const mcpServer = new McpServer({
name: "test server",
version: "1.0",
});

const client = new Client(
{
name: "test client",
version: "1.0",
},
{
capabilities: {
tools: {},
},
},
);

mcpServer.tool({
name: "test",
paramsSchema: {
type: "object",
properties: {
name: { type: "string" },
value: { type: "number" }
},
additionalProperties: false
},
cb: async (args) => ({
content: [
{
type: "text",
// @ts-expect-error - no type inference when using a JSON Schema object
text: `${args.name}: ${args.value}`,
},
],
}),
});

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

await Promise.all([
client.connect(clientTransport),
mcpServer.server.connect(serverTransport),
]);

await expect(
client.request(
{
method: "tools/call",
params: {
name: "test",
arguments: {
name: "test",
value: 123,
},
},
},
CallToolResultSchema,
),
).resolves.toBeDefined();

await expect(
client.request(
{
method: "tools/call",
params: {
name: "test",
arguments: {
name: "test",
value: "not a number",
},
},
},
CallToolResultSchema,
),
).rejects.toThrow(/Invalid arguments/);
});
})

test("should validate tool args", async () => {
const mcpServer = new McpServer({
name: "test server",
Expand Down
Loading