| summary | Architecture overview for the mcporter runtime, CLI, generated-CLI toolkit, and typed-client layer. |
|---|
Inspired in part by Anthropic’s guidance on MCP code execution agents: https://www.anthropic.com/engineering/code-execution-with-mcp
mcporter is a TypeScript runtime and CLI for calling Model Context Protocol servers from terminals, scripts, generated CLIs, and agents. For day-to-day command usage, see CLI reference; for install and first-run flows, see Quickstart.
createRuntime()hosts shared connections, lists tools, calls tools, and resolves resources.callOnce()keeps one-shot scripts simple when connection reuse is unnecessary.createServerProxy()maps tool names to method-style calls, fills JSON-schema defaults, validates required arguments, and returnsCallResulthelpers for.text(),.markdown(),.json(),.images(), and.content().- The CLI (
npx mcporter list|call|auth|resource|serve|emit-ts|generate-cli) uses the same runtime as the library API.
- Load MCP definitions from JSON (support relative paths + HTTPS).
- Reuse
@modelcontextprotocol/sdktransports; invoke stdio servers directly (e.g., callnpxwith env overrides) without an extra wrapper script. - Automatically detect OAuth requirements for ad-hoc HTTP servers by retrying failed handshakes and promoting the definition to
auth: "oauth"when a 401/403 is encountered, then launching the browser flow immediately. - Mirror Python helper interpolation behavior for
${VAR},${VAR:-default}, and$env:VAR. - Support OAuth token cache directory handling through
~/.mcporter, XDG paths, and per-server overrides. - Fetch tool signatures and schemas for
list, generated CLIs, and typed clients. - Provide lazy connection pooling per server to minimize startup cost.
- Document Cursor-compatible
config/mcporter.jsonstructure; support env-sourced headers and stdio commands while keeping inline overrides available for scripts.
- Cache tool schemas on first access, persist them under
~/.mcporter/<server>/schema.jsonor$XDG_CACHE_HOME/mcporter/<server>/schema.jsonfor reuse across processes, and tolerate failures by falling back to rawcallTool. - Allow direct method-style invocations such as
context7.getLibraryDocs("react")by:- Mapping camelCase properties to kebab-case tool names.
- Detecting positional arguments and assigning them to required schema fields in order.
- Handling multi-argument tools (e.g., Firecrawl’s
scrape/map) via positional arrays, plain objects, or mixed option bags. - Merging JSON-schema defaults and validating required fields before dispatch.
- Return
CallResultobjects exposing.raw,.text(),.markdown(),.json()helpers for consistent post-processing. - Keep implementation generic—no hardcoded knowledge of specific servers—so new MCP servers automatically gain the ergonomic API.
- Encourage lightweight composition helpers in examples (e.g., resolving then fetching Context7 docs) while keeping library exports generic.
- Back the proxy with targeted unit tests that cover primitive-only calls, positional tuples + option bags, and error fallbacks when schemas are missing.
generate-clishould accept inline JSON, file paths, inline stdio commands (either via--commandor as the first positional argument), or existing config names and produce a ready-to-run CLI that maps tools to Commander subcommands.- Embed schemas (via
listTools { includeSchema: true }) directly in the generated source so repeat executions avoid additional metadata calls. - Support optional bundling through Rolldown by default, or Bun's native bundler when targeting Bun, with executable shebangs on bundled artifacts.
- Surface flags for output path, runtime target (
nodeorbun), bundle destination, and per-call timeout (default 30s). - See CLI generator for current flags, bundling behavior, artifact metadata, and regeneration flows.
- Single file
config/mcporter.jsonmirrors Cursor/Claude schema:mcpServersmap with entries containingbaseUrlorcommand+args, optionalheaders,env,description,auth,tokenCacheDir, and conveniencebearerToken/bearerTokenEnvfields. - Optional
importsarray (defaulting to ['cursor', 'claude-code', 'claude-desktop', 'codex', 'windsurf', 'opencode', 'vscode']) controls auto-merging of editor configs; entries earlier in the list win conflicts while local definitions can still override. - Provide
configPathoverride for scripts/tests; keep inline overrides in examples for completeness but default to file-based configuration. - Add fixtures validating HTTP vs. stdio normalization, header/env behavior, and editor config imports (Cursor, Claude Code/Desktop, Codex, Windsurf, OpenCode, VS Code) to ensure priority ordering matches defaults.