-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathREADME.md.bak
More file actions
318 lines (259 loc) · 7.95 KB
/
Copy pathREADME.md.bak
File metadata and controls
318 lines (259 loc) · 7.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# MCPheonix
A simplified implementation of the Model Context Protocol (MCP) server using Elixir's Phoenix Framework.
## Overview
MCPheonix provides a server that implements the Model Context Protocol, allowing AI models to interact with your application data and functionality. This implementation is designed to be simple and easy to understand, without heavy dependencies on frameworks like Ash.
## Features
- Server-Sent Events (SSE) stream for real-time notifications
- JSON-RPC endpoint for client requests
- Simple resource system
- Event publish/subscribe mechanism
- Basic tool invocation
- Flux image generation
- Dart API for task managementintegration
- Dart task management integration
- Extensible MCP server architecture
## Getting Started
### Prerequisites
- Elixir 1.14 or higher
- Erlang 25 or higher
- Phoenix 1.7.0 or higher
- Python 3.9+ (for Flux and Dart integration)
- Node.js 18+ (for Dart MCP server)
### Installation
1. Clone the repository
```bash
git clone https://github.com/yourusername/mcpheonix.git
cd mcpheonix
```
2. Install dependencies
```bash
mix deps.get
```
3. Configure the Flux integration (if using image generation)
- Set up the Flux CLI environment as described in the [Flux Integration](#flux-integration) section
4. Configure the Dart integration (if using task management)
- Set up the Dart MCP server as described in the [Dart Integration](#dart-integration) section
5. Start the server
```bash
mix phx.server
```
The server will be available at http://localhost:4001.
### Adding Custom MCP Servers
MCPheonix is designed to work with multiple MCP servers. This system includes a flexible infrastructure for integrating custom MCP servers through:
1. **Simple JSON Configuration**: Define your server settings in `priv/config/mcp_servers.json`:
```json
{
"mcpServers": {
"your_server_id": {
"command": "/path/to/executable",
"args": ["arg1", "arg2"],
"env": {
"ENV_VAR1": "value1",
"ENV_VAR2": "value2"
},
"tools": {
"your_tool": {
"description": "Description of your tool",
"parameters": [
{ "name": "param1", "type": "string", "description": "Parameter description" }
]
}
}
}
}
}
```
2. **Automatic Server Management**: Servers are automatically loaded and managed during application startup.
For comprehensive implementation details, including the Elixir architecture, server lifecycle management, and protocol handling, see the [Adding MCP Servers](docs/adding_mcp_servers.md) documentation.
## MCP Endpoints
- **SSE Stream**: `GET /mcp/stream`
- Establishes a Server-Sent Events stream for receiving real-time notifications
- Returns a client ID in the response headers
- **JSON-RPC**: `POST /mcp/rpc`
- Accepts JSON-RPC 2.0 requests
- Client ID can be provided in the `x-mcp-client-id` header or will be generated if missing
## Built-in Capabilities
### Resources
- `user`: User resource
- Actions: `list`, `get`
- `message`: Message resource
- Actions: `list`, `get`
### Tools
- `echo`: Echoes back the input message
- Parameters: `message` (string)
- `timestamp`: Returns the current timestamp
- Parameters: none
- `random_number`: Generate a random number within a range
- Parameters: `min` (integer), `max` (integer)
- `generate_image`: Generate an image from a text prompt
- Parameters: `prompt` (string), `aspect_ratio` (string, optional), `model` (string, optional), `output` (string, optional)
- `img2img`: Generate an image using another image as reference
- Parameters: `image` (string), `prompt` (string), `name` (string), `strength` (number, optional)
- `create_task`: Create a new task in Dart
- Parameters: `title` (string), `description` (string), `dartboard_duid` (string), other parameters optional
- `get_default_space`: Get the default space DUID for a dartboard
- Parameters: `dartboard_duid` (string)
## Flux Integration
MCPheonix includes integration with the Flux CLI for image generation. This allows AI models to generate images or transform existing images through the MCP protocol.
### Setting Up Flux
1. Clone the Flux repository and set up the Python environment:
```bash
git clone https://github.com/Cascade-AI/flux.git
cd flux
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
```
2. Configure the Flux environment variables in `lib/mcpheonix/mcp/flux_server.ex`:
```elixir
@flux_dir "/path/to/your/flux/directory"
@virtual_env "/path/to/your/flux/.venv"
@bfl_api_key "your-api-key"
```
3. Make sure the Flux CLI is working by testing it directly:
```bash
cd /path/to/your/flux
.venv/bin/python fluxcli.py generate --prompt "Test image" --output test.jpg
```
### Using Image Generation Tools
Once set up, you can use the image generation tools through MCP:
#### Generate a new image:
```json
{
"jsonrpc": "2.0",
"method": "invoke_tool",
"params": {
"server_id": "flux",
"tool": "generate_image",
"parameters": {
"prompt": "A beautiful sunset over mountains",
"aspect_ratio": "16:9"
}
},
"id": 1
}
```
#### Transform an existing image:
```json
{
"jsonrpc": "2.0",
"method": "invoke_tool",
"params": {
"server_id": "flux",
"tool": "img2img",
"parameters": {
"image": "/path/to/input/image.jpg",
"prompt": "A beautiful sunset over mountains with birds",
"name": "transformed_image"
}
},
"id": 2
}
```
Generated images are saved to `~/Pictures/flux-generations/` by default and will be opened automatically.
## Dart Integration
MCPheonix integrates with the Dart MCP server to enable task management through the MCP protocol. This allows AI models to create and manage tasks in Dart.
### Setting Up Dart Integration
1. Clone the Dart MCP server repository:
```bash
git clone https://github.com/yourusername/dart-mcp-server.git
cd dart-mcp-server
```
2. Set up the Node.js and Python environments:
```bash
npm install
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
```
3. Build the server:
```bash
npm run build
```
4. Configure the Dart MCP server in `priv/config/mcp_servers.json`:
```json
{
"mcpServers": {
"dart": {
"command": "/path/to/node",
"args": ["/path/to/dart-mcp-server/build/index.js"],
"env": {
"PATH": "/path/to/dart-mcp-server/.venv/bin:/usr/local/bin:/usr/bin:/bin",
"PYTHONUNBUFFERED": "1",
"VIRTUAL_ENV": "/path/to/dart-mcp-server/.venv",
"DART_TOKEN": "your-dart-api-token"
}
}
}
}
```
5. Set up your Dart API token:
- Obtain a Dart API token from your Dart instance
- Add it to the `DART_TOKEN` environment variable in the config
### Using Dart Tools
Once set up, you can use the Dart tools through MCP:
#### Create a new task:
```json
{
"jsonrpc": "2.0",
"method": "invoke_tool",
"params": {
"server_id": "dart",
"tool": "create_task",
"parameters": {
"dartboard_duid": "your-dartboard-duid",
"title": "Test Task from MCP",
"description": "This task was created through the MCP protocol"
}
},
"id": 1
}
```
#### Get the default space for a dartboard:
```json
{
"jsonrpc": "2.0",
"method": "invoke_tool",
"params": {
"server_id": "dart",
"tool": "get_default_space",
"parameters": {
"dartboard_duid": "your-dartboard-duid"
}
},
"id": 2
}
```
For more details on the Dart integration, see the [Dart Integration Documentation](docs/dart_integration.md).
## Sample JSON-RPC Requests
Initialize the connection:
```json
{
"jsonrpc": "2.0",
"method": "initialize",
"id": 1
}
```
Invoke a tool:
```json
{
"jsonrpc": "2.0",
"method": "invoke_tool",
"params": {
"server_id": "core",
"tool": "echo",
"parameters": {
"message": "Hello, world!"
}
},
"id": 2
}
```
## License
This project is licensed under the MIT License - see the LICENSE file for details.
## Acknowledgments
- The MCP Protocol specification
- Phoenix Framework
- Elixir community
- Flux image generation
- Dart API for task management