Skip to content

Commit d8f6a37

Browse files
authored
mcp-api-wrapper example (#1230)
1 parent f805374 commit d8f6a37

File tree

11 files changed

+1603
-0
lines changed

11 files changed

+1603
-0
lines changed

starter/mcp-api-wrapper/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.vercel
2+
node_modules

starter/mcp-api-wrapper/LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2025 Vercel, Inc
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

starter/mcp-api-wrapper/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Run an MCP Server on Vercel
2+
3+
## Usage
4+
5+
Update `api/server.ts` with your tools, prompts, and resources following the [MCP TypeScript SDK documentation](https://github.com/modelcontextprotocol/typescript-sdk/tree/main?tab=readme-ov-file#server).
6+
7+
[There is also a Next.js version of this template](https://vercel.com/templates/next.js/model-context-protocol-mcp-with-next-js)
8+
9+
## Notes for running on Vercel
10+
11+
- Requires a Redis attached to the project under `process.env.REDIS_URL`
12+
- Make sure you have [Fluid compute](https://vercel.com/docs/functions/fluid-compute) enabled for efficient execution
13+
- After enabling Fluid compute, open `vercel.json` and adjust max duration to 800 if you using a Vercel Pro or Enterprise account
14+
- [Deploy the MCP template](https://vercel.com/templates/other/model-context-protocol-mcp-with-vercel-functions)
15+
16+
## Local dev
17+
18+
- Run `vercel dev` for local development
19+
- Alternatively, integrate the system into the server framework of your choice.
20+
21+
## Sample Client
22+
23+
`script/test-client.mjs` contains a sample client to try invocations.
24+
25+
```sh
26+
node scripts/test-client.mjs https://mcp-on-vercel.vercel.app
27+
```
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { createMcpHandler } from 'mcp-handler'
2+
import { z } from 'zod'
3+
4+
const handler = createMcpHandler((server) => {
5+
server.tool(
6+
'roll_dice',
7+
'Rolls an N-sided die',
8+
{ sides: z.number().int().min(2) },
9+
async ({ sides }) => {
10+
const value = 1 + Math.floor(Math.random() * sides)
11+
return {
12+
content: [{ type: 'text', text: `🎲 You rolled a ${value}!` }],
13+
}
14+
}
15+
)
16+
server.tool(
17+
'get_weather',
18+
'Get the current weather at a location',
19+
{
20+
latitude: z.number(),
21+
longitude: z.number(),
22+
city: z.string(),
23+
},
24+
async ({ latitude, longitude, city }) => {
25+
const response = await fetch(
26+
`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&current_weather=true`
27+
)
28+
const weatherData = await response.json()
29+
return {
30+
content: [
31+
{
32+
type: 'text',
33+
text: `The weather in ${city} is ${weatherData.current_weather.temperature}°C currently.`,
34+
},
35+
],
36+
}
37+
}
38+
)
39+
})
40+
41+
export { handler as GET, handler as POST, handler as DELETE }
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "mcp-on-vercel",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"type": "module",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"packageManager": "[email protected]",
14+
"dependencies": {
15+
"mcp-handler": "^1.0.1",
16+
"zod": "^3.24.2"
17+
},
18+
"devDependencies": {
19+
"@types/node": "^22.13.10"
20+
}
21+
}

0 commit comments

Comments
 (0)