Skip to content

Commit 0e93d4c

Browse files
committed
feat(integrations): initial implementation of MCP integration
1 parent f99a17b commit 0e93d4c

File tree

5 files changed

+712
-0
lines changed

5 files changed

+712
-0
lines changed

sentry_sdk/consts.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,36 @@ class SPANDATA:
749749
Example: "MainThread"
750750
"""
751751

752+
MCP_TOOL_NAME = "mcp.tool.name"
753+
"""
754+
The name of the MCP tool being called.
755+
Example: "get_weather"
756+
"""
757+
758+
MCP_PROMPT_NAME = "mcp.prompt.name"
759+
"""
760+
The name of the MCP prompt being retrieved.
761+
Example: "code_review"
762+
"""
763+
764+
MCP_RESOURCE_URI = "mcp.resource.uri"
765+
"""
766+
The URI of the MCP resource being accessed.
767+
Example: "file:///path/to/resource"
768+
"""
769+
770+
MCP_METHOD_NAME = "mcp.method.name"
771+
"""
772+
The MCP protocol method name being called.
773+
Example: "tools/call", "prompts/get", "resources/read"
774+
"""
775+
776+
MCP_REQUEST_ID = "mcp.request.id"
777+
"""
778+
The unique identifier for the MCP request.
779+
Example: "req_123abc"
780+
"""
781+
752782

753783
class SPANSTATUS:
754784
"""
@@ -845,6 +875,9 @@ class OP:
845875
WEBSOCKET_SERVER = "websocket.server"
846876
SOCKET_CONNECTION = "socket.connection"
847877
SOCKET_DNS = "socket.dns"
878+
MCP_TOOL = "mcp.tool"
879+
MCP_PROMPT = "mcp.prompt"
880+
MCP_RESOURCE = "mcp.resource"
848881

849882

850883
# This type exists to trick mypy and PyCharm into thinking `init` and `Client`

sentry_sdk/integrations/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def iter_default_integrations(with_auto_enabling_integrations):
9898
"sentry_sdk.integrations.langgraph.LanggraphIntegration",
9999
"sentry_sdk.integrations.litestar.LitestarIntegration",
100100
"sentry_sdk.integrations.loguru.LoguruIntegration",
101+
"sentry_sdk.integrations.mcp.MCPIntegration",
101102
"sentry_sdk.integrations.openai.OpenAIIntegration",
102103
"sentry_sdk.integrations.pymongo.PyMongoIntegration",
103104
"sentry_sdk.integrations.pyramid.PyramidIntegration",
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Sentry integration for MCP (Model Context Protocol) servers.
3+
4+
This integration instruments MCP servers to create spans for tool, prompt,
5+
and resource handler execution, and captures errors that occur during execution.
6+
7+
Supports both the low-level `mcp.server.lowlevel.Server` and high-level
8+
`mcp.server.fastmcp.FastMCP` APIs.
9+
"""
10+
11+
from sentry_sdk.integrations import Integration, DidNotEnable
12+
13+
try:
14+
import mcp.server.lowlevel # noqa: F401
15+
import mcp.server.fastmcp # noqa: F401
16+
except ImportError:
17+
raise DidNotEnable("MCP SDK not installed")
18+
19+
20+
class MCPIntegration(Integration):
21+
identifier = "mcp"
22+
origin = "auto.ai.mcp"
23+
24+
@staticmethod
25+
def setup_once():
26+
# type: () -> None
27+
"""
28+
Patches MCP server classes to instrument handler execution.
29+
"""
30+
from sentry_sdk.integrations.mcp.lowlevel import patch_lowlevel_server
31+
from sentry_sdk.integrations.mcp.fastmcp import patch_fastmcp_server
32+
33+
patch_lowlevel_server()
34+
patch_fastmcp_server()
35+
36+
37+
__all__ = ["MCPIntegration"]

0 commit comments

Comments
 (0)