diff --git a/src/index.ts b/src/index.ts index 6f8552c..05bad44 100644 --- a/src/index.ts +++ b/src/index.ts @@ -66,7 +66,8 @@ const server = new McpServer( { capabilities: { tools: {}, - resources: {} + resources: {}, + logging: {} } } ); @@ -84,6 +85,7 @@ allResources.forEach((resource) => { async function main() { // Initialize OpenTelemetry tracing if not in test mode let tracingInitialized = false; + let tracingError: Error | null = null; if (process.env.NODE_ENV !== 'test' && !process.env.VITEST) { try { await initializeTracing(); @@ -122,14 +124,25 @@ async function main() { span.end(); } } catch (error) { - // Tracing initialization failed, log it but continue without tracing - server.server.sendLoggingMessage({ - level: 'warning', - data: `Failed to initialize tracing: ${error instanceof Error ? error.message : String(error)}` - }); + // Store the error to log after connection is established + tracingError = error instanceof Error ? error : new Error(String(error)); } } + // Start receiving messages on stdin and sending messages on stdout + const transport = new StdioServerTransport(); + await server.connect(transport); + + // Now that we're connected, send all the logging messages + + // Log tracing initialization error if any + if (tracingError) { + server.server.sendLoggingMessage({ + level: 'warning', + data: `Failed to initialize tracing: ${tracingError.message}` + }); + } + // Log tracing status and configuration if (tracingInitialized) { const tracingConfig = { @@ -175,10 +188,6 @@ async function main() { level: 'debug', data: JSON.stringify(relevantEnvVars, null, 2) }); - - // Start receiving messages on stdin and sending messages on stdout - const transport = new StdioServerTransport(); - await server.connect(transport); } // Ensure cleanup interval is cleared when the process exits