Skip to content

Commit af23cec

Browse files
committed
fix: extract session ID from Mcp-Session-Id response header
Session ID is returned in the Mcp-Session-Id HTTP response header, not in SSE comments. Updated to: - Extract from response.headers['Mcp-Session-Id'] after first request - Send as 'Mcp-Session-Id' header in subsequent requests - Matches rmcp StreamableHttpService session protocol This fixes 401 Unauthorized error by properly managing session state.
1 parent 8bb3f8d commit af23cec

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

test_http_agentic_tools.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def send_request(self, payload, timeout=60):
182182

183183
# Add session ID if we have one
184184
if self.session_id:
185-
headers["X-Session-ID"] = self.session_id
185+
headers["Mcp-Session-Id"] = self.session_id
186186

187187
response = self.session.post(
188188
f"{self.base_url}/mcp",
@@ -193,21 +193,20 @@ def send_request(self, payload, timeout=60):
193193
)
194194

195195
if response.status_code == 200:
196-
# Parse SSE stream for JSON-RPC responses and session ID
196+
# Extract session ID from response header (first request only)
197+
if self.session_id is None and "Mcp-Session-Id" in response.headers:
198+
self.session_id = response.headers["Mcp-Session-Id"]
199+
print(f" 📝 Session ID: {self.session_id[:16]}...")
200+
201+
# Parse SSE stream for JSON-RPC responses
197202
result_data = None
198203

199204
for line in response.iter_lines(decode_unicode=True):
200205
if not line:
201206
continue
202207

203-
# SSE comments (session info)
208+
# Skip SSE comments
204209
if line.startswith(':'):
205-
# Check for session ID in comments
206-
if 'session:' in line:
207-
session_id = line.split('session:')[1].strip()
208-
if self.session_id is None:
209-
self.session_id = session_id
210-
print(f" 📝 Session ID: {session_id[:16]}...")
211210
continue
212211

213212
# SSE data events

0 commit comments

Comments
 (0)