import { FastMCP } from 'fastmcp'; import { z } from 'zod'; import { createMCPToolResult } from './ToolHelpers.js'; /** * Configuration options for creating an MCP server */ export interface ServerConfig { /** Server name */ name: string; /** Server version */ version: `${number}.${number}.${number}`; /** Server instructions/description */ instructions?: string; } /** * Interface for agents that can register tools with the MCP server */ export interface Agent { register(server: FastMCP): Promise; } /** * Options for creating an MCP server with tools and agents */ export interface CreateServerOptions extends ServerConfig { /** Tools to register with the server */ tools?: unknown[]; /** Agents to register with the server */ agents?: Agent[]; /** Async initialization function called after server creation */ onInitialize?: (server: FastMCP) => Promise; } /** * Create a FastMCP server instance with the specified configuration. * This factory provides a reusable pattern for creating MCP servers * with tools and agents. * * @param options Server configuration options * @returns Configured FastMCP server instance * * @example * ```typescript * const server = await createMCPServer({ * name: 'my-server', * version: '1.0.0', * instructions: 'My MCP server', * tools: [myTool], * agents: [myAgent], * onInitialize: async (server) => { * // Custom initialization logic * } * }); * ``` */ export async function createMCPServer( options: CreateServerOptions ): Promise { const { name, version, instructions, tools = [], agents = [], onInitialize } = options; // Create FastMCP server const server = new FastMCP({ name, version, instructions: instructions || `${name} MCP Server v${version}` }); // Register tools for (const tool of tools) { server.addTool(tool as any); } // Register agents for (const agent of agents) { await agent.register(server); } // Run custom initialization if (onInitialize) { await onInitialize(server); } return server; } /** * Helper to create a basic health check tool */ export function createHealthCheckTool( version: string, additionalInfo?: Record ): unknown { return { name: 'health-check', description: 'Check server health and status', parameters: z.object({}), // Empty Zod schema instead of plain object execute: async () => { const payload = { status: 'healthy', timestamp: new Date().toISOString(), version, ...additionalInfo }; return createMCPToolResult(payload); } }; }