Skip to content
Closed
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
44 changes: 32 additions & 12 deletions src/content/docs/agents/model-context-protocol/authorization.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,15 @@ When a user authenticates to your MCP server through Cloudflare's OAuth Provider
```js
export class MyMCP extends McpAgent<Env, unknown, AuthContext> {
async init() {
this.server.tool("userInfo", "Get user information", {}, async () => ({
content: [{ type: "text", text: `Hello, ${this.props.claims.name || "user"}!` }],
}));
this.server.registerTool(
"userInfo",
{
description: "Get user information"
},
async () => ({
content: [{ type: "text", text: `Hello, ${this.props.claims.name || "user"}!` }],
})
);
}
}
```
Expand Down Expand Up @@ -222,15 +228,23 @@ function requirePermission(permission, handler) {
// Use the wrapper with your MCP tools
async init() {
// Basic tools available to all authenticated users
this.server.tool("basicTool", "Available to all users", {}, async () => {
// Implementation for all users
});
this.server.registerTool(
"basicTool",
{
description: "Available to all users"
},
async () => {
// Implementation for all users
}
);

// Protected tool using the permission wrapper
this.server.tool(
this.server.registerTool(
"adminAction",
"Administrative action requiring special permission",
{ /* parameters */ },
{
description: "Administrative action requiring special permission"
// inputSchema can include parameters if needed
},
requirePermission("admin", async (req) => {
// Only executes if user has "admin" permission
return {
Expand All @@ -241,9 +255,15 @@ async init() {

// Conditionally register tools based on user permissions
if (this.props.permissions?.includes("special_feature")) {
this.server.tool("specialTool", "Special feature", {}, async () => {
// This tool only appears for users with the special_feature permission
});
this.server.registerTool(
"specialTool",
{
description: "Special feature"
},
async () => {
// This tool only appears for users with the special_feature permission
}
);
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ export class MyMCP extends McpAgent {
server = new McpServer({ name: "Demo", version: "1.0.0" });

async init() {
this.server.tool(
this.server.registerTool(
"add",
{ a: z.number(), b: z.number() },
{
description: "Adds two numbers together",
inputSchema: { a: z.number(), b: z.number() }
},
async ({ a, b }) => ({
content: [{ type: "text", text: String(a + b) }],
}),
Expand Down Expand Up @@ -106,10 +109,12 @@ export class MyMCP extends McpAgent<Env, State, {}> {
};
});

this.server.tool(
this.server.registerTool(
"add",
"Add to the counter, stored in the MCP",
{ a: z.number() },
{
description: "Add to the counter, stored in the MCP",
inputSchema: { a: z.number() }
},
async ({ a }) => {
this.setState({ ...this.state, counter: this.state.counter + a });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,12 @@ const server = new McpServer({
version: "1.0.0",
});

server.tool(
server.registerTool(
"hello",
"Returns a greeting message",
{ name: z.string().optional() },
{
description: "Returns a greeting message",
inputSchema: { name: z.string().optional() }
},
async ({ name }) => {
return {
content: [
Expand Down Expand Up @@ -437,21 +439,27 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";

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

server.tool("getProfile", "Get the current user's profile", {}, async () => {
// Access user info automatically populated by OAuth provider
const auth = getMcpAuthContext();
const username = auth?.props?.username as string | undefined;
const email = auth?.props?.email as string | undefined;

return {
content: [
{
type: "text",
text: `User: ${username ?? "anonymous"}, Email: ${email ?? "none"}`,
},
],
};
});
server.registerTool(
"getProfile",
{
description: "Get the current user's profile"
},
async () => {
// Access user info automatically populated by OAuth provider
const auth = getMcpAuthContext();
const username = auth?.props?.username as string | undefined;
const email = auth?.props?.email as string | undefined;

return {
content: [
{
type: "text",
text: `User: ${username ?? "anonymous"}, Email: ${email ?? "none"}`,
},
],
};
}
);
```

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

```ts
server.tool("riskyOperation", "An operation that might fail", {}, async () => {
if (Math.random() > 0.5) {
throw new Error("Random failure occurred");
server.registerTool(
"riskyOperation",
{
description: "An operation that might fail"
},
async () => {
if (Math.random() > 0.5) {
throw new Error("Random failure occurred");
}
return {
content: [{ type: "text", text: "Success!" }],
};
}
return {
content: [{ type: "text", text: "Success!" }],
};
});
);

// Errors are automatically caught and returned as:
// {
Expand Down
7 changes: 5 additions & 2 deletions src/content/docs/agents/model-context-protocol/tools.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ import { z } from "zod";
export class MyMCP extends McpAgent {
server = new McpServer({ name: "Demo", version: "1.0.0" });
async init() {
this.server.tool(
this.server.registerTool(
"add",
{ a: z.number(), b: z.number() },
{
description: "Adds two numbers together",
inputSchema: { a: z.number(), b: z.number() }
},
async ({ a, b }) => ({
content: [{ type: "text", text: String(a + b) }],
}),
Expand Down
8 changes: 5 additions & 3 deletions src/content/docs/agents/x402.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,12 @@ export class PaidMCP extends McpAgent<Env> {
);

// Free tool
this.server.tool(
this.server.registerTool(
"echo",
"Echo a message",
{ message: z.string() },
{
description: "Echo a message",
inputSchema: { message: z.string() }
},
async ({ message }) => {
return { content: [{ type: "text", text: message }] };
},
Expand Down
Loading