Home Blog MCP from Cobra

Emitting an MCP server from a Cobra tree

A walk-through of the code generator that turns sub-commands into a standards-compliant MCP transport.

April 16, 2026 · 9 min read

Model Context Protocol (MCP) is the fastest way to let an agent drive a tool without screen-scraping. But hand-writing an MCP server for every CLI is a maintenance tax. sapctl takes a different route: it emits an MCP server from its own command tree.

The insight

A Cobra command tree already encodes everything an MCP tool needs: a name, a description, flags, and a run function. The gap is a transport. sapctl walks the AST at runtime and exposes each sub-command as an MCP tool over JSON-RPC 2.0 stdio.

sapctl mcp serve

That single command turns the whole CLI into an MCP server. Claude Desktop, Cursor, and Continue can all connect to it.

How it works

The emitter walks the Cobra tree, and for each leaf command it:

Because the tree is the source of truth, the MCP surface can never drift from the CLI surface. Add a command, and it appears in the MCP server on the next run.

Why this matters for SAP

SAP API Policy v4/2026 requires agents to route through approved pathways rather than orchestrating raw APIs. An MCP server emitted from a CLI that only calls Published APIs is exactly that: the agent gets a constrained, auditable tool surface, and every call still writes to the signed audit chain.

What this buys you

An MCP server is not a separate product. It is a view over the same command tree. When the CLI and the agent surface share one source of truth, there is nothing to keep in sync, and nothing for an agent to bypass.

Back to blog