UNPKG

mcp-use

Version:

Opinionated MCP Framework for TypeScript (@modelcontextprotocol/sdk compatible) - Build MCP Agents, Clients and Servers with support for ChatGPT Apps, Code Mode, OAuth, Notifications, Sampling, Observability and more.

96 lines 2.89 kB
/** * MCP Proxy Middleware for Hono * * Provides a CORS proxy for browser-based MCP clients to connect to remote MCP servers * that don't support CORS or require server-side forwarding. * * @module mcp-proxy */ import type { Context, Hono } from "hono"; /** * Options for configuring the MCP proxy middleware */ export interface McpProxyOptions { /** * Route path for the proxy endpoint * @default "/mcp/proxy" * @example "/inspector/api/proxy" */ path?: string; /** * Optional authentication function to validate requests * Return true to allow the request, false to reject with 401 * * @example * ```typescript * authenticate: async (c) => { * const apiKey = c.req.header("X-API-Key"); * return apiKey === process.env.API_KEY; * } * ``` */ authenticate?: (c: Context) => Promise<boolean> | boolean; /** * Optional request validator to check if target URL is allowed * Return true to allow, false to reject with 403 * * @example * ```typescript * validateRequest: (targetUrl) => { * // Only allow specific domains * return targetUrl.startsWith("https://api.example.com"); * } * ``` */ validateRequest?: (targetUrl: string, c: Context) => Promise<boolean> | boolean; /** * Enable request logging * @default true */ enableLogging?: boolean; } /** * Mount MCP proxy middleware on a Hono app * * This middleware proxies MCP requests to target servers based on the X-Target-URL header. * It handles CORS, streaming responses (SSE), and provides optional authentication. * * The proxy: * 1. Reads the target URL from the X-Target-URL header * 2. Forwards the request to that URL with appropriate headers * 3. Streams the response back to the client * 4. Handles compression and encoding correctly * * @param app - Hono application instance * @param options - Configuration options for the proxy * * @example * ```typescript * import { Hono } from "hono"; * import { mountMcpProxy } from "mcp-use/server"; * * const app = new Hono(); * * // Basic usage * mountMcpProxy(app); * * // With authentication * mountMcpProxy(app, { * path: "/api/proxy", * authenticate: async (c) => { * const token = c.req.header("Authorization"); * return token === `Bearer ${process.env.SECRET_TOKEN}`; * }, * validateRequest: (targetUrl) => { * // Only allow specific domains * return targetUrl.startsWith("https://mcp.example.com"); * } * }); * ``` * * @remarks * WARNING: This proxy does not implement authentication by default. * For production use, provide an `authenticate` function or restrict access to localhost only. */ export declare function mountMcpProxy(app: Hono, options?: McpProxyOptions): void; //# sourceMappingURL=mcp-proxy.d.ts.map