1
1
# MCP over SSE
2
2
3
- [ ![ Module Version] ( https://img.shields.io/hexpm/v/mcp_sse.svg )] ( https://github.com/kEND/mcp_sse/releases )
4
- [ ![ Hex Docs] ( https://img.shields.io/badge/hex-docs-purple.svg )] ( https://hexdocs.pm/mcp_sse/ )
5
- [ ![ Total Download] ( https://img.shields.io/hexpm/dt/mcp_sse.svg )] ( https://hex.pm/packages/mcp_sse )
6
- [ ![ License] ( https://img.shields.io/hexpm/l/mcp_sse.svg )] ( https://github.com/kEND/mcp_sse/blob/main/LICENSE )
7
- [ ![ Last Updated] ( https://img.shields.io/github/last-commit/kEND/mcp_sse.svg )] ( https://github.com/kEND/mcp_sse/commits/main )
3
+ [ ![ Releases] ( https://img.shields.io/hexpm/v/mcp_sse.svg )] ( https://github.com/kEND/mcp_sse/releases )
4
+ [ ![ Documentation] ( https://img.shields.io/badge/hex-docs-purple.svg )] ( https://hexdocs.pm/mcp_sse/ )
5
+ [ ![ MCP Specification Version] ( https://img.shields.io/badge/spec-2024--11--05-blue )] ( https://modelcontextprotocol.io/specification/2024-11-05/index )
6
+ [ ![ Downloads] ( https://img.shields.io/hexpm/dt/mcp_sse.svg )] ( https://hex.pm/packages/mcp_sse )
7
+ [ ![ License] ( https://img.shields.io/badge/licence-MIT-blue.svg )] ( https://github.com/kEND/mcp_sse/blob/main/LICENSE )
8
+ [ ![ CI] ( https://github.com/kEND/mcp_sse/actions/workflows/ci.yml/badge.svg?branch=main )] ( https://github.com/kEND/mcp_sse/actions/workflows/ci.yml?query=branch%3Amain )
9
+ [ ![ Last Commit] ( https://img.shields.io/github/last-commit/kEND/mcp_sse.svg )] ( https://github.com/kEND/mcp_sse/commits/main )
8
10
9
11
This library provides a simple implementation of the Model Context Protocol (MCP) over Server-Sent Events (SSE).
10
12
11
13
For more information about the Model Context Protocol, visit:
12
14
[ Model Context Protocol Documentation] ( https://modelcontextprotocol.io/introduction )
13
15
16
+ ## Table of Contents
17
+
18
+ - [ Features] ( #features )
19
+ - [ Create your own MCP server] ( #create-your-own-mcp-server )
20
+ - [ Installation] ( #installation )
21
+ - [ For Phoenix Applications] ( #for-phoenix-applications )
22
+ - [ For Plug Applications with Bandit] ( #for-plug-applications-with-bandit )
23
+ - [ Usage] ( #usage )
24
+ - [ For Cursor] ( #for-cursor )
25
+ - [ Configuration Options] ( #configuration-options )
26
+ - [ Quick Demo] ( #quick-demo )
27
+ - [ Other Notes] ( #other-notes )
28
+ - [ Example Client Usage] ( #example-client-usage )
29
+ - [ Session Management] ( #session-management )
30
+ - [ SSE Keepalive] ( #sse-keepalive )
31
+ - [ MCP Response Formatting] ( #mcp-response-formatting )
32
+ - [ Contributing] ( #contributing )
33
+
34
+ ## Features
35
+
36
+ - Full MCP server implementation
37
+ - SSE connection management
38
+ - JSON-RPC message handling
39
+ - Tool registration and execution
40
+ - Session management
41
+ - Automatic ping/keepalive
42
+ - Error handling and validation
43
+
44
+ ## Create your own MCP server
45
+
46
+ You must implement the ` MCPServer ` behaviour.
47
+
48
+ You only need to implement the required callbacks (` handle_ping/1 ` and ` handle_initialize/2 ` ) and any optional callbacks for features you want to support.
49
+
50
+ The ` use MCPServer ` macro provides:
51
+ - Built-in message routing
52
+ - Protocol version validation
53
+ - Default implementations for optional callbacks
54
+ - JSON-RPC error handling
55
+ - Logging
56
+
57
+ See [ ` DefaultServer ` ] ( https://hexdocs.pm/mcp_sse/MCPServer.html#module-example ) for a default implementation of the ` MCPServer ` behaviour.
58
+
14
59
## Installation
15
60
16
61
### For Phoenix Applications:
@@ -24,8 +69,7 @@ config :mime, :types, %{
24
69
}
25
70
26
71
# Configure the MCP Server
27
- config :mcp_sse , :mcp_server , MCP .DefaultServer
28
- # config :mcp_sse, :mcp_server, YourApp.YourMCPServer
72
+ config :mcp_sse , :mcp_server , YourApp .YourMCPServer
29
73
```
30
74
31
75
2 . Add to your dependencies in ` mix.exs ` :
48
92
scope " /" do
49
93
pipe_through :sse
50
94
get " /sse" , SSE .ConnectionPlug , :call
51
-
52
- pipe_through :api
53
95
post " /message" , SSE .ConnectionPlug , :call
54
96
end
55
97
```
56
98
99
+ 4 . Run your application:
100
+
101
+ ``` bash
102
+ mix phx.server
103
+ ```
104
+
57
105
### For Plug Applications with Bandit:
58
106
59
107
1 . Create a new Plug application with supervision:
@@ -73,7 +121,7 @@ config :mime, :types, %{
73
121
}
74
122
75
123
# Configure the MCP Server
76
- config :mcp_sse , :mcp_server , YourApp .MCPServer
124
+ config :mcp_sse , :mcp_server , YourApp .YourMCPServer
77
125
```
78
126
79
127
3 . Add dependencies to ` mix.exs ` :
@@ -152,14 +200,48 @@ defmodule YourApp.Application do
152
200
end
153
201
```
154
202
155
- ### Session Management
203
+ 6 . Run your application:
156
204
157
- The MCP SSE server requires a session ID for each connection. The router automatically:
158
- - Uses an existing session ID from query parameters if provided
159
- - Generates a new session ID if none exists
160
- - Ensures all requests to ` /sse ` and ` /message ` endpoints have a valid session ID
205
+ ``` bash
206
+ mix run --no-halt
207
+ ```
208
+
209
+ ## Usage
161
210
162
- ### Configuration Options
211
+ ### With MCP Inspector
212
+
213
+ - Start the inspector:
214
+
215
+ ``` bash
216
+ MCP_SERVER_URL=localhost:4000 npx @modelcontextprotocol/inspector@latest
217
+ ```
218
+
219
+ - Navigate to http://localhost:6274/
220
+ - Make sure your server is running
221
+ - Click ` Connect `
222
+ - You can now list tools and call them
223
+
224
+ ### With Cursor
225
+
226
+ - Open Cursor Settings
227
+ - Navigate to the MCP tab
228
+ - Click ` Add new global MCP server `
229
+ - Fill in the ` ~/.cursor/mcp.json ` with:
230
+
231
+ ``` json
232
+ {
233
+ "mcpServers" : {
234
+ "your-mcp-server" : {
235
+ "url" : " http://localhost:4000/sse"
236
+ }
237
+ }
238
+ }
239
+ ```
240
+
241
+ - Make sure your server is running
242
+ - Ask Cursor to run one of your tools
243
+
244
+ ## Configuration Options
163
245
164
246
The Bandit server can be configured with additional options in your application module:
165
247
@@ -176,45 +258,20 @@ children = [
176
258
]
177
259
```
178
260
179
- The ` use MCPServer ` macro provides:
180
- - Built-in message routing
181
- - Protocol version validation
182
- - Default implementations for optional callbacks
183
- - JSON-RPC error handling
184
- - Logging
185
-
186
- You only need to implement the required callbacks (` handle_ping/1 ` and ` handle_initialize/2 ` ) and any optional callbacks for features you want to support.
187
-
188
- ## Protocol Specification
189
-
190
- For detailed information about the Model Context Protocol, visit:
191
- [ Model Context Protocol Specification] ( https://modelcontextprotocol.io/specification/2024-11-05/index )
192
-
193
- ## Features
194
-
195
- - Full MCP server implementation
196
- - SSE connection management
197
- - JSON-RPC message handling
198
- - Tool registration and execution
199
- - Session management
200
- - Automatic ping/keepalive
201
- - Error handling and validation
202
-
203
- ## Contributing
204
-
205
- - Fork the repository and clone it
206
- - Create a new branch in your fork
207
- - Make your changes and commit them
208
- - Push the changes to your fork
209
- - Open a pull request in upstream
210
-
211
261
## Quick Demo
212
262
213
263
To see the MCP server in action:
214
264
215
- 1 . Start the example server in one terminal:
265
+ 1 . Start a server in one terminal:
216
266
``` bash
267
+ # Our example server
217
268
elixir dev/example_server.exs
269
+
270
+ # Your Phoenix application
271
+ mix phx.server
272
+
273
+ # Your Plug application
274
+ mix run --no-halt
218
275
```
219
276
220
277
2 . In another terminal, run the demo client script:
@@ -229,7 +286,7 @@ The client script will:
229
286
- Call the upcase tool with example input
230
287
- Display the results of each step
231
288
232
- This provides a practical demonstration of the MCP protocol flow and server capabilities.
289
+ This provides a practical demonstration of the Model Context Protocol flow and server capabilities.
233
290
234
291
## Other Notes
235
292
@@ -261,14 +318,19 @@ fetch('/message?sessionId=YOUR_SESSION_ID', {
261
318
});
262
319
```
263
320
321
+ ### Session Management
322
+
323
+ The MCP SSE server requires a session ID for each connection. The router automatically:
324
+ - Uses an existing session ID from query parameters if provided
325
+ - Generates a new session ID if none exists
326
+ - Ensures all requests to ` /sse ` and ` /message ` endpoints have a valid session ID
327
+
264
328
### SSE Keepalive
265
329
266
330
The SSE connection sends periodic keepalive pings to prevent connection timeouts.
267
- You can configure the ping interval or disable it entirely:
331
+ You can configure the ping interval or disable it entirely in ` config/config.exs ` :
268
332
269
333
``` elixir
270
- # In config/config.exs
271
-
272
334
# Set custom ping interval (in milliseconds)
273
335
config :mcp_sse , :sse_keepalive_timeout , 30_000 # 30 seconds
274
336
355
417
```
356
418
357
419
For more details on response formatting, see the [ MCP Content Types Specification] ( https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/messages/#responses ) .
420
+
421
+ ## Contributing
422
+
423
+ - Fork the repository and clone it
424
+ - Create a new branch in your fork
425
+ - Make your changes and commit them
426
+ - Push the changes to your fork
427
+ - Open a pull request in upstream
0 commit comments