1+ #!/usr/bin/env node
2+ import { spawn } from 'child_process' ;
3+ import readline from 'readline' ;
4+
5+ // Simple JSON-RPC client for testing MCP server
6+ class SimpleJSONRPCClient {
7+ private process : any ;
8+ private rl : readline . Interface ;
9+ private requestId = 1 ;
10+ private pendingRequests = new Map ( ) ;
11+
12+ constructor ( command : string , args : string [ ] = [ ] , env : any = { } ) {
13+ this . process = spawn ( command , args , {
14+ stdio : [ 'pipe' , 'pipe' , 'pipe' ] ,
15+ env : { ...process . env , ...env }
16+ } ) ;
17+
18+ this . rl = readline . createInterface ( {
19+ input : this . process . stdout ,
20+ crlfDelay : Infinity
21+ } ) ;
22+
23+ this . rl . on ( 'line' , ( line ) => {
24+ try {
25+ const response = JSON . parse ( line ) ;
26+ if ( response . id && this . pendingRequests . has ( response . id ) ) {
27+ const { resolve, reject } = this . pendingRequests . get ( response . id ) ;
28+ this . pendingRequests . delete ( response . id ) ;
29+
30+ if ( response . error ) {
31+ reject ( response . error ) ;
32+ } else {
33+ resolve ( response . result ) ;
34+ }
35+ } else if ( response . method ) {
36+ console . log ( 'Notification:' , response ) ;
37+ }
38+ } catch ( e ) {
39+ console . error ( 'Failed to parse response:' , line ) ;
40+ }
41+ } ) ;
42+
43+ this . process . stderr . on ( 'data' , ( data : Buffer ) => {
44+ console . error ( 'Server stderr:' , data . toString ( ) ) ;
45+ } ) ;
46+ }
47+
48+ async sendRequest ( method : string , params : any = { } ) {
49+ const id = this . requestId ++ ;
50+ const request = {
51+ jsonrpc : '2.0' ,
52+ id,
53+ method,
54+ params
55+ } ;
56+
57+ return new Promise ( ( resolve , reject ) => {
58+ this . pendingRequests . set ( id , { resolve, reject } ) ;
59+ this . process . stdin . write ( JSON . stringify ( request ) + '\n' ) ;
60+ } ) ;
61+ }
62+
63+ close ( ) {
64+ this . rl . close ( ) ;
65+ this . process . kill ( ) ;
66+ }
67+ }
68+
69+ async function main ( ) {
70+ const apiKey = process . env . SOCKET_API_KEY ;
71+ if ( ! apiKey ) {
72+ console . error ( 'Error: SOCKET_API_KEY environment variable is required' ) ;
73+ process . exit ( 1 ) ;
74+ }
75+
76+ console . log ( 'Starting MCP server debug client...' ) ;
77+
78+ const client = new SimpleJSONRPCClient ( './build/index.js' , [ ] , {
79+ SOCKET_API_KEY : apiKey
80+ } ) ;
81+
82+ try {
83+ // Initialize the connection
84+ console . log ( '\n1. Initializing connection...' ) ;
85+ const initResult = await client . sendRequest ( 'initialize' , {
86+ protocolVersion : '0.1.0' ,
87+ capabilities : { } ,
88+ clientInfo : {
89+ name : 'debug-client' ,
90+ version : '1.0.0'
91+ }
92+ } ) ;
93+ console . log ( 'Initialize response:' , JSON . stringify ( initResult , null , 2 ) ) ;
94+
95+ // List available tools
96+ console . log ( '\n2. Listing available tools...' ) ;
97+ const toolsResult = await client . sendRequest ( 'tools/list' , { } ) ;
98+ console . log ( 'Available tools:' , JSON . stringify ( toolsResult , null , 2 ) ) ;
99+
100+ // Call the depscore tool
101+ console . log ( '\n3. Calling depscore tool...' ) ;
102+ const depscoreResult = await client . sendRequest ( 'tools/call' , {
103+ name : 'depscore' ,
104+ arguments : {
105+ packages : [
106+ { depname : 'express' , ecosystem : 'npm' , version : '5.0.1' } ,
107+ { depname : 'lodash' , ecosystem : 'npm' , version : '4.17.21' } ,
108+ { depname : 'react' , ecosystem : 'npm' , version : '18.2.0' } ,
109+ { depname : 'flask' , ecosystem : 'pypi' , version : '2.3.2' } ,
110+ { depname : 'unknown-package' , ecosystem : 'npm' , version : 'unknown' }
111+ ]
112+ }
113+ } ) ;
114+ console . log ( 'Depscore result:' , JSON . stringify ( depscoreResult , null , 2 ) ) ;
115+
116+ // Test with minimal input
117+ console . log ( '\n4. Testing with minimal input (default to npm)...' ) ;
118+ const minimalResult = await client . sendRequest ( 'tools/call' , {
119+ name : 'depscore' ,
120+ arguments : {
121+ packages : [
122+ { depname : 'axios' } ,
123+ { depname : 'typescript' }
124+ ]
125+ }
126+ } ) ;
127+ console . log ( 'Minimal input result:' , JSON . stringify ( minimalResult , null , 2 ) ) ;
128+
129+ // Test error handling
130+ console . log ( '\n5. Testing error handling (empty packages)...' ) ;
131+ try {
132+ await client . sendRequest ( 'tools/call' , {
133+ name : 'depscore' ,
134+ arguments : {
135+ packages : [ ]
136+ }
137+ } ) ;
138+ } catch ( error ) {
139+ console . log ( 'Expected error:' , error ) ;
140+ }
141+
142+ console . log ( '\nDebug session complete!' ) ;
143+ } catch ( error ) {
144+ console . error ( 'Client error:' , error ) ;
145+ } finally {
146+ client . close ( ) ;
147+ }
148+ }
149+
150+ main ( ) . catch ( console . error ) ;
0 commit comments