dd-trace
Version:
Datadog APM tracing client for JavaScript
69 lines (52 loc) • 2 kB
JavaScript
const LLMObsPlugin = require('../base')
const { formatInput, formatOutput } = require('./utils')
class McpToolCallLLMObsPlugin extends LLMObsPlugin {
static id = 'llmobs_mcp_tool_call'
static integration = 'modelcontextprotocol-sdk'
static prefix = 'tracing:orchestrion:@modelcontextprotocol/sdk:Client_callTool'
getLLMObsSpanRegisterOptions (ctx) {
const params = ctx.arguments?.[0]
const toolName = params?.name || 'unknown_tool'
return {
kind: 'tool',
name: `MCP Client Tool Call: ${toolName}`,
}
}
setLLMObsTags (ctx) {
const span = ctx.currentStore?.span
if (!span) return
const params = ctx.arguments?.[0]
const toolName = params?.name
const toolArguments = params?.arguments
const spanTags = { mcp_tool_kind: 'client' }
const serverVersion = ctx.self?.getServerVersion?.()
if (serverVersion) {
if (serverVersion.name) spanTags.mcp_server_name = serverVersion.name
if (serverVersion.version) spanTags.mcp_server_version = serverVersion.version
if (serverVersion.title) spanTags.mcp_server_title = serverVersion.title
}
this._tagger.tagSpanTags(span, spanTags)
const hasError = ctx.error || ctx.result?.isError
const input = formatInput(toolName, toolArguments)
const output = hasError ? undefined : formatOutput(ctx.result)
this._tagger.tagTextIO(span, input, output)
}
}
class McpListToolsLLMObsPlugin extends LLMObsPlugin {
static id = 'llmobs_mcp_list_tools'
static integration = 'modelcontextprotocol-sdk'
static prefix = 'tracing:orchestrion:@modelcontextprotocol/sdk:Client_listTools'
getLLMObsSpanRegisterOptions () {
return {
kind: 'task',
name: 'MCP Client List Tools',
}
}
setLLMObsTags (ctx) {
const span = ctx.currentStore?.span
if (!span || ctx.error) return
this._tagger.tagTextIO(span, null, JSON.stringify(ctx.result))
}
}
module.exports = [McpToolCallLLMObsPlugin, McpListToolsLLMObsPlugin]