·6 min read

Your whole API as three MCP tools

Spunto's MCP server used to have 20 hand-written tools. Now it has three generic ones that browse the OpenAPI spec and replay requests through the API's own router, covering 202 operations in about 2.5 KB of tool definitions.

Mathieu

Mathieu

Founder, Spunto

Your whole API as three MCP tools

Until October 28, Spunto's MCP server had 20 tools, each written by hand: create_worker, run_command, get_worker_logs, and so on. That day we removed a whole feature from the product, and 14 of those tools went with it. The REST API had more than 200 operations; the MCP server covered a tenth of them, and the coverage only ever went down.

So we replaced them with three:

ToolWhat it does
find_api_operationssearches the API's own OpenAPI spec and returns METHOD /path — operationId — summary lines
describe_api_operationgives the parameters, request body and responses of one operation, with the schemas it references
call_apimakes the request as the caller and returns HTTP <status> and the body

An agent connected to spunto.net/mcp can now do anything the API can do, including routes written after this post, without anyone touching the MCP server again.

The obvious alternative is to generate one MCP tool per operation from the spec. I measured it against our own API: 202 operations, each with its parameters and request body inlined (an MCP tool can't $ref into a shared components section).

ToolsDefinitions sent in tools/list
One tool per operation202108 KB
The three generic tools32.5 KB

108 KB of JSON is roughly 30,000 tokens, sitting in the client's context before the agent has asked anything. Some clients also cap how many tools they load. And the list would still drift: every new route means a new tool, a new description, a new place to forget.

This is the actual output from a local Spunto stack. First, the agent asks what exists:

→ find_api_operations {}
 
202 operations. Sections (pass one as `tag`, or search with `query`):
- ApiKeys (3)
- Billing (11)
- Projects (12)
- Sandboxes (30)
- Secrets (12)
- Tasks (24)
…

Then it searches:

→ find_api_operations { "query": "create sandbox" }
 
POST /api/orgs/{orgId}/sandboxes/prebuilds — createSandboxPrebuild — Build a project for a provider
POST /api/orgs/{orgId}/sandboxes — createSandbox — Create a sandbox
POST /api/orgs/{orgId}/sandboxes/{sandboxId}/snapshots — snapshotSandbox — Snapshot a running sandbox
…

describe_api_operation { "operationId": "createSandbox" } returns the description written on the route ("Asks the chosen runtime app to boot a box… status starting until the in-sandbox agent says hello"), the path parameters, and the body schema with its field-level docs. The agent then calls the API and gets the answer the API gives anyone:

→ call_api { "method": "POST",
             "path": "/api/orgs/Rfzkn…/sandboxes",
             "body": { "size": "small" } }
 
HTTP 400
{ "error": "appId is required (unless fromSnapshot)" }

That 400 is the important design choice in call_api: an error status is an answer, not a crash. The tool returns it with isError: true, and the agent reads it the way a developer reads a terminal. Missing scope, not found, conflict, missing field: the API's own error message says what to fix, so the next call is usually right.

The tempting implementation of call_api is to call the service layer directly. It's also the dangerous one. Everything app.ts mounts in front of a route (org membership, API-key scopes, the project↔org guard) would have to be redone inside each tool, and that duplicated copy is exactly where an authorization hole shows up the day someone adds a scope.

So the tool doesn't call services. It builds an HTTP request, replays the caller's own credentials on it, and sends it through the API's Hono router in memory:

// apps/api/src/modules/mcp/api-dispatch.ts (simplified)
export async function apiRequest(ctx: McpContext, method: string, path: string, body?: unknown) {
  const res = await app.request(path, {          // Hono, in-process: no socket
    method,
    headers: {
      ...ctx.authHeaders,                         // the caller's own Bearer / cookie, verbatim
      ...(body !== undefined ? { "Content-Type": "application/json" } : {}),
    },
    ...(body !== undefined ? { body: JSON.stringify(body) } : {}),
  })
  return { status: res.status, contentType: res.headers.get("content-type") ?? "", text: await res.text() }
}

The request goes through the same middleware chain as any REST request: authMiddleware → requireOrgAccess → requireResourceScope → the usual handler. An MCP client can't reach anything its token couldn't already reach over REST, and a new scope rule applies to MCP the day it's written. Pointed at another organization, the same token gets the same answer it would get over curl:

→ call_api { "method": "GET", "path": "/api/orgs/h5WLO…/projects" }
 
HTTP 403
{ "error": "Forbidden" }

One thing the in-memory door must not do is be wider than the public one. Our reverse proxy never routes a few internal prefixes from outside (/api/internal, the tunnel, routing callbacks), but app.request() doesn't go through the proxy. So call_api mirrors that list and refuses those paths itself:

→ call_api { "method": "GET", "path": "/api/internal/whatever" }
 
This path is internal to the platform: not part of the public API.

Nothing comes free, and here are the trade-offs:

  • Round trips. An agent that knows nothing makes three calls before acting (find, describe, call) where a dedicated tool needs one. In practice it describes an operation once per session and then calls it many times.
  • The spec has to be good. The agent's only documentation is what the routes say about themselves. A vague summary or a missing field description now costs every agent that uses it. We already generated the spec from the routes (describeRoute in Hono); this made us write the descriptions properly.
  • Not everything is a request. WebSocket upgrades (a live terminal, streaming logs) can't be held by one tool call, so they're filtered out of the search. Running a command in a sandbox goes through exec, which is a normal request.
  • A few hand-written tools are worth keeping. We kept four, for the first reads every session makes: list_organizations, search (a name to an id in one call), list_projects and get_project. They save the agent a lookup at the moment it knows the least.

The pattern fits any API that already has an OpenAPI spec and a router you can call in-process (Hono's app.request, Express with supertest-style injection, Fastify's inject):

  1. Load the spec your server already generates. Don't write a second one for MCP.
  2. Expose search and describe over it, read-only, and keep the output to short lines plus only the schemas an operation references.
  3. Expose one call tool that replays the caller's credentials through your real middleware, refuses the paths your proxy doesn't route, truncates huge bodies, and returns errors as answers.

The whole thing is about 190 lines on our side. If you want to try it against Spunto, the MCP reference has the one-line setup for Claude Code, by OAuth or with an API key. You can also connect in read-only mode, which limits the token to GET requests.

Mathieu

Mathieu

Founder, Spunto

Building Spunto — self-hosted dev environments for engineers who want their compute to work for them, not against them.

Your whole API as three MCP tools — Spunto