Skip to content

Commit f3e14ce

Browse files
committed
http example.
1 parent ba43d31 commit f3e14ce

File tree

9 files changed

+2367
-6
lines changed

9 files changed

+2367
-6
lines changed

instrumentation-genai/opentelemetry-instrumentation-mcp/README.rst

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,22 @@ Automatically enabled with:
1616
opentelemetry-instrument python your_mcp_app.py
1717
```
1818

19-
## Configuration
19+
## Examples
20+
21+
See the `examples/` directory for complete working examples:
22+
23+
- **simple-client-server**: stdio transport example with Jaeger integration
24+
- **http-transport**: HTTP/SSE transport example with console exporter
25+
26+
## Supported Transports
27+
28+
- **stdio**: Process-based communication (stdin/stdout)
29+
- **HTTP/SSE**: Network-based communication with Server-Sent Events
30+
31+
Both transports are fully instrumented with automatic trace context propagation.
2032

2133
## Spans Created
2234

23-
- **Client**:
24-
- Initialize: `mcp.initialize`
25-
- List Tools: `mcp.list_tools`
26-
- Call Tool: `mcp.call_tool.{tool_name}`
27-
- **Server**: `tools/initialize`, `tools/list`, `tools/{tool_name}`
35+
- **Client spans**: `mcp.client` with method-specific names (e.g., `tools/call add`)
36+
- **Server spans**: `mcp.server` with method-specific names
37+
- Distributed tracing via W3C trace context in message metadata
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# MCP HTTP Transport Example
2+
3+
This example demonstrates OpenTelemetry instrumentation for MCP using HTTP transport with Server-Sent Events (SSE).
4+
5+
## Running the Server
6+
7+
### 1. Install Dependencies
8+
9+
```bash
10+
cd server
11+
uv sync
12+
uv run opentelemetry-bootstrap -a install
13+
14+
# Install MCP instrumentation library
15+
uv run pip install -e ../../../../opentelemetry-instrumentation-mcp
16+
```
17+
18+
### 2. Start the Server
19+
20+
```bash
21+
cd server
22+
23+
OTEL_SERVICE_NAME=mcp-server-PyHttp \
24+
OTEL_TRACES_EXPORTER=otlp \
25+
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://xyz-jaeger-100:4317/v1/traces \
26+
uv run opentelemetry-instrument python ./server.py
27+
```
28+
29+
The server will start on `http://localhost:8000`.
30+
31+
## Running the Client
32+
33+
### 1. Install Dependencies
34+
35+
```bash
36+
cd client
37+
uv sync
38+
uv run opentelemetry-bootstrap -a install
39+
40+
# Install MCP instrumentation library
41+
uv run pip install -e ../../../../opentelemetry-instrumentation-mcp
42+
```
43+
44+
### 2. Run the Client
45+
46+
```bash
47+
cd client
48+
49+
OTEL_SERVICE_NAME=mcp-client-PyHttp \
50+
OTEL_TRACES_EXPORTER=otlp \
51+
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://xyz-jaeger-100:4317/v1/traces \
52+
uv run opentelemetry-instrument python ./client.py
53+
```
54+
55+
## Jaeger Trace Visualization
56+
57+
![MCP HTTP Transport Trace](mcphttp.png)
58+
59+
60+
## How It Works
61+
62+
### Transport Layer
63+
64+
- **Server**: Uses `mcp.server.sse` for HTTP transport with Server-Sent Events
65+
- **Client**: Uses `mcp.client.sse.sse_client()` to connect via HTTP
66+
- **Communication**: Real-time bidirectional communication over HTTP/SSE
67+
68+
### Instrumentation
69+
70+
The OpenTelemetry instrumentation automatically:
71+
72+
1. **Traces all MCP operations**: Tool calls, resource reads, prompt handling, and session lifecycle
73+
2. **Injects W3C trace context**: Embeds trace context into MCP message metadata
74+
3. **Propagates context**: Maintains trace continuity across HTTP requests
75+
4. **Links spans**: Creates parent-child relationships between client and server operations
76+
77+
## Transport Comparison
78+
79+
| Feature | stdio Transport | HTTP/SSE Transport |
80+
|---------|----------------|--------------------|
81+
| Communication | Process-based (stdin/stdout) | Network-based (HTTP) |
82+
| Use Case | Local processes | Distributed systems |
83+
| Instrumentation | Fully supported | Fully supported |
84+
| Trace Propagation | W3C trace context | W3C trace context |
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python3
2+
"""MCP client with HTTP transport using SSE."""
3+
4+
import asyncio
5+
6+
from mcp import ClientSession
7+
from mcp.client.sse import sse_client
8+
from pydantic import AnyUrl
9+
10+
from opentelemetry import trace
11+
12+
13+
async def main():
14+
tracer = trace.get_tracer(__name__)
15+
16+
with tracer.start_as_current_span("mcp_http_client"):
17+
# Connect to MCP server via HTTP
18+
async with sse_client("http://localhost:8000/sse") as (read, write):
19+
async with ClientSession(read, write) as session:
20+
# Initialize
21+
await session.initialize()
22+
23+
# List tools
24+
tools = await session.list_tools()
25+
print(f"Available tools: {[t.name for t in tools.tools]}")
26+
27+
# Call tool
28+
result = await session.call_tool(
29+
"add", arguments={"a": 5, "b": 3}
30+
)
31+
print(f"add(5, 3) = {result.content}")
32+
33+
# Read resource
34+
greeting = await session.read_resource(
35+
AnyUrl("greeting://World")
36+
)
37+
print(f"Greeting: {greeting}")
38+
39+
40+
if __name__ == "__main__":
41+
asyncio.run(main())
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[project]
2+
name = "mcp-http-client-example"
3+
version = "0.1.0"
4+
requires-python = ">=3.10"
5+
dependencies = [
6+
"mcp>=1.8.1",
7+
"opentelemetry-api>=1.37.0",
8+
"opentelemetry-distro>=0.58b0",
9+
"opentelemetry-exporter-otlp>=1.37.0",
10+
"opentelemetry-instrumentation-mcp",
11+
"opentelemetry-sdk",
12+
"pip>=25.2",
13+
]

0 commit comments

Comments
 (0)