@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
137 lines (136 loc) • 5.38 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 { startInactiveSpan, 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
};
}
/**
* Builds the name and attributes of an MCP notification span
* @internal
*/
function buildMcpNotificationSpanConfig(type, message, transport, options, extra) {
const { method } = message;
const params = message.params;
const client = getClient();
const spanStreamingEnabled = !!client && hasSpanStreamingEnabled(client);
const name = 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);
return {
name,
attributes: filterMcpPiiFromSpanData(rawAttributes, userInfo)
};
}
function notificationSpanOptions({ name, attributes, links }) {
return {
name,
forceTransaction: true,
attributes,
links
};
}
/** Starts an MCP notification span around `callback`. */
function startMcpNotificationSpan(config, callback) {
return startSpan(notificationSpanOptions(config), callback);
}
/** Starts an MCP notification span that the caller ends. */
function startInactiveMcpNotificationSpan(config) {
return startInactiveSpan(notificationSpanOptions(config));
}
/**
* 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 startMcpNotificationSpan(buildMcpNotificationSpanConfig("notification-outgoing", 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 { buildMcpNotificationSpanConfig, buildMcpServerSpanConfig, createMcpOutgoingNotificationSpan, startInactiveMcpNotificationSpan, startMcpNotificationSpan };
//# sourceMappingURL=spans.js.map