Skip to content

feat(mcp): add mcp support#1867

Open
NameHaibinZhang wants to merge 16 commits intoopen-telemetry:mainfrom
NameHaibinZhang:feature/mcp
Open

feat(mcp): add mcp support#1867
NameHaibinZhang wants to merge 16 commits intoopen-telemetry:mainfrom
NameHaibinZhang:feature/mcp

Conversation

@NameHaibinZhang
Copy link
Copy Markdown
Member

@NameHaibinZhang NameHaibinZhang commented Apr 17, 2026

Summary

Add observability support for the Model Context Protocol (MCP) — the emerging standard for communication between AI agents and tool/resource servers.

MCP runs over HTTP using JSON-RPC 2.0. This change enables OBI to automatically detect, parse, and export MCP spans with rich semantic attributes following the OpenTelemetry MCP semantic conventions (semconv v1.40.0).

Key changes

  • Protocol detection (pkg/ebpf/common/http/mcp.go): Identifies MCP traffic by parsing the JSON-RPC request body and matching method names (tools/call, resources/read, prompts/get, initialize, etc.) with a jsonrpc: "2.0" version check. Falls back to the Mcp-Session-Id header for unknown extension methods. Extracts tool name, resource URI, prompt name, session ID, protocol version, request ID, and error information from both request and response bodies.

  • Span enrichment (pkg/appolly/app/request/span.go): Adds HTTPSubtypeMCP subtype, MCPCall struct, and OperationName() for GenAI operation naming (tools/callexecute_tool). Extends SpanStatusCode and SpanStatusMessage to treat MCP JSON-RPC errors (non-zero error code in HTTP 200 responses) as span errors, matching existing JSON-RPC behavior.

  • HTTP transform integration (pkg/ebpf/common/http_transform.go): Wires MCP detection into the HTTP span pipeline, gated by the MCP enabled flag. Placed before JSON-RPC detection so MCP-specific semantics take priority.

  • OTel attribute export (pkg/export/otel/tracesgen/tracesgen.go, pkg/export/attributes/names/attrs.go): Emits mcp.method.name, mcp.session.id, mcp.resource.uri, mcp.protocol.version, gen_ai.tool.name, gen_ai.prompt.name, gen_ai.operation.name, jsonrpc.request.id, rpc.response.status_code, and error.message attributes.

  • Configuration (pkg/config/payload_extraction.go, pkg/obi/config.go): Added MCPConfig with OTEL_EBPF_HTTP_MCP_ENABLED env var, disabled by default. GenAIConfig.Enabled() includes MCP.

  • Tests: Comprehensive unit tests covering all MCP method types, error responses, session header fallback, non-MCP rejection (missing jsonrpc: "2.0", unknown methods, non-POST, invalid JSON), span status/message for MCP errors, GenAIConfig.Enabled() coverage, and OTel trace generation for MCP success/error spans (server and client).

Validation

@NameHaibinZhang NameHaibinZhang requested a review from a team as a code owner April 17, 2026 10:30
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 17, 2026

Codecov Report

❌ Patch coverage is 85.24590% with 27 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.15%. Comparing base (360521f) to head (ca5a238).
⚠️ Report is 15 commits behind head on main.

Files with missing lines Patch % Lines
pkg/ebpf/common/http/mcp.go 84.34% 12 Missing and 6 partials ⚠️
pkg/export/otel/tracesgen/tracesgen.go 83.33% 4 Missing and 2 partials ⚠️
pkg/appolly/app/request/span.go 88.00% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #1867       +/-   ##
===========================================
+ Coverage   67.22%   78.15%   +10.92%     
===========================================
  Files         277      278        +1     
  Lines       33258    33790      +532     
===========================================
+ Hits        22358    26407     +4049     
+ Misses       9539     6126     -3413     
+ Partials     1361     1257      -104     
Flag Coverage Δ
integration-test 55.49% <62.29%> (+<0.01%) ⬆️
integration-test-arm 29.07% <6.01%> (-0.39%) ⬇️
integration-test-vm-x86_64-5.15.152 29.14% <6.01%> (-0.46%) ⬇️
integration-test-vm-x86_64-6.10.6 28.74% <6.01%> (-0.84%) ⬇️
k8s-integration-test 42.30% <6.01%> (-0.78%) ⬇️
oats-test 37.45% <6.01%> (-1.33%) ⬇️
unittests 58.15% <81.96%> (+6.80%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions
Copy link
Copy Markdown
Contributor

CI Supervisor: Pull request checks (attempt 1)

Job Conclusion Duration Verdict
Generate and checks failure 4m flaky
Action: Re-running failed jobs (attempt 2 of 2)

@github-actions
Copy link
Copy Markdown
Contributor

CI Supervisor: Pull request checks (attempt 2)

Job Conclusion Duration Verdict
Generate and checks failure 4m flaky
Action: NOT re-running. Reason: Maximum re-run attempts reached (attempt 2 of 2)

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 17, 2026

CI Supervisor

Workflow Job Last state Re-running? Attempt
Pull request checks Generate and checks failure No 2/2
Pull request integration tests shard-1 (11 tests) failure Yes 1/2

@NimrodAvni78
Copy link
Copy Markdown
Contributor

@NameHaibinZhang we recently added JSONRPC detection, did you consider using the same logic from there for parsing and detection?

Copy link
Copy Markdown
Contributor

@NimrodAvni78 NimrodAvni78 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for the addition!
i think the scope of this PR is too wide, mainly with the go large buffers refactor and not reusing existing logic of JsonRPC parsing
I would also appreciate adding integration tests for this

Comment thread pkg/internal/ebpf/generictracer/generictracer.go Outdated
Comment thread pkg/export/attributes/names/attrs.go Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds Model Context Protocol (MCP) observability by detecting MCP-over-HTTP (JSON-RPC 2.0), enriching spans with MCP/GenAI semantic attributes, and enabling cross-tracer payload correlation so Go uprobe HTTP spans can be enriched using kprobe-captured body data.

Changes:

  • Implement MCP HTTP payload parsing/detection and add unit tests for MCP method variants and error handling.
  • Add connection-based large-buffer lookup and use it to enrich Go tracer HTTP spans with MCP/JSON-RPC detection.
  • Export MCP/GenAI semantic attributes via OTel traces, and add config/schema/docs entries to gate MCP support.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
pkg/obi/config.go Adds MCP config defaults (disabled by default).
pkg/internal/ebpf/generictracer/generictracer.go Expands PID filtering to include Go PIDs when payload extraction is enabled; adds periodic PID filter rebuild.
pkg/export/otel/tracesgen/tracesgen.go Adds MCP span attribute emission to OTel exporter.
pkg/export/attributes/names/attrs.go Adds MCP/GenAI attribute name constants (semconv v1.40 keys where needed).
pkg/ebpf/common/tcp_large_buffer.go Adds connection-keyed buffer index + extraction helper to share buffers across tracers.
pkg/ebpf/common/tcp_large_buffer_test.go Adds tests validating connection-key buffer extraction semantics.
pkg/ebpf/common/http_transform.go Detects MCP during HTTP parsing and adds Go-span enrichment via connection-based large-buffer lookup.
pkg/ebpf/common/http_transform_test.go Adds test ensuring Go HTTP span enrichment can detect MCP via connection-based lookup.
pkg/ebpf/common/common.go Extends parse context with connBuffers and invokes Go-span enrichment in ringbuf parsing.
pkg/ebpf/common/http/mcp.go Implements MCP detection/parsing from JSON-RPC request/response bodies.
pkg/ebpf/common/http/mcp_test.go Adds comprehensive MCP unit tests.
pkg/ebpf/common/http/openai_test.go Minor lint annotation removal in shared HTTP test helper.
pkg/config/payload_extraction.go Adds MCPConfig and includes it in GenAIConfig.Enabled().
pkg/config/payload_extraction_test.go Adds tests for GenAIConfig.Enabled() including MCP.
pkg/appolly/app/request/span.go Adds MCP subtype + MCPCall model, trace naming, and GenAI operation name support.
docs/config-schema.json Updates JSON schema for MCP payload extraction config.
devdocs/features.md Documents MCP support and required env vars.
SUPPORT_MATRIX.md Updates GenAI vendor list and adds MCP support entry.

Comment thread pkg/ebpf/common/http_transform.go Outdated
Comment thread pkg/ebpf/common/http_transform.go Outdated
Comment thread pkg/internal/ebpf/generictracer/generictracer.go Outdated
Comment thread pkg/internal/ebpf/generictracer/generictracer.go Outdated
Comment thread pkg/ebpf/common/http/mcp.go
Comment thread pkg/ebpf/common/tcp_large_buffer.go Outdated
@rafaelroquetto
Copy link
Copy Markdown
Contributor

I tend to agree with @NimrodAvni78, it would be great if we could split this into smaller PRs as per contributing guidelines.

@NameHaibinZhang
Copy link
Copy Markdown
Member Author

I tend to agree with @NimrodAvni78, it would be great if we could split this into smaller PRs as per contributing guidelines.

ok

@NameHaibinZhang
Copy link
Copy Markdown
Member Author

@NameHaibinZhang we recently added JSONRPC detection, did you consider using the same logic from there for parsing and detection?

ok

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Comment thread pkg/export/otel/tracesgen/tracesgen.go
Comment thread pkg/export/otel/tracesgen/tracesgen.go
@NameHaibinZhang
Copy link
Copy Markdown
Member Author

@NimrodAvni78 @rafaelroquetto I've minimized the MCP support code and reverted the changes to gotracer. It passed all tests, please review, thks.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 3 comments.

Comment thread pkg/ebpf/common/http/mcp.go Outdated
Comment thread pkg/ebpf/common/http/mcp.go
Comment thread pkg/ebpf/common/http/mcp.go
@NimrodAvni78
Copy link
Copy Markdown
Contributor

@NameHaibinZhang can we add integration tests for this?

@NameHaibinZhang
Copy link
Copy Markdown
Member Author

@NameHaibinZhang can we add integration tests for this?

sure

@NameHaibinZhang
Copy link
Copy Markdown
Member Author

@NimrodAvni78 I have already added integration tests, please review, thks.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 18 out of 18 changed files in this pull request and generated 1 comment.

Comment thread pkg/ebpf/common/http/mcp.go
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 18 out of 18 changed files in this pull request and generated 1 comment.

Comment thread pkg/ebpf/common/http/mcp.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants