@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
159 lines (158 loc) • 5.84 kB
JavaScript
import { getClient } from "../../currentScopes.js";
import { hasSpanStreamingEnabled } from "../../tracing/spans/hasSpanStreamingEnabled.js";
import { MCP_SERVER_SPAN_NAME_FALLBACK } from "../../tracing/spans/spanNames.js";
import { startSpan } from "../../tracing/trace.js";
import { buildTransportAttributes } from "./sessionExtraction.js";
import { buildTypeSpecificAttributes } from "./attributeExtraction.js";
import { MCP_FUNCTION_ORIGIN_VALUE, MCP_NOTIFICATION_ORIGIN_VALUE, MCP_ROUTE_SOURCE_VALUE } from "./attributes.js";
import { extractTargetInfo } from "./methodConfig.js";
import { filterMcpPiiFromSpanData } from "./piiFiltering.js";
import { MCP_METHOD_NAME, SENTRY_OP, SENTRY_ORIGIN, SENTRY_SEGMENT_NAME_SOURCE } from "@sentry/conventions/attributes";
import { MCP_NOTIFICATION_CLIENT_TO_SERVER, MCP_NOTIFICATION_SERVER_TO_CLIENT, MCP_SERVER } from "@sentry/conventions/op";
// src/integrations/mcp-server/spans.ts
/**
* Span creation and management functions for MCP server instrumentation
*
* Provides unified span creation following OpenTelemetry MCP semantic conventions and our opinitionated take on MCP.
* Handles both request and notification spans with attribute extraction.
*/
/**
* Creates a span name based on the method and target
* @internal
* @param method - MCP method name
* @param target - Optional target identifier
* @returns Formatted span name
*/
function createSpanName(method, target) {
return target ? `${method} ${target}` : method;
}
/**
* Build Sentry-specific attributes based on span type
* @internal
* @param type - Span type configuration
* @returns Sentry-specific attributes
*/
function buildSentryAttributes(type) {
let op;
let origin;
switch (type) {
case "request":
op = MCP_SERVER;
origin = MCP_FUNCTION_ORIGIN_VALUE;
break;
case "notification-incoming":
op = MCP_NOTIFICATION_CLIENT_TO_SERVER;
origin = MCP_NOTIFICATION_ORIGIN_VALUE;
break;
case "notification-outgoing":
op = MCP_NOTIFICATION_SERVER_TO_CLIENT;
origin = MCP_NOTIFICATION_ORIGIN_VALUE;
}
return {
[SENTRY_OP]: op,
[SENTRY_ORIGIN]: origin,
[SENTRY_SEGMENT_NAME_SOURCE]: MCP_ROUTE_SOURCE_VALUE
};
}
/**
* Unified builder for creating MCP spans
* @internal
* @param config - Span configuration
* @returns Created span
*/
function createMcpSpan(config) {
const { type, message, transport, extra, callback, options, links } = config;
const { method } = message;
const params = message.params;
const client = getClient();
const spanStreamingEnabled = !!client && hasSpanStreamingEnabled(client);
let spanName;
if (type === "request") {
const targetInfo = extractTargetInfo(method, params || {});
const target = spanStreamingEnabled && !targetInfo.targetIsLowCardinality ? void 0 : targetInfo.target;
spanName = method ? createSpanName(method, target) : MCP_SERVER_SPAN_NAME_FALLBACK;
} else spanName = method || (spanStreamingEnabled ? "MCP notification" : method);
const rawAttributes = {
...buildTransportAttributes(transport, extra, message),
[MCP_METHOD_NAME]: method,
...buildTypeSpecificAttributes(type, message, params, options?.recordInputs),
...buildSentryAttributes(type)
};
const userInfo = Boolean(client?.getDataCollectionOptions().userInfo);
const attributes = filterMcpPiiFromSpanData(rawAttributes, userInfo);
return startSpan({
name: spanName,
forceTransaction: true,
attributes,
links
}, callback);
}
/**
* Creates a span for incoming MCP notifications
* @param jsonRpcMessage - Notification message
* @param transport - MCP transport instance
* @param extra - Extra handler data
* @param options - Resolved MCP options
* @param callback - Span execution callback
* @param links - Links to add to the span
* @returns Span execution result
*/
function createMcpNotificationSpan(jsonRpcMessage, transport, extra, options, callback, links) {
return createMcpSpan({
type: "notification-incoming",
message: jsonRpcMessage,
transport,
extra,
callback,
options,
links
});
}
/**
* Creates a span for outgoing MCP notifications
* @param jsonRpcMessage - Notification message
* @param transport - MCP transport instance
* @param options - Resolved MCP options
* @param callback - Span execution callback
* @returns Span execution result
*/
function createMcpOutgoingNotificationSpan(jsonRpcMessage, transport, options, callback) {
return createMcpSpan({
type: "notification-outgoing",
message: jsonRpcMessage,
transport,
options,
callback
});
}
/**
* Builds span configuration for MCP server requests
* @param jsonRpcMessage - Request message
* @param transport - MCP transport instance
* @param extra - Optional extra handler data
* @param options - Resolved MCP options
* @returns Span configuration object
*/
function buildMcpServerSpanConfig(jsonRpcMessage, transport, extra, options) {
const { method } = jsonRpcMessage;
const params = jsonRpcMessage.params;
const client = getClient();
const spanStreamingEnabled = !!client && hasSpanStreamingEnabled(client);
const targetInfo = extractTargetInfo(method, params || {});
const target = spanStreamingEnabled && !targetInfo.targetIsLowCardinality ? void 0 : targetInfo.target;
const spanName = method ? createSpanName(method, target) : MCP_SERVER_SPAN_NAME_FALLBACK;
const rawAttributes = {
...buildTransportAttributes(transport, extra, jsonRpcMessage),
[MCP_METHOD_NAME]: method,
...buildTypeSpecificAttributes("request", jsonRpcMessage, params, options?.recordInputs),
...buildSentryAttributes("request")
};
const userInfo = Boolean(client?.getDataCollectionOptions().userInfo);
return {
name: spanName,
forceTransaction: true,
attributes: filterMcpPiiFromSpanData(rawAttributes, userInfo)
};
}
export { buildMcpServerSpanConfig, createMcpNotificationSpan, createMcpOutgoingNotificationSpan };
//# sourceMappingURL=spans.js.map