Skip to content

Commit dddcbbd

Browse files
Update MCP tool registration API to use registerTool()
This sync updates the documentation to reflect the breaking API change in @modelcontextprotocol/sdk v1.22.0, which changed tool registration from: server.tool(name, description, schema, handler) to: server.registerTool(name, { description, inputSchema }, handler) Updated files: - model-context-protocol/mcp-agent-api.mdx - model-context-protocol/tools.mdx - model-context-protocol/authorization.mdx - model-context-protocol/mcp-handler-api.mdx - x402.mdx Synced from cloudflare/agents PR #659 cloudflare/agents#659 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 64dc488 commit dddcbbd

File tree

5 files changed

+98
-49
lines changed

5 files changed

+98
-49
lines changed

src/content/docs/agents/model-context-protocol/authorization.mdx

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,16 @@ When a user authenticates to your MCP server through Cloudflare's OAuth Provider
182182
```js
183183
export class MyMCP extends McpAgent<Env, unknown, AuthContext> {
184184
async init() {
185-
this.server.tool("userInfo", "Get user information", {}, async () => ({
186-
content: [{ type: "text", text: `Hello, ${this.props.claims.name || "user"}!` }],
187-
}));
185+
this.server.registerTool(
186+
"userInfo",
187+
{
188+
description: "Get user information",
189+
inputSchema: {},
190+
},
191+
async () => ({
192+
content: [{ type: "text", text: `Hello, ${this.props.claims.name || "user"}!` }],
193+
}),
194+
);
188195
}
189196
}
190197
```
@@ -222,28 +229,44 @@ function requirePermission(permission, handler) {
222229
// Use the wrapper with your MCP tools
223230
async init() {
224231
// Basic tools available to all authenticated users
225-
this.server.tool("basicTool", "Available to all users", {}, async () => {
226-
// Implementation for all users
227-
});
232+
this.server.registerTool(
233+
"basicTool",
234+
{
235+
description: "Available to all users",
236+
inputSchema: {},
237+
},
238+
async () => {
239+
// Implementation for all users
240+
},
241+
);
228242

229243
// Protected tool using the permission wrapper
230-
this.server.tool(
244+
this.server.registerTool(
231245
"adminAction",
232-
"Administrative action requiring special permission",
233-
{ /* parameters */ },
246+
{
247+
description: "Administrative action requiring special permission",
248+
inputSchema: { /* parameters */ },
249+
},
234250
requirePermission("admin", async (req) => {
235251
// Only executes if user has "admin" permission
236252
return {
237253
content: [{ type: "text", text: "Admin action completed" }]
238254
};
239-
})
255+
}),
240256
);
241257

242258
// Conditionally register tools based on user permissions
243259
if (this.props.permissions?.includes("special_feature")) {
244-
this.server.tool("specialTool", "Special feature", {}, async () => {
245-
// This tool only appears for users with the special_feature permission
246-
});
260+
this.server.registerTool(
261+
"specialTool",
262+
{
263+
description: "Special feature",
264+
inputSchema: {},
265+
},
266+
async () => {
267+
// This tool only appears for users with the special_feature permission
268+
},
269+
);
247270
}
248271
}
249272
```

src/content/docs/agents/model-context-protocol/mcp-agent-api.mdx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ export class MyMCP extends McpAgent {
2222
server = new McpServer({ name: "Demo", version: "1.0.0" });
2323

2424
async init() {
25-
this.server.tool(
25+
this.server.registerTool(
2626
"add",
27-
{ a: z.number(), b: z.number() },
27+
{
28+
description: "Add two numbers together",
29+
inputSchema: { a: z.number(), b: z.number() },
30+
},
2831
async ({ a, b }) => ({
2932
content: [{ type: "text", text: String(a + b) }],
3033
}),
@@ -106,10 +109,12 @@ export class MyMCP extends McpAgent<Env, State, {}> {
106109
};
107110
});
108111

109-
this.server.tool(
112+
this.server.registerTool(
110113
"add",
111-
"Add to the counter, stored in the MCP",
112-
{ a: z.number() },
114+
{
115+
description: "Add to the counter, stored in the MCP",
116+
inputSchema: { a: z.number() },
117+
},
113118
async ({ a }) => {
114119
this.setState({ ...this.state, counter: this.state.counter + a });
115120

src/content/docs/agents/model-context-protocol/mcp-handler-api.mdx

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,12 @@ const server = new McpServer({
128128
version: "1.0.0",
129129
});
130130

131-
server.tool(
131+
server.registerTool(
132132
"hello",
133-
"Returns a greeting message",
134-
{ name: z.string().optional() },
133+
{
134+
description: "Returns a greeting message",
135+
inputSchema: { name: z.string().optional() },
136+
},
135137
async ({ name }) => {
136138
return {
137139
content: [
@@ -437,21 +439,28 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
437439

438440
const server = new McpServer({ name: "Auth Server", version: "1.0.0" });
439441

440-
server.tool("getProfile", "Get the current user's profile", {}, async () => {
441-
// Access user info automatically populated by OAuth provider
442-
const auth = getMcpAuthContext();
443-
const username = auth?.props?.username as string | undefined;
444-
const email = auth?.props?.email as string | undefined;
445-
446-
return {
447-
content: [
448-
{
449-
type: "text",
450-
text: `User: ${username ?? "anonymous"}, Email: ${email ?? "none"}`,
451-
},
452-
],
453-
};
454-
});
442+
server.registerTool(
443+
"getProfile",
444+
{
445+
description: "Get the current user's profile",
446+
inputSchema: {},
447+
},
448+
async () => {
449+
// Access user info automatically populated by OAuth provider
450+
const auth = getMcpAuthContext();
451+
const username = auth?.props?.username as string | undefined;
452+
const email = auth?.props?.email as string | undefined;
453+
454+
return {
455+
content: [
456+
{
457+
type: "text",
458+
text: `User: ${username ?? "anonymous"}, Email: ${email ?? "none"}`,
459+
},
460+
],
461+
};
462+
},
463+
);
455464
```
456465

457466
</TypeScriptExample>
@@ -467,14 +476,21 @@ The `createMcpHandler` automatically catches errors and returns JSON-RPC error r
467476
<TypeScriptExample>
468477

469478
```ts
470-
server.tool("riskyOperation", "An operation that might fail", {}, async () => {
471-
if (Math.random() > 0.5) {
472-
throw new Error("Random failure occurred");
473-
}
474-
return {
475-
content: [{ type: "text", text: "Success!" }],
476-
};
477-
});
479+
server.registerTool(
480+
"riskyOperation",
481+
{
482+
description: "An operation that might fail",
483+
inputSchema: {},
484+
},
485+
async () => {
486+
if (Math.random() > 0.5) {
487+
throw new Error("Random failure occurred");
488+
}
489+
return {
490+
content: [{ type: "text", text: "Success!" }],
491+
};
492+
},
493+
);
478494

479495
// Errors are automatically caught and returned as:
480496
// {

src/content/docs/agents/model-context-protocol/tools.mdx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ import { McpAgent } from "agents/mcp";
2121
export class MyMCP extends McpAgent {
2222
server = new McpServer({ name: "Demo", version: "1.0.0" });
2323
async init() {
24-
this.server.tool(
24+
this.server.registerTool(
2525
"add",
26-
{ a: z.number(), b: z.number() },
26+
{
27+
description: "Add two numbers together",
28+
inputSchema: { a: z.number(), b: z.number() },
29+
},
2730
async ({ a, b }) => ({
2831
content: [{ type: "text", text: String(a + b) }],
2932
}),

src/content/docs/agents/x402.mdx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,12 @@ export class PaidMCP extends McpAgent<Env> {
124124
);
125125

126126
// Free tool
127-
this.server.tool(
127+
this.server.registerTool(
128128
"echo",
129-
"Echo a message",
130-
{ message: z.string() },
129+
{
130+
description: "Echo a message",
131+
inputSchema: { message: z.string() },
132+
},
131133
async ({ message }) => {
132134
return { content: [{ type: "text", text: message }] };
133135
},

0 commit comments

Comments
 (0)