Files
llm-observability-mcp/src/tools/opentelemetry-llm.tool.ts
Stefano 97f358245d Based on the staged changes, here are the appropriate commit messages:
1. For the documentation reorganization and tool renaming:
docs(opentelemetry): reorganize documentation and rename observability tools

- Move OpenTelemetry documentation to docs/ directory
- Rename tools from 'capture_llm_observability_opentelemetry' to 'llm_observability_otel'
- Rename PostHog tool from 'capture_llm_observability' to 'llm_observability_posthog'
- Update README to reflect new tool names and documentation structure

2. For the file deletions and additions:
chore(docs): remove old documentation files

- Delete OPENTELEMETRY.md and examples/opentelemetry-usage.md
- Add new comprehensive docs/opentelemetry.md

3. For the tool implementation changes:
refactor(tools): update tool names in implementation files

- Update tool names in opentelemetry-llm.tool.ts and posthog-llm.tool.ts
- Keep all functionality identical, only change naming
2025-07-15 17:17:01 -05:00

78 lines
2.7 KiB
TypeScript

import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { Logger } from '../utils/logger.util.js';
import { formatErrorForMcpTool } from '../utils/error.util.js';
import { OpenTelemetryController } from '../controllers/opentelemetry-llm.controller.js';
import { CallToolResult } from '@modelcontextprotocol/sdk/types.js';
import {
OpenTelemetryLlmInputSchema,
OpenTelemetryLlmInputSchemaType,
} from '../types/opentelemetry-llm.types.js';
/**
* @function captureOpenTelemetryLlmObservability
* @description MCP Tool handler to capture LLM observability events using OpenTelemetry.
* It records metrics, traces, and spans for LLM requests that can be sent to any OpenTelemetry-compatible backend.
* @param {OpenTelemetryLlmInputSchemaType} args - Arguments provided to the tool.
* @returns {Promise<CallToolResult>} Formatted response for the MCP.
* @throws {McpError} Formatted error if the controller or service layer encounters an issue.
*/
async function captureOpenTelemetryLlmObservability(
args: OpenTelemetryLlmInputSchemaType,
): Promise<CallToolResult> {
const methodLogger = Logger.forContext(
'opentelemetry.tool',
'captureOpenTelemetryLlmObservability',
);
methodLogger.debug('Capture LLM Observability with OpenTelemetry...', args);
try {
// Parse and validate arguments
const validatedArgs = OpenTelemetryLlmInputSchema.parse(args);
// Pass validated args to the controller
const result = await OpenTelemetryController.capture(validatedArgs);
methodLogger.debug('Got response from controller', result);
// Format the response for the MCP tool
return {
content: [
{
type: 'text' as const,
text: result.content,
},
],
};
} catch (error) {
methodLogger.error(
'Error tracking LLM generation with OpenTelemetry',
error,
);
return formatErrorForMcpTool(error);
}
}
/**
* @function registerTools
* @description Registers the OpenTelemetry LLM observability tool with the MCP server.
*
* @param {McpServer} server - The MCP server instance.
*/
function registerTools(server: McpServer) {
const methodLogger = Logger.forContext(
'opentelemetry.tool',
'registerTools',
);
methodLogger.debug('Registering OpenTelemetry LLM observability tools...');
server.tool(
'llm_observability_otel',
`Captures LLM usage using OpenTelemetry for observability, including requests, responses, and performance metrics. Works with any OpenTelemetry-compatible backend like Jaeger, New Relic, Grafana, etc.`,
OpenTelemetryLlmInputSchema.shape,
captureOpenTelemetryLlmObservability,
);
methodLogger.debug('Successfully registered llm_observability_otel tool.');
}
export default { registerTools };