Docs/How-To Guides/Set Up MCP Server

>_ DOCS / HOW-TO GUIDES

SET UP THE
MCP SERVER.

Connect Claude Desktop, Cursor, or any MCP-compatible host to P402 routing — no SDK, no code changes to your existing agents.

How It Works

The P402 MCP server exposes six tools over the Model Context Protocol (MCP). When your AI host calls p402_chat, P402 routes the request to the cheapest capable provider, applies your session budget, and returns an OpenAI-compatible response — all transparently.

Claude DesktopP402 MCP ServerCheapest Provider
(DeepSeek · Groq · Mistral · OpenAI · Anthropic · 9 more)
1

Run the Server

The server ships as an npm package. No global install needed — npx handles it.

bash
P402_API_KEY=your_key npx -y @p402/mcp-server@latest

The server starts on stdio (default) or --transport sse --port 3100 for HTTP SSE mode.

2

Configure Claude Desktop

Open claude_desktop_config.json and add the P402 server block. Claude Desktop auto-restarts the server on each session.

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%\Claude\claude_desktop_config.json
json
{
  "mcpServers": {
    "p402": {
      "command": "npx",
      "args": ["-y", "@p402/mcp-server@latest"],
      "env": {
        "P402_API_KEY": "your_p402_api_key_here"
      }
    }
  }
}

Restart Claude Desktop

After saving the config, fully quit and reopen Claude Desktop. You should see a hammer icon (🔨) in the composer — that's the MCP tools menu.

3

Configure Cursor

In Cursor: Settings → MCP → Add Server. Use the stdio transport.

json
{
  "mcpServers": {
    "p402": {
      "command": "npx",
      "args": ["-y", "@p402/mcp-server@latest"],
      "env": { "P402_API_KEY": "your_key" }
    }
  }
}
4

Custom MCP Host (SSE)

For programmatic access, start the server in HTTP SSE mode and connect with the official MCP TypeScript SDK.

bash
# Terminal 1 — start the server
P402_API_KEY=your_key npx -y @p402/mcp-server@latest --transport sse --port 3100
typescript
// Terminal 2 — connect via SDK
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';

const client = new Client({ name: 'my-agent', version: '1.0' }, { capabilities: {} });
const transport = new SSEClientTransport(new URL('http://localhost:3100/sse'));

await client.connect(transport);

// Call p402_chat
const result = await client.callTool({
  name: 'p402_chat',
  arguments: {
    messages: [{ role: 'user', content: 'What is EIP-3009?' }],
    mode: 'cost',
    cache: true,
    session_id: 'YOUR_SESSION_ID',   // optional
  },
});

console.log(result.content[0].text);

Available Tools

Tool
Description
p402_chat
Send a cost-optimised chat completion via P402 routing.
p402_create_session
Create a budget-capped session and return its ID.
p402_get_session
Inspect a session — balance, spend, request count.
p402_list_models
List all 300+ routable models and their live prices.
p402_compare_providers
Side-by-side cost/latency/quality comparison for a prompt.
p402_health
Check P402 and provider health status.

Environment Variables

Variable
Default
Description
P402_API_KEY
(required)
Your P402 API key.
P402_DEFAULT_MODE
cost
Routing mode: cost | quality | speed | balanced.
P402_DEFAULT_CACHE
true
Enable semantic caching by default.
P402_SESSION_ID
(none)
Pin all calls to a specific session.