Skip to content

Commit 6e62eec

Browse files
authored
Merge pull request #203 from huang195/forward-token-weather-service
weather_service: forward inbound Authorization header to MCP tool calls
2 parents 74c2cf6 + 4a7f60e commit 6e62eec

2 files changed

Lines changed: 20 additions & 10 deletions

File tree

a2a/weather_service/src/weather_service/agent.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,20 @@ async def execute(self, context: RequestContext, event_queue: EventQueue):
122122
# Here we just run the agent logic - spans from LangChain are auto-captured
123123
output = None
124124

125+
# Forward inbound Authorization header to outbound MCP tool calls.
126+
# This enables transparent token exchange when deployed behind a waypoint
127+
# or AuthBridge proxy (same pattern as git_issue_agent, see c8ebde1).
128+
mcp_headers = None
129+
if context.call_context and (context.call_context.state or {}).get("headers", {}).get("authorization"):
130+
mcp_headers = {"Authorization": context.call_context.state["headers"]["authorization"]}
131+
logger.info("Forwarding inbound Authorization header to MCP tool calls")
132+
else:
133+
logger.warning("No inbound Authorization header; MCP tool calls will be unauthenticated")
134+
125135
# Test MCP connection first
126136
logger.info(f"Attempting to connect to MCP server at: {os.getenv('MCP_URL', 'http://localhost:8000/sse')}")
127137

128-
mcpclient = get_mcpclient()
138+
mcpclient = get_mcpclient(headers=mcp_headers)
129139

130140
# Try to get tools to verify connection
131141
try:

a2a/weather_service/src/weather_service/graph.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ class ExtendedMessagesState(MessagesState):
1616
final_answer: str = ""
1717

1818

19-
def get_mcpclient():
20-
return MultiServerMCPClient(
21-
{
22-
"math": {
23-
"url": os.getenv("MCP_URL", "http://localhost:8000/mcp"),
24-
"transport": os.getenv("MCP_TRANSPORT", "streamable_http"),
25-
}
26-
}
27-
)
19+
def get_mcpclient(headers=None):
20+
"""Create MCP client, optionally forwarding headers (e.g. Authorization)."""
21+
mcp_config = {
22+
"url": os.getenv("MCP_URL", "http://localhost:8000/mcp"),
23+
"transport": os.getenv("MCP_TRANSPORT", "streamable_http"),
24+
}
25+
if headers:
26+
mcp_config["headers"] = headers
27+
return MultiServerMCPClient({"math": mcp_config})
2828

2929

3030
async def get_graph(client) -> StateGraph:

0 commit comments

Comments
 (0)