server.ts
Language TypeScript1.3 KB32 lines
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";import { tools } from "./tools/index.js";import type { ToolContext, ToolResult } from "./tools/types.js";import pkg from "../package.json" with { type: "json" };export const SERVER_INFO = { name: "meridian-orders-mcp", version: pkg.version } as const;/*** Register the read-only tool set onto a server, binding each handler to the* given context. Transport-agnostic: called by both the Node (stdio) entry and* the Cloudflare Workers (HTTP) entry, so tool behavior stays identical across* runtimes.*/export function registerTools(server: McpServer, ctx: ToolContext): void {for (const tool of tools) {server.registerTool(tool.name, tool.config, async (args: Record<string, unknown>): Promise<ToolResult> => {try {return await tool.handler(args, ctx);} catch (err) {const message = err instanceof Error ? err.message : String(err);return { content: [{ type: "text", text: message }], isError: true };}});}}/** Build a fully-registered McpServer for a context. Used by the stdio entry. */export function createServer(ctx: ToolContext): McpServer {const server = new McpServer(SERVER_INFO);registerTools(server, ctx);return server;}