File tree Expand file tree Collapse file tree 3 files changed +995
-14
lines changed
examples/clients/typescript Expand file tree Collapse file tree 3 files changed +995
-14
lines changed Original file line number Diff line number Diff line change 1+ #!/usr/bin/env node
2+
3+ import { Client } from '@modelcontextprotocol/sdk/client/index.js' ;
4+ import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js' ;
5+
6+ async function main ( ) : Promise < void > {
7+ const serverUrl = process . argv [ 2 ] ;
8+
9+ if ( ! serverUrl ) {
10+ console . error ( 'Usage: test-client <server-url>' ) ;
11+ process . exit ( 1 ) ;
12+ }
13+
14+ console . log ( `Connecting to MCP server at: ${ serverUrl } ` ) ;
15+
16+ try {
17+ const client = new Client ( {
18+ name : 'test-client' ,
19+ version : '1.0.0'
20+ } , {
21+ capabilities : { }
22+ } ) ;
23+
24+ const transport = new StreamableHTTPClientTransport (
25+ new URL ( serverUrl )
26+ ) ;
27+
28+ await client . connect ( transport ) ;
29+ console . log ( '✅ Successfully connected to MCP server' ) ;
30+
31+ await client . listTools ( ) ;
32+ console . log ( '✅ Successfully listed tools' ) ;
33+
34+ await transport . close ( ) ;
35+ console . log ( '✅ Connection closed successfully' ) ;
36+
37+ process . exit ( 0 ) ;
38+ } catch ( error ) {
39+ console . error ( '❌ Failed to connect to MCP server:' , error ) ;
40+ process . exit ( 1 ) ;
41+ }
42+ }
43+
44+ main ( ) . catch ( ( error ) => {
45+ console . error ( 'Unhandled error:' , error ) ;
46+ process . exit ( 1 ) ;
47+ } ) ;
You can’t perform that action at this time.
0 commit comments