Skip to content

Commit 410beaf

Browse files
committed
refactor: enhance HTTP client response handling and session management
1 parent 9f31ad9 commit 410beaf

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

mock-client/http-client.ts

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
#!/usr/bin/env node
22
import fetch from 'node-fetch';
33

4+
// Helper function to parse SSE or JSON response
5+
async function parseResponse(response: any) {
6+
const contentType = response.headers.get('content-type');
7+
const text = await response.text();
8+
9+
if (contentType?.includes('text/event-stream')) {
10+
// Parse SSE format: "event: message\ndata: {json}\n"
11+
const dataMatch = text.match(/data: (.+)/);
12+
if (dataMatch) {
13+
return JSON.parse(dataMatch[1]);
14+
}
15+
return null;
16+
} else {
17+
return JSON.parse(text);
18+
}
19+
}
20+
421
// Simple HTTP client for testing MCP server in HTTP mode
522
async function testHTTPMode() {
6-
const baseUrl = process.env.MCP_URL || 'http://localhost:3000';
23+
const baseUrl = (process.env.MCP_URL || 'http://localhost:3000').replace(/\/$/, ''); // Remove trailing slash
724
const sessionId = `test-session-${Date.now()}`;
825

926
console.log('Testing Socket MCP in HTTP mode...');
@@ -31,14 +48,20 @@ async function testHTTPMode() {
3148
method: 'POST',
3249
headers: {
3350
'Content-Type': 'application/json',
34-
'x-session-id': sessionId
51+
'Accept': 'application/json, text/event-stream',
52+
'User-Agent': 'socket-mcp-debug-client/1.0.0'
3553
},
3654
body: JSON.stringify(initRequest)
3755
});
3856

39-
const initResult = await initResponse.json();
57+
const initResult = await parseResponse(initResponse);
4058
console.log('Initialize response:', JSON.stringify(initResult, null, 2));
4159

60+
// Extract session ID from response headers
61+
const serverSessionId = initResponse.headers.get('mcp-session-id');
62+
const actualSessionId = serverSessionId || sessionId;
63+
console.log('Session ID:', actualSessionId);
64+
4265
// 2. List tools
4366
console.log('\n2. Listing available tools...');
4467
const toolsRequest = {
@@ -52,12 +75,13 @@ async function testHTTPMode() {
5275
method: 'POST',
5376
headers: {
5477
'Content-Type': 'application/json',
55-
'x-session-id': sessionId
78+
'Accept': 'application/json, text/event-stream',
79+
'mcp-session-id': actualSessionId
5680
},
5781
body: JSON.stringify(toolsRequest)
5882
});
5983

60-
const toolsResult = await toolsResponse.json();
84+
const toolsResult = await parseResponse(toolsResponse);
6185
console.log('Available tools:', JSON.stringify(toolsResult, null, 2));
6286

6387
// 3. Call depscore
@@ -82,20 +106,21 @@ async function testHTTPMode() {
82106
method: 'POST',
83107
headers: {
84108
'Content-Type': 'application/json',
85-
'x-session-id': sessionId
109+
'Accept': 'application/json, text/event-stream',
110+
'mcp-session-id': actualSessionId
86111
},
87112
body: JSON.stringify(depscoreRequest)
88113
});
89114

90-
const depscoreResult = await depscoreResponse.json();
115+
const depscoreResult = await parseResponse(depscoreResponse);
91116
console.log('Depscore result:', JSON.stringify(depscoreResult, null, 2));
92117

93118
// 4. Test SSE stream (optional)
94119
console.log('\n4. Testing SSE stream connection...');
95120
const sseResponse = await fetch(`${baseUrl}/mcp`, {
96121
method: 'GET',
97122
headers: {
98-
'x-session-id': sessionId,
123+
'mcp-session-id': actualSessionId,
99124
'Accept': 'text/event-stream'
100125
}
101126
});
@@ -110,7 +135,7 @@ async function testHTTPMode() {
110135
const cleanupResponse = await fetch(`${baseUrl}/mcp`, {
111136
method: 'DELETE',
112137
headers: {
113-
'x-session-id': sessionId
138+
'mcp-session-id': actualSessionId
114139
}
115140
});
116141

0 commit comments

Comments
 (0)