-
Notifications
You must be signed in to change notification settings - Fork 26
[feat] Contribute mcp Instrumentor #435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
d8a0ee3
19b25ce
b2bad7c
49649e7
442a45a
10d91ac
0e6e339
6a5f9d6
db1af61
a6ba4c4
4ea1757
3f576c2
f54609e
f8e7172
1a9ce26
fb9d1c7
7b9a8f4
de0ec1d
43526f6
bd28ccc
70b0da3
5f4f773
0af52d9
0d10a82
775f0b1
141f93c
176fd90
0ea9695
e6c479e
1f4f476
8621644
c78df18
5f70570
064c71c
c97c8df
dc2c730
76c2405
323b87a
3902c60
51aaba9
9c0e57a
dc91c62
d7666ca
aab4e7d
5c1aeec
de1020e
5a3437f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,4 +111,4 @@ include = [ | |
] | ||
|
||
[tool.hatch.build.targets.wheel] | ||
packages = ["src/amazon"] | ||
packages = ["src/amazon"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# MCP Instrumentor | ||
|
||
OpenTelemetry MCP instrumentation package. | ||
|
||
## Installation | ||
|
||
```bash | ||
pip install mcpinstrumentor | ||
``` | ||
|
||
## Usage | ||
|
||
```python | ||
from mcpinstrumentor import MCPInstrumentor | ||
|
||
MCPInstrumentor().instrument() | ||
``` |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,188 @@ | ||||||||
import logging | ||||||||
from typing import Any, Collection | ||||||||
|
||||||||
from openinference.instrumentation.mcp.package import _instruments | ||||||||
from wrapt import register_post_import_hook, wrap_function_wrapper | ||||||||
|
||||||||
from opentelemetry import trace | ||||||||
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor | ||||||||
from opentelemetry.instrumentation.utils import unwrap | ||||||||
|
||||||||
|
||||||||
def setup_logger_two(): | ||||||||
logger = logging.getLogger("loggertwo") | ||||||||
logger.setLevel(logging.DEBUG) | ||||||||
handler = logging.FileHandler("loggertwo.log", mode="w") | ||||||||
handler.setLevel(logging.DEBUG) | ||||||||
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s") | ||||||||
handler.setFormatter(formatter) | ||||||||
if not logger.handlers: | ||||||||
logger.addHandler(handler) | ||||||||
return logger | ||||||||
|
||||||||
|
||||||||
logger_two = setup_logger_two() | ||||||||
|
||||||||
|
||||||||
class MCPInstrumentor(BaseInstrumentor): | ||||||||
""" | ||||||||
An instrumenter for MCP. | ||||||||
""" | ||||||||
|
||||||||
def instrumentation_dependencies(self) -> Collection[str]: | ||||||||
return _instruments | ||||||||
|
||||||||
def _instrument(self, **kwargs: Any) -> None: | ||||||||
tracer_provider = kwargs.get("tracer_provider") | ||||||||
if tracer_provider: | ||||||||
self.tracer_provider = tracer_provider | ||||||||
else: | ||||||||
self.tracer_provider = None | ||||||||
register_post_import_hook( | ||||||||
lambda _: wrap_function_wrapper( | ||||||||
"mcp.shared.session", | ||||||||
"BaseSession.send_request", | ||||||||
self._wrap_send_request, | ||||||||
), | ||||||||
"mcp.shared.session", | ||||||||
) | ||||||||
register_post_import_hook( | ||||||||
lambda _: wrap_function_wrapper( | ||||||||
"mcp.server.lowlevel.server", | ||||||||
"Server._handle_request", | ||||||||
self._wrap_handle_request, | ||||||||
), | ||||||||
"mcp.server.lowlevel.server", | ||||||||
) | ||||||||
|
||||||||
def _uninstrument(self, **kwargs: Any) -> None: | ||||||||
unwrap("mcp.shared.session", "BaseSession.send_request") | ||||||||
unwrap("mcp.server.lowlevel.server", "Server._handle_request") | ||||||||
|
||||||||
def handle_attributes(self, span, request, is_client=True): | ||||||||
import mcp.types as types | ||||||||
|
||||||||
operation = "Server Handle Request" | ||||||||
|
operation = "Server Handle Request" | |
operation = "Server Handle Request" | |
span_name = "unknown" |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have duplicate code where we check isinstance(request, [type])
in get_name
. In my opinion we should delete get_name
and consolidate the logic into this function.
if isinstance(request, types.ListToolsRequest): | |
if isinstance(request, types.ListToolsRequest): | |
span_name = "tools/list" |
There was a problem hiding this 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 suggestion! I understand your point but I feel like we should keep it because each function handles distinct logic specific to client or server. To me, that's easier to read and if things go wrong, it is also easier to test fixes like I can just add an attribute to the client function to see what changes on CloudWatch.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will this error if we directly check for request.params.name?
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The attribute names we are setting should be defined here. Import to use the variable name instead of the explicit string.
If we are defining our own attribute like "tool.name", we should also create and use a variable for it. (PS should this be gen_ai.tool.name?)
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above. If "server_side" is defined by you then you should create a variable, otherwise import the predefined one.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we rename trace_context
to traceparent
and inject a W3C trace header string instead of the dictionary?
When the trace context gets received we should parse the string for the trace id and parent span id.
See: https://www.w3.org/TR/trace-context/#trace-context-http-headers-format
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, thank you!
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove comment
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: probably don't need to talk about how it preserves the function's behavior. More importantly, what does the wrapper introduce? (adds attributes, etc.) same for the server wrapper
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider moving the above logic into a separate function since its duplicated in the server. You could pass in either mcp.client or mcp.server.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should delete this function, see above comment
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove comment
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be helpful if you can add a comment of what the request object looks like.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, thanks for the suggestion!
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
[build-system] | ||
requires = ["hatchling"] | ||
build-backend = "hatchling.build" | ||
|
||
[project] | ||
name = "amazon-opentelemetry-distro-mcpinstrumentor" | ||
version = "0.1.0" | ||
description = "OpenTelemetry MCP instrumentation for AWS Distro" | ||
readme = "README.md" | ||
license = "Apache-2.0" | ||
requires-python = ">=3.9" | ||
authors = [ | ||
{ name = "Johnny Lin", email = "[email protected]" }, | ||
] | ||
classifiers = [ | ||
"Development Status :: 4 - Beta", | ||
"Intended Audience :: Developers", | ||
"License :: OSI Approved :: Apache Software License", | ||
"Programming Language :: Python", | ||
"Programming Language :: Python :: 3", | ||
|
||
"Programming Language :: Python :: 3.9", | ||
"Programming Language :: Python :: 3.10", | ||
"Programming Language :: Python :: 3.11", | ||
"Programming Language :: Python :: 3.12", | ||
"Programming Language :: Python :: 3.13", | ||
] | ||
dependencies = [ | ||
"openinference-instrumentation-mcp", | ||
"opentelemetry-api", | ||
"opentelemetry-instrumentation", | ||
"opentelemetry-sdk", | ||
|
||
"wrapt" | ||
] | ||
|
||
[project.optional-dependencies] | ||
instruments = ["mcp"] | ||
|
||
[project.entry-points.opentelemetry_instrumentor] | ||
mcp = "mcpinstrumentor:MCPInstrumentor" | ||
|
||
[tool.hatch.build.targets.sdist] | ||
include = [ | ||
"mcpinstrumentor.py", | ||
"README.md" | ||
] | ||
|
||
[tool.hatch.build.targets.wheel] | ||
packages = ["."] |
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can you organize your functions top-to-bottom (i.e. most important functions at top _wrap_send_request and _wrap_handle_request and then helper functions)