External Clients

Learn how to connect to external MCP servers via HTTP or STDIO and generate fully typed TypeScript clients with autocomplete support.

xmcp lets you connect to external MCP servers and generate fully typed clients. The CLI generates TypeScript clients with autocomplete for all tools exposed by HTTP or STDIO-based MCP servers.

Creating the Clients File

Create a src/clients.ts file and export a ClientConnections object. The object keys become the client names:

src/clients.ts

HTTP Clients

HTTP clients connect to remote MCP servers over HTTP.

Example:

STDIO Clients

STDIO clients spawn local processes that communicate via standard input/output.

Examples:

The npm package will be installed automatically if it is not already installed in your project.

Running the Generator

Run the generator from your project root:

This reads from src/clients.ts and writes generated clients to src/generated/.

Options:

  • -o, --out <path> - Output directory (default: src/generated)
  • -c, --clients <path> - Clients file path (default: src/clients.ts)

Generated Output

For each client defined in clients.ts, the CLI generates a client.{name}.ts file containing:

  • Zod schemas for each tool's arguments
  • Type exports (e.g., GreetArgs)
  • Tool metadata objects
  • createRemoteToolClient() factory function
  • Pre-instantiated client export

An index file (client.index.ts) is always generated with a unified generatedClients object for accessing all clients.

Using Generated Clients

Import generatedClients from the generated index file and call tools directly:

src/tools/get-library-docs.ts

The generated clients provide full autocomplete for all available tools and their arguments.

Example: Browser Navigation

src/tools/browser-navigate.ts

Caveats

  • Server must be available — The CLI connects over HTTP or spawns the STDIO package to fetch tool definitions. Ensure the remote server is reachable or the npm package is installed.
  • Prefer env for secrets — API keys can be provided as CLI args or via the env map. Prefer env for sensitive values.

On this page

One framework to rule them all