1
1
// TypeScript SSE client example for rmcp_typescript
2
2
// Closely mirrors the structure and flow of sse.rs from the core Rust SDK
3
3
4
- import { JsTransport , JsTransportEnum , JsClientInfo , JsImplementation } from '../../index ' ;
4
+ import { JsTransport , JsSseTransport , JsClientInfo , JsImplementation , JsClientCapabilities , JsExperimentalCapabilities , JsRootsCapabilities , JsClient } from 'rmcp-typescript ' ;
5
5
6
6
// TODO: Replace 'any' with proper types from WASM/SDK when available
7
7
// type ClientCapabilities = any;
@@ -13,52 +13,41 @@ async function main() {
13
13
14
14
// Step 2: Create transport
15
15
const sseEndpoint = 'http://localhost:8000/sse' ;
16
- const transport = new JsTransport ( JsTransportEnum . SSE , sseEndpoint ) ;
16
+ const sseTransport = await JsSseTransport . start ( sseEndpoint ) ;
17
+ console . log ( 'sseTransport:' , sseTransport ) ;
18
+ const transport = JsTransport . fromSse ( sseTransport ) ;
19
+ console . log ( 'transport:' , transport . kind ) ;
17
20
18
21
// Step 3: Define client info (mirror Rust structure)
19
- const clientInfo : JsClientInfo = {
20
- protocolVersion : '2024-11-05' , // TODO: Use ProtocolVersion.latest() if available
21
- capabilities : { } , // TODO: Use proper ClientCapabilities
22
- clientInfo : {
23
- name : 'typescript-sse-client' ,
24
- version : '0.0.1' ,
25
- } ,
26
- } ;
22
+ const experimental = JsExperimentalCapabilities . new ( { } ) ;
23
+ const roots = new JsRootsCapabilities ( ) ;
24
+ const sampling = null ;
25
+ console . log ( 'JsClientCapabilities.new args:' , { experimental, roots, sampling } ) ;
26
+ const clientInfo = new JsClientInfo (
27
+ '2024-11-05' ,
28
+ new JsClientCapabilities ( experimental , roots , sampling ) ,
29
+ new JsImplementation ( 'typescript-sse-client' , '0.0.1' )
30
+ ) ;
27
31
28
32
try {
29
- // Step 4: Connect and serve (stub for now)
30
- transport . start (
31
- ( data : string ) => {
32
- console . log ( 'Received SSE message:' , data ) ;
33
- // TODO: Parse and handle protocol messages here (initialize, tool list, etc.)
34
- } ,
35
- ( err : Event ) => {
36
- console . error ( 'SSE error:' , err ) ;
37
- }
38
- ) ;
39
-
40
- // TODO: Replace with real async/await protocol flow when WASM/SDK methods are available
41
- // Example (pseudo-code):
42
- /*
43
- const client = await clientInfo.serve(transport);
33
+ // Step 4: Start the client and get peer info
34
+ const clientObj = await clientInfo . serve ( transport ) ;
35
+ const client = clientObj . inner as JsClient ;
44
36
const serverInfo = client . peerInfo ( ) ;
45
37
console . info ( 'Connected to server:' , serverInfo ) ;
46
38
47
- const tools = await client.listTools({});
39
+ // Step 5: List available tools
40
+ const tools = await client . listAllTools ( ) ;
48
41
console . info ( 'Available tools:' , tools ) ;
49
42
50
- const toolResult = await client.callTool({
51
- name: 'increment',
52
- arguments: {},
53
- });
54
- console.info('Tool result:', toolResult);
43
+ // Step 6: Call a tool (example)
44
+
45
+ const result = await client . callTool ( "increment" , { } ) ;
46
+ console . info ( 'Tool result:' , result ) ;
55
47
56
- await client.cancel();
57
- */
58
48
59
- // For now, keep connection open for demonstration
49
+ // Keep connection open for demonstration
60
50
setTimeout ( ( ) => {
61
- transport . close ( ) ;
62
51
console . info ( 'Connection closed.' ) ;
63
52
} , 10000 ) ;
64
53
0 commit comments