Skip to content

Commit a01872d

Browse files
Update MCP Server API documentation for SDK v1.22.0
This updates all MCP server examples to use the new registerTool API from @modelcontextprotocol/sdk v1.22.0, which introduces a breaking change to the tool registration syntax. Changes: - Replace server.tool() with server.registerTool() - Update tool registration to use object-based configuration with description and inputSchema properties - Update examples across all MCP-related documentation pages The new API improves clarity by explicitly separating the tool description and input schema into a configuration object. Synced from cloudflare/agents PR #659 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 1f88efe commit a01872d

File tree

5 files changed

+91
-47
lines changed

5 files changed

+91
-47
lines changed

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

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,15 @@ 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+
},
190+
async () => ({
191+
content: [{ type: "text", text: `Hello, ${this.props.claims.name || "user"}!` }],
192+
})
193+
);
188194
}
189195
}
190196
```
@@ -222,15 +228,23 @@ function requirePermission(permission, handler) {
222228
// Use the wrapper with your MCP tools
223229
async init() {
224230
// Basic tools available to all authenticated users
225-
this.server.tool("basicTool", "Available to all users", {}, async () => {
226-
// Implementation for all users
227-
});
231+
this.server.registerTool(
232+
"basicTool",
233+
{
234+
description: "Available to all users"
235+
},
236+
async () => {
237+
// Implementation for all users
238+
}
239+
);
228240

229241
// Protected tool using the permission wrapper
230-
this.server.tool(
242+
this.server.registerTool(
231243
"adminAction",
232-
"Administrative action requiring special permission",
233-
{ /* parameters */ },
244+
{
245+
description: "Administrative action requiring special permission"
246+
// inputSchema can include parameters if needed
247+
},
234248
requirePermission("admin", async (req) => {
235249
// Only executes if user has "admin" permission
236250
return {
@@ -241,9 +255,15 @@ async init() {
241255

242256
// Conditionally register tools based on user permissions
243257
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-
});
258+
this.server.registerTool(
259+
"specialTool",
260+
{
261+
description: "Special feature"
262+
},
263+
async () => {
264+
// This tool only appears for users with the special_feature permission
265+
}
266+
);
247267
}
248268
}
249269
```

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: "Adds 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: 39 additions & 25 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,27 @@ 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+
},
447+
async () => {
448+
// Access user info automatically populated by OAuth provider
449+
const auth = getMcpAuthContext();
450+
const username = auth?.props?.username as string | undefined;
451+
const email = auth?.props?.email as string | undefined;
452+
453+
return {
454+
content: [
455+
{
456+
type: "text",
457+
text: `User: ${username ?? "anonymous"}, Email: ${email ?? "none"}`,
458+
},
459+
],
460+
};
461+
}
462+
);
455463
```
456464

457465
</TypeScriptExample>
@@ -467,14 +475,20 @@ The `createMcpHandler` automatically catches errors and returns JSON-RPC error r
467475
<TypeScriptExample>
468476

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

479493
// Errors are automatically caught and returned as:
480494
// {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ import { z } from "zod";
2222
export class MyMCP extends McpAgent {
2323
server = new McpServer({ name: "Demo", version: "1.0.0" });
2424
async init() {
25-
this.server.tool(
25+
this.server.registerTool(
2626
"add",
27-
{ a: z.number(), b: z.number() },
27+
{
28+
description: "Adds 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
}),

src/content/docs/agents/x402.mdx

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

127127
// Free tool
128-
this.server.tool(
128+
this.server.registerTool(
129129
"echo",
130-
"Echo a message",
131-
{ message: z.string() },
130+
{
131+
description: "Echo a message",
132+
inputSchema: { message: z.string() }
133+
},
132134
async ({ message }) => {
133135
return { content: [{ type: "text", text: message }] };
134136
},

0 commit comments

Comments
 (0)