mcp-spawn-client
v0.0.0
Published
Spawn Model Context Protocol clients connected to servers using in-memory transports
Maintainers
Readme
MCP Spawn Client
A utility module for spawning Model Context Protocol (MCP) clients connected to servers using in-memory transports.
Installation
npm install mcp-client-routerUsage
import { spawnClient } from 'mcp-client-router/spawn-client';
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
// Create a server
const server = new McpServer({
name: 'test-server',
version: '1.0.0',
});
// Define tools, prompts, and resources
server.tool('greet', { name: 'string' }, async ({ name }) => {
return { content: [{ type: 'text', text: `Hello, ${name}!` }] };
});
// Spawn a client connected to the server
const client = await spawnClient(server, {
name: 'test-client',
version: '1.0.0'
});
// Use the client
const result = await client.callTool({
name: 'greet',
arguments: { name: 'World' }
});
console.log(result); // { content: [{ type: 'text', text: 'Hello, World!' }] }API Reference
spawnClient
async function spawnClient(server, ...clientArgs)Parameters
server(McpServer): The MCP server to connect to...clientArgs: Arguments to pass to the Client constructor
Returns
- Promise: A connected client instance
How It Works
The spawnClient function:
- Creates a pair of linked in-memory transports
- Connects one transport to the server
- Creates a new client with the provided args
- Connects the client to the other transport
- Returns the connected client
This allows for easy testing and development without needing to set up network transports.
License
ISC
