mattrguerra/meridian-orders-mcpmainPrivate on GitHub, published here in full

server.ts

Language TypeScript1.3 KB32 lines

mcp-server/src/server.ts

  1. import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
  2. import { tools } from "./tools/index.js";
  3. import type { ToolContext, ToolResult } from "./tools/types.js";
  4. import pkg from "../package.json" with { type: "json" };
  5. export const SERVER_INFO = { name: "meridian-orders-mcp", version: pkg.version } as const;
  6. /**
  7. * Register the read-only tool set onto a server, binding each handler to the
  8. * given context. Transport-agnostic: called by both the Node (stdio) entry and
  9. * the Cloudflare Workers (HTTP) entry, so tool behavior stays identical across
  10. * runtimes.
  11. */
  12. export function registerTools(server: McpServer, ctx: ToolContext): void {
  13. for (const tool of tools) {
  14. server.registerTool(tool.name, tool.config, async (args: Record<string, unknown>): Promise<ToolResult> => {
  15. try {
  16. return await tool.handler(args, ctx);
  17. } catch (err) {
  18. const message = err instanceof Error ? err.message : String(err);
  19. return { content: [{ type: "text", text: message }], isError: true };
  20. }
  21. });
  22. }
  23. }
  24. /** Build a fully-registered McpServer for a context. Used by the stdio entry. */
  25. export function createServer(ctx: ToolContext): McpServer {
  26. const server = new McpServer(SERVER_INFO);
  27. registerTools(server, ctx);
  28. return server;
  29. }