2
2
3
3
This directory contains example implementations of MCP clients and servers using the TypeScript SDK.
4
4
5
- ## Streamable HTTP Examples
5
+ ## Streamable HTTP - single node deployment with basic session state management
6
6
7
- ### List Tool Request Example
8
-
9
- Using ` curl ` to list available tools:
10
-
11
- ``` bash
12
- # First initialize the server and save the session ID to a variable
13
- SESSION_ID=$( curl -X POST -H " Content-Type: application/json" -H " Accept: application/json, text/event-stream" \
14
- -d ' {"jsonrpc":"2.0","method":"initialize","params":{"capabilities":{}},"id":"1"}' \
15
- -i http://localhost:3000/mcp 2>&1 | grep -i " mcp-session-id" | cut -d' ' -f2 | tr -d ' \r' )
16
- echo " Session ID: $SESSION_ID "
17
-
18
- # Then list tools using the saved session ID
19
- curl -X POST -H " Content-Type: application/json" -H " Accept: application/json, text/event-stream" \
20
- -H " mcp-session-id: $SESSION_ID " \
21
- -d ' {"jsonrpc":"2.0","method":"tools/list","params":{},"id":"2"}' \
22
- http://localhost:3000/mcp
23
- ```
24
-
25
- Using the TypeScript client (session management is handled automatically):
26
-
27
- ``` typescript
28
- const toolsRequest = { method: ' tools/list' , params: {} };
29
- const toolsResult = await client .request (toolsRequest , ListToolsResultSchema );
30
- console .log (' Available tools:' , toolsResult .tools );
31
- ```
32
-
33
- ### Call Tool Request Example
34
-
35
- Using ` curl ` to call a tool:
36
-
37
- ``` bash
38
- curl -X POST -H " Content-Type: application/json" -H " Accept: application/json, text/event-stream" \
39
- -H " mcp-session-id: $SESSION_ID " \
40
- -d ' {"jsonrpc":"2.0","method":"tools/call","params":{"name":"greet","arguments":{"name":"User"}},"id":"3"}' \
41
- http://localhost:3000/mcp
42
- ```
43
-
44
- Using the TypeScript client:
45
-
46
- ``` typescript
47
- const greetRequest = {
48
- method: ' tools/call' ,
49
- params: {
50
- name: ' greet' ,
51
- arguments: { name: ' MCP User' }
52
- }
53
- };
54
- const greetResult = await client .request (greetRequest , CallToolResultSchema );
55
- ```
56
-
57
- ### Get Prompt Request Example
58
-
59
- Using ` curl ` to get a prompt:
60
-
61
- ``` bash
62
- curl -X POST -H " Content-Type: application/json" -H " Accept: application/json, text/event-stream" \
63
- -H " mcp-session-id: $SESSION_ID " \
64
- -d ' {"jsonrpc":"2.0","method":"prompts/get","params":{"name":"greeting-template","arguments":{"name":"User"}},"id":"4"}' \
65
- http://localhost:3000/mcp
66
- ```
67
-
68
- Using the TypeScript client:
69
-
70
- ``` typescript
71
- const promptRequest = {
72
- method: ' prompts/get' ,
73
- params: {
74
- name: ' greeting-template' ,
75
- arguments: { name: ' MCP User' }
76
- }
77
- };
78
- const promptResult = await client .request (promptRequest , GetPromptResultSchema );
79
- ```
7
+ Multi node with stete management example will be added soon after we add support.
80
8
81
9
### Server (` server/simpleStreamableHttp.ts ` )
82
10
@@ -92,11 +20,19 @@ A simple MCP server that uses the Streamable HTTP transport, implemented with Ex
92
20
npx tsx src/examples/server/simpleStreamableHttp.ts
93
21
```
94
22
95
- The server will start on port 3000. You can test the initialization with :
23
+ The server will start on port 3000. You can test the initialization and tool listing :
96
24
97
25
``` bash
98
- curl -X POST -H " Content-Type: application/json" -H " Accept: application/json, text/event-stream" \
26
+ # First initialize the server and save the session ID to a variable
27
+ SESSION_ID=$( curl -X POST -H " Content-Type: application/json" -H " Accept: application/json, text/event-stream" \
99
28
-d ' {"jsonrpc":"2.0","method":"initialize","params":{"capabilities":{}},"id":"1"}' \
29
+ -i http://localhost:3000/mcp 2>&1 | grep -i " mcp-session-id" | cut -d' ' -f2 | tr -d ' \r' )
30
+ echo " Session ID: $SESSION_ID "
31
+
32
+ # Then list tools using the saved session ID
33
+ curl -X POST -H " Content-Type: application/json" -H " Accept: application/json, text/event-stream" \
34
+ -H " mcp-session-id: $SESSION_ID " \
35
+ -d ' {"jsonrpc":"2.0","method":"tools/list","params":{},"id":"2"}' \
100
36
http://localhost:3000/mcp
101
37
```
102
38
@@ -119,5 +55,5 @@ Make sure the server is running before starting the client.
119
55
## Notes
120
56
121
57
- These examples demonstrate the basic usage of the Streamable HTTP transport
122
- - The server manages sessions for stateful connections
123
- - The client handles both direct HTTP responses and SSE streaming responses
58
+ - The server manages sessions between the calls
59
+ - The client handles both direct HTTP responses and SSE streaming responses
0 commit comments