API Reference

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/mcp
bun add @ts-kizuna/mcp
npm install @ts-kizuna/mcp
import { createMcpServer } from '@ts-kizuna/mcp/server';

Parameters

createMcpServer(api: Api, options?: McpServerOptions): McpServer
ParameterTypeRequiredDescription
apiApiYesAPI created with server.api
optionsMcpServerOptionsNoConfiguration options

Options

OptionDefaultDescription
name'MCP Server'Human-readable name shown to AI assistants
version'1.0.0'Semantic version string
routeFilterexcludes multipartPredicate to filter which routes become tools
handlerContextnoneExtra context spread into every handler call
credentialHeadersnoneHeaders 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

src/index.ts
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.

On this page