Skip to content

Commit f63f957

Browse files
add example of dynamic routing (#100)
* add example of dynamic routing from issue #28 and comment by quuu, add example usage of dynamic routing * README.md * Clarify --------- Co-authored-by: Andrew Qu <qual1337@gmail.com>
1 parent 956dc11 commit f63f957

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,56 @@ const handler = createMcpHandler(
5050
export { handler as GET, handler as POST };
5151
```
5252

53+
## Advanced Routing
54+
```typescript
55+
// app/dynamic/[p]/[transport]/route.ts
56+
57+
import { createMcpHandler } from "@vercel/mcp-adapter";
58+
import type { NextRequest } from "next/server";
59+
import { z } from "zod";
60+
61+
const handler = async (
62+
req: NextRequest,
63+
{ params }: { params: Promise<{ p: string; transport: string }> }
64+
) => {
65+
const { p, transport } = await params;
66+
67+
return createMcpHandler(
68+
(server) => {
69+
server.tool(
70+
"roll_dice",
71+
"Rolls an N-sided die",
72+
{ sides: z.number().int().min(2) },
73+
async ({ sides }) => {
74+
const value = 1 + Math.floor(Math.random() * sides);
75+
return {
76+
content: [{ type: "text", text: `🎲 You rolled a ${value}!` }],
77+
};
78+
}
79+
);
80+
},
81+
{
82+
capabilities: {
83+
tools: {
84+
roll_dice: {
85+
description: "Roll a dice",
86+
},
87+
},
88+
},
89+
},
90+
{
91+
redisUrl: process.env.REDIS_URL,
92+
basePath: `/dynamic/${p}`,
93+
verboseLogs: true,
94+
maxDuration: 60,
95+
}
96+
)(req);
97+
};
98+
export { handler as GET, handler as POST, handler as DELETE };
99+
100+
101+
```
102+
53103
## Connecting to your MCP server via stdio
54104

55105
Depending on the version of your client application, remote MCP's may need to use

0 commit comments

Comments
 (0)