Skip to content

Commit 40a930d

Browse files
committed
⚗️ Trace to tool attempt
Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com>
1 parent eaf9949 commit 40a930d

2 files changed

Lines changed: 32 additions & 6 deletions

File tree

a2a/weather_service/src/weather_service/agent.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,18 @@ async def execute(self, context: RequestContext, event_queue: EventQueue):
119119
# Test MCP connection first
120120
logger.info(f'Attempting to connect to MCP server at: {os.getenv("MCP_URL", "http://localhost:8000/sse")}')
121121

122-
mcpclient = get_mcpclient()
122+
# Extract current trace context for propagation to Envoy/MCP
123+
traceparent = None
124+
root_span = get_root_span()
125+
if root_span and root_span.is_recording():
126+
span_context = root_span.get_span_context()
127+
if span_context.is_valid:
128+
# Format traceparent according to W3C Trace Context spec
129+
# Format: version-trace_id-span_id-trace_flags
130+
traceparent = f"00-{format(span_context.trace_id, '032x')}-{format(span_context.span_id, '016x')}-{format(span_context.trace_flags, '02x')}"
131+
logger.info(f'Propagating trace context to MCP: {traceparent}')
132+
133+
mcpclient = get_mcpclient(traceparent=traceparent)
123134

124135
# Try to get tools to verify connection
125136
try:

a2a/weather_service/src/weather_service/graph.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,27 @@
1212
class ExtendedMessagesState(MessagesState):
1313
final_answer: str = ""
1414

15-
def get_mcpclient():
16-
return MultiServerMCPClient({
17-
"math": {
18-
"url": os.getenv("MCP_URL", "http://localhost:8000/mcp"),
19-
"transport": os.getenv("MCP_TRANSPORT", "streamable_http"),
15+
def get_mcpclient(traceparent: str | None = None):
16+
"""
17+
Create an MCP client with optional trace context propagation.
18+
19+
Args:
20+
traceparent: W3C Trace Context traceparent header value
21+
Format: "00-{trace-id}-{parent-id}-{trace-flags}"
22+
"""
23+
server_config = {
24+
"url": os.getenv("MCP_URL", "http://localhost:8000/mcp"),
25+
"transport": os.getenv("MCP_TRANSPORT", "streamable_http"),
26+
}
27+
28+
# Add traceparent header if provided for distributed tracing through Envoy
29+
if traceparent:
30+
server_config["headers"] = {
31+
"traceparent": traceparent
2032
}
33+
34+
return MultiServerMCPClient({
35+
"math": server_config
2136
})
2237

2338
async def get_graph(client) -> StateGraph:

0 commit comments

Comments
 (0)