createMcpServer
Create a raw MCP server from a ts-kizuna API for use with any transport (stdio, SSE, etc.).
Create a raw MCP server from a ts-kizuna API. Use this when you want full control over the transport, for example stdio for CLI tools or SSE for custom setups.
To serve it from your app, install mcpPlugin instead.
pnpm add @ts-kizuna/mcpbun add @ts-kizuna/mcpnpm install @ts-kizuna/mcpimport { createMcpServer } from '@ts-kizuna/mcp/server';Parameters
createMcpServer(api: Api, options?: McpServerOptions): McpServer| Parameter | Type | Required | Description |
|---|---|---|---|
api | Api | Yes | API created with server.api |
options | McpServerOptions | No | Configuration options |
Options
| Option | Default | Description |
|---|---|---|
name | 'MCP Server' | Human-readable name shown to AI assistants |
version | '1.0.0' | Semantic version string |
routeFilter | excludes multipart | Predicate to filter which routes become tools |
handlerContext | none | Extra context spread into every handler call |
credentialHeaders | none | Headers of the MCP transport request, used to run guards on secured tools |
Secured routes
Each call to a secured tool runs the same guard pipeline as an HTTP request: the identity's credential is extracted from credentialHeaders, the guard resolves it to context (passed to the handler under the identity's name), access gates are checked, and a deny(...) becomes a tool error result.
The mcpPlugin endpoint populates credentialHeaders automatically from the incoming request. When you drive the transport yourself, pass the headers along. For stdio there is no HTTP request, so supply whatever credential headers the environment provides:
const server = createMcpServer(api, {
name: 'My API',
credentialHeaders: {
authorization: `Bearer ${process.env.API_TOKEN}`,
},
});Returns
An McpServer instance from @modelcontextprotocol/sdk. Connect it to any transport.
Example
import { createMcpServer } from '@ts-kizuna/mcp/server';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { api } from './api';
const server = createMcpServer(api, {
name: 'My API',
version: '1.0.0',
});
await server.connect(new StdioServerTransport());See the MCP guide for connecting clients and tool input/output structure.