# Serve over MCP

Expose your project's tools, prompts and resources to any [MCP](/docs/mcp.md) client with `createMCPServer`. Here it serves the AI Agent template's `calculator` tool.

## Before you start

You need a project made from the AI Agent template. See [Build your first agent](/docs/start/first-agent.md).

## Serve your tools

**Code**

1. Add the server as a route. The `validate` function decides which tokens are allowed, and any other token gets `401`:

```ts title="app/api/mcp/route.ts"
import { createMCPServer } from "veryfront/mcp";

const server = createMCPServer({
  enabled: true,
  auth: { type: "bearer", validate: (token) => token === process.env.MCP_TOKEN },
});

export const POST = server.createHTTPHandler();
```

2. Let MCP clients post to it. Veryfront refuses a POST that has no CSRF cookie, which a client never has:

```ts title="veryfront.config.ts"
export default {
  security: { csrf: { excludePaths: ["/api/mcp"] } },
};
```

3. Start the project with a secret of your own as the token: `MCP_TOKEN=<a secret> npm run dev`.
4. Ask for the tool list. You should now see `calculator` in `result.tools`:

```bash
curl -X POST http://localhost:3000/api/mcp \
  -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" \
  -H "Authorization: Bearer $MCP_TOKEN" \
  -d '{ "jsonrpc": "2.0", "id": 1, "method": "tools/list" }'
```

### Try it in a sandbox

```playground
step: Create a project | npm create -y veryfront@latest team-assistant -- --template ai-agent --yes
step: Add the server as a route | cd team-assistant && mkdir -p app/api/mcp && printf '%s\n' 'import { createMCPServer } from "veryfront/mcp";' '' 'const server = createMCPServer({' '  enabled: true,' '  auth: { type: "bearer", validate: (token) => token === process.env.MCP_TOKEN },' '});' '' 'export const POST = server.createHTTPHandler();' > app/api/mcp/route.ts
step: Let MCP clients post to it | printf '%s\n' 'export default {' '  security: { csrf: { excludePaths: ["/api/mcp"] } },' '};' > veryfront.config.ts
step: Pick a secret as the token | export MCP_TOKEN=docs-playground-secret
step: Start it | (npm run dev > dev.log 2>&1 &) && for look in $(seq 90); do grep -q 'Waiting for requests' dev.log && break; sleep 2; done
check: curl -X POST http://localhost:3000/api/mcp -H "Content-Type: application/json" -H "Accept: application/json, text/event-stream" -H "Authorization: Bearer $MCP_TOKEN" -d '{ "jsonrpc": "2.0", "id": 1, "method": "tools/list" }'
expect: "name":"calculator"
```

## Dev tools for a coding agent

`veryfront dev` also serves Veryfront's dev tools (`vf_*`, such as `vf_scaffold`) at `http://localhost:3002/mcp`, for a coding agent working on the project. They never run in production.

## Troubleshooting

**If `curl` answers "Forbidden: invalid or missing CSRF token"**, the route still checks CSRF. Add step 2's `veryfront.config.ts` and start the project again.

## Next steps

* [Connect a coding agent](/docs/mcp/connect.md)
* [Add a tool](/docs/agents/tools.md)
