@mcp-ts/sdk/server) gives you everything you need to accept MCP connections, manage sessions, and call tools on connected servers. Use createNextMcpHandler for Next.js App Router projects, createSSEHandler for Express or other Node.js frameworks, and MultiSessionClient or MCPClient when you need to drive connections from your own server-side code.
createNextMcpHandler(options)
Creates GET and POST route handlers for Next.js App Router API routes. The POST handler accepts JSON-RPC requests and returns either a direct JSON response or an SSE stream (when the client sends Accept: text/event-stream).
Options
identity query parameter. Return null to signal a missing identity, which causes the handler to respond with 400 MISSING_IDENTITY.token query parameter or the Authorization header.false to respond with 401 UNAUTHORIZED. Defaults to always returning true.30000.clientName?, clientUri?, logoUri?, policyUri?.clientDefaults.Return value
405 METHOD_NOT_ALLOWED response directing clients to use POST.application/json by default or text/event-stream when the client sends Accept: text/event-stream.MultiSessionClient
Manages all active MCP connections for a single user identity. It reads session data from your configured storage backend, connects in batches of five, and retries failed connections automatically. Use this on traditional long-running servers (where you can cache the instance between requests) or on serverless platforms (where you create a fresh instance per invocation — session data persists in storage either way).
Constructor
MultiSessionOptions
15000.2.1000.Methods
connect(): Promise<void>
connect(): Promise<void>
active: false (OAuth-pending or previously failed) are skipped. A session that fails after all retries is logged and skipped — it does not throw.getClients(): MCPClient[]
getClients(): MCPClient[]
MCPClient instances. Use this to enumerate available tools across servers or to route a tool call to the right client by serverId.disconnect(): void
disconnect(): void
MCPClient
A single MCP client connection that supports OAuth 2.1, automatic transport negotiation (streamable HTTP → SSE fallback), and session persistence via the storage backend. You rarely need to instantiate MCPClient directly — prefer MultiSessionClient for multi-server scenarios.
Constructor options (MCPOAuthClientOptions)
streamable_http first, then falls back to sse.Methods
connect(): Promise<void>
connect(): Promise<void>
state_changed events throughout.Throws UnauthorizedError when OAuth authorization is required. In that case, the session is saved with a 10-minute TTL and the auth_required event fires with an authUrl.disconnect(reason?: string): void
disconnect(reason?: string): void
clearSession() for that.isConnected(): boolean
isConnected(): boolean
true when the underlying MCP client is active.listTools(): Promise<ListToolsResult>
listTools(): Promise<ListToolsResult>
DISCOVERING → READY state transitions and fires tools_discovered.callTool(toolName, toolArgs): Promise<CallToolResult>
callTool(toolName, toolArgs): Promise<CallToolResult>
listPrompts(): Promise<ListPromptsResult>
listPrompts(): Promise<ListPromptsResult>
getPrompt(name, args?): Promise<GetPromptResult>
getPrompt(name, args?): Promise<GetPromptResult>
listResources(): Promise<ListResourcesResult>
listResources(): Promise<ListResourcesResult>
readResource(uri): Promise<ReadResourceResult>
readResource(uri): Promise<ReadResourceResult>
finishAuth(authCode): Promise<void>
finishAuth(authCode): Promise<void>
clearSession(): Promise<void>
clearSession(): Promise<void>
disconnect().getServerId(): string | undefined
getServerId(): string | undefined
getSessionId(): string
getSessionId(): string
getServerName(): string | undefined
getServerName(): string | undefined
getServerUrl(): string
getServerUrl(): string
getTransportType(): 'sse' | 'streamable_http'
getTransportType(): 'sse' | 'streamable_http'
'streamable_http'.static getMcpServerConfig(identity): Promise<Record<string, any>>
static getMcpServerConfig(identity): Promise<Record<string, any>>
createSSEHandler(options)
Creates an SSE handler for Node.js HTTP frameworks like Express. Manages SSE streaming and routes RPC requests.
Options (SSEHandlerOptions)
30000.storage
A global StorageBackend proxy that lazily initializes the appropriate storage backend based on environment variables. All methods are async, even on backends where the underlying operation is synchronous.
MCP_TS_STORAGE_TYPEenvironment variable (redis,file,sqlite,supabase,memory)- Auto-detection:
REDIS_URL→MCP_TS_STORAGE_FILE→MCP_TS_STORAGE_SQLITE_PATH→SUPABASE_URL - Falls back to in-memory storage