1
1
#!/usr/bin/env node
2
2
import fetch from 'node-fetch' ;
3
3
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 ( / d a t a : ( .+ ) / ) ;
12
+ if ( dataMatch ) {
13
+ return JSON . parse ( dataMatch [ 1 ] ) ;
14
+ }
15
+ return null ;
16
+ } else {
17
+ return JSON . parse ( text ) ;
18
+ }
19
+ }
20
+
4
21
// Simple HTTP client for testing MCP server in HTTP mode
5
22
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
7
24
const sessionId = `test-session-${ Date . now ( ) } ` ;
8
25
9
26
console . log ( 'Testing Socket MCP in HTTP mode...' ) ;
@@ -31,14 +48,20 @@ async function testHTTPMode() {
31
48
method : 'POST' ,
32
49
headers : {
33
50
'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'
35
53
} ,
36
54
body : JSON . stringify ( initRequest )
37
55
} ) ;
38
56
39
- const initResult = await initResponse . json ( ) ;
57
+ const initResult = await parseResponse ( initResponse ) ;
40
58
console . log ( 'Initialize response:' , JSON . stringify ( initResult , null , 2 ) ) ;
41
59
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
+
42
65
// 2. List tools
43
66
console . log ( '\n2. Listing available tools...' ) ;
44
67
const toolsRequest = {
@@ -52,12 +75,13 @@ async function testHTTPMode() {
52
75
method : 'POST' ,
53
76
headers : {
54
77
'Content-Type' : 'application/json' ,
55
- 'x-session-id' : sessionId
78
+ 'Accept' : 'application/json, text/event-stream' ,
79
+ 'mcp-session-id' : actualSessionId
56
80
} ,
57
81
body : JSON . stringify ( toolsRequest )
58
82
} ) ;
59
83
60
- const toolsResult = await toolsResponse . json ( ) ;
84
+ const toolsResult = await parseResponse ( toolsResponse ) ;
61
85
console . log ( 'Available tools:' , JSON . stringify ( toolsResult , null , 2 ) ) ;
62
86
63
87
// 3. Call depscore
@@ -82,20 +106,21 @@ async function testHTTPMode() {
82
106
method : 'POST' ,
83
107
headers : {
84
108
'Content-Type' : 'application/json' ,
85
- 'x-session-id' : sessionId
109
+ 'Accept' : 'application/json, text/event-stream' ,
110
+ 'mcp-session-id' : actualSessionId
86
111
} ,
87
112
body : JSON . stringify ( depscoreRequest )
88
113
} ) ;
89
114
90
- const depscoreResult = await depscoreResponse . json ( ) ;
115
+ const depscoreResult = await parseResponse ( depscoreResponse ) ;
91
116
console . log ( 'Depscore result:' , JSON . stringify ( depscoreResult , null , 2 ) ) ;
92
117
93
118
// 4. Test SSE stream (optional)
94
119
console . log ( '\n4. Testing SSE stream connection...' ) ;
95
120
const sseResponse = await fetch ( `${ baseUrl } /mcp` , {
96
121
method : 'GET' ,
97
122
headers : {
98
- 'x -session-id' : sessionId ,
123
+ 'mcp -session-id' : actualSessionId ,
99
124
'Accept' : 'text/event-stream'
100
125
}
101
126
} ) ;
@@ -110,7 +135,7 @@ async function testHTTPMode() {
110
135
const cleanupResponse = await fetch ( `${ baseUrl } /mcp` , {
111
136
method : 'DELETE' ,
112
137
headers : {
113
- 'x -session-id' : sessionId
138
+ 'mcp -session-id' : actualSessionId
114
139
}
115
140
} ) ;
116
141
0 commit comments