@modelfetch/core
Version:
Core utilities for MCP servers built with ModelFetch
58 lines • 1.63 kB
JavaScript
/**
* This module provides the foundational components for creating MCP servers that can be deployed
* across various JavaScript/TypeScript runtimes.
*
* @example Basic Example
* ```ts
* import { createApp } from "@modelfetch/core";
* import server from "./server";
*
* const app = createApp(server);
* ```
*
* @example Advanced Example
* ```ts
* import { createApp } from "@modelfetch/core";
* import server from "./server";
*
* const app = createApp({
* server,
* base: "/api",
* path: "/mcp",
* middleware: [authMiddleware, loggingMiddleware],
* pre: (app) => {
* app.get("/health", (c) => c.text("OK"));
* },
* post: (app) => {
* app.notFound((c) => c.text("Not found", 404));
* }
* });
* ```
*
* @module
*/
import { StreamableHTTPTransport } from "@hono/mcp";
import { Hono } from "hono";
/**
* Creates an MCP server application from an MCP server implementation or configuration.
*/
export function createApp(arg) {
const config = "connect" in arg ? { server: arg } : arg;
const app = new Hono();
const router = config.base ? app.basePath(config.base) : app;
if (config.pre)
config.pre(router);
const path = (config.path ?? "") || "/mcp";
if (config.middleware)
for (const fn of config.middleware)
router.use(path, fn);
router.all(path, async (c) => {
const transport = new StreamableHTTPTransport();
await config.server.connect(transport);
return transport.handleRequest(c);
});
if (config.post)
config.post(router);
return app;
}
//# sourceMappingURL=index.js.map