Skip to content

Commit 93124ce

Browse files
committed
Refactor MCP server for FastMCP and update documentation
Rewrote and streamlined the MCP server to use FastMCP as the core, removed legacy FastAPI/HTTP API code, and improved argument parsing for transport selection. Updated and condensed README.md, and added a new, comprehensive README_NEW.md with clear instructions, architecture, and usage examples for FastMCP transports and CLI. The server now focuses on FastMCP transports (stdio, http, streamable-http, sse) and provides improved logging and configuration.
1 parent a61a31f commit 93124ce

File tree

3 files changed

+543
-238
lines changed

3 files changed

+543
-238
lines changed

src/backend/v3/mcp_server/README.md

Lines changed: 120 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,14 @@
11
# MACAE MCP Server
22

3-
A unified Model Context Protocol (MCP) server for the Multi-Agent Custom Automation Engine (MACAE) solution accelerator.
3+
A FastMCP-based Model Context Protocol (MCP) server for the Multi-Agent Custom Automation Engine (MACAE) solution accelerator.
44

55
## Features
66

7-
- **Unified Server**: Single `mcp_server.py` supporting both HTTP API and MCP protocol
8-
-# Get HR tools (with auth if enabled)
9-
curl -H "Authorization: Bearer YOUR_TOKEN" http://localhost:9000/tools/hr
10-
11-
````
12-
13-
## MCP Client Usage
14-
15-
For MCP clients, connect to the MCP server:
16-
17-
```python
18-
from fastmcp import Client
19-
20-
client = Client("http://localhost:9000")
21-
22-
async with client:
23-
result = await client.call_tool("greet", {"name": "World"})
24-
print(result)
25-
````
26-
27-
### Quick Test
28-
29-
**Test HTTP API mode:**
30-
31-
```bash
32-
# Start the HTTP server
33-
python mcp_server.py --mode http
34-
35-
# In another terminal, test the API
36-
curl http://localhost:9000/tools | jq
37-
curl -X POST http://localhost:9000/tools/list_employees | jq
38-
```
39-
40-
**Test MCP Protocol mode:**
41-
42-
```bash
43-
# Start the MCP server
44-
python mcp_server.py --mode mcp
45-
46-
# Test with the included client
47-
python client_example.py
48-
```
49-
50-
**Test with FastMCP CLI (recommended for MCP clients):**
51-
52-
```bash
53-
# Using fastmcp run command (streamable HTTP transport)
54-
fastmcp run mcp_server.py -t streamable-http --port 9000 -l DEBUG
55-
56-
# Or use the helper script
57-
python run_fastmcp.py
58-
59-
# Server will be available at: http://127.0.0.1:9000/mcp/
60-
```
61-
62-
## Troubleshooting
63-
64-
### Common Issues
65-
66-
1. **Import Errors**: Make sure you're in the correct directory and dependencies are installed
67-
2. **Authentication Errors**: Check your Azure AD configuration and tokens
68-
3. **Port Conflicts**: Change the port in configuration if 9000 is already in use
69-
4. **Missing fastmcp**: Install with `pip install fastmcp`**Factory Pattern**: Reusable MCP tools factory for easy service management
70-
7+
- **FastMCP Server**: Pure FastMCP implementation supporting multiple transport protocols
8+
- **Factory Pattern**: Reusable MCP tools factory for easy service management
719
- **Domain-Based Organization**: Services organized by business domains (HR, Tech Support, etc.)
7210
- **Authentication**: Optional Azure AD authentication support
73-
- **Dual Protocol Support**: HTTP API (FastAPI) and MCP protocol in one server
11+
- **Multiple Transports**: STDIO, HTTP (Streamable), and SSE transport support
7412
- **Docker Support**: Containerized deployment with health checks
7513
- **VS Code Integration**: Debug configurations and development settings
7614
- **Comprehensive Testing**: Unit tests with pytest
@@ -95,7 +33,7 @@ src/backend/v3/mcp_server/
9533
├── config/ # Configuration management
9634
│ ├── __init__.py
9735
│ └── settings.py # Settings and configuration
98-
├── mcp_server.py # Unified server (HTTP API + MCP protocol)
36+
├── mcp_server.py # FastMCP server implementation
9937
├── requirements.txt # Python dependencies
10038
├── uv.lock # Lock file for dependencies
10139
├── Dockerfile # Container configuration
@@ -155,39 +93,50 @@ src/backend/v3/mcp_server/
15593

15694
4. **Start the Server**:
15795

158-
````bash
159-
# HTTP API mode (default) - for REST clients
96+
```bash
97+
# Default STDIO transport (for local MCP clients)
16098
python mcp_server.py
16199

162-
# MCP protocol mode - for MCP clients
163-
python mcp_server.py --mode mcp
100+
# HTTP transport (for web-based clients)
101+
python mcp_server.py --transport http --port 9000
164102

165-
# FastMCP CLI (recommended for MCP clients) - streamable HTTP transport
103+
# Using FastMCP CLI (recommended)
166104
fastmcp run mcp_server.py -t streamable-http --port 9000 -l DEBUG
167105

168106
# Debug mode with authentication disabled
169-
python mcp_server.py --debug --no-auth
170-
```### Server Modes
171-
````
107+
python mcp_server.py --transport http --debug --no-auth
108+
```
172109

173-
**The unified `mcp_server.py` supports two modes:**
110+
### Transport Options
174111

175-
**1. HTTP API Mode (default)**
112+
**1. STDIO Transport (default)**
113+
- 🔧 Perfect for: Local tools, command-line integrations, Claude Desktop
114+
- 🚀 Usage: `python mcp_server.py` or `python mcp_server.py --transport stdio`
176115

177-
- 🌐 FastAPI-based REST API
178-
- 📚 Automatic Swagger/OpenAPI documentation
179-
- 🔗 CORS support for web clients
180-
- 🔧 HTTP endpoints for all MCP tools
181-
- 💡 Perfect for: Web apps, curl, Postman, REST clients
182-
- 🌐 **Port: 9000**
116+
**2. HTTP (Streamable) Transport**
117+
- 🌐 Perfect for: Web-based deployments, microservices, remote access
118+
- 🚀 Usage: `python mcp_server.py --transport http --port 9000`
119+
- 🌐 URL: `http://127.0.0.1:9000/mcp/`
183120

184-
**2. MCP Protocol Mode**
121+
**3. SSE Transport (deprecated)**
122+
- ⚠️ Legacy support only - use HTTP transport for new projects
123+
- 🚀 Usage: `python mcp_server.py --transport sse --port 9000`
185124

186-
- 🤖 Model Context Protocol compliant
187-
- ⚡ Direct tool invocation protocol
188-
- 🔌 MCP client compatibility
189-
- 💡 Perfect for: AI agents, MCP-compatible clients
190-
- 🌐 **Port: 9000**
125+
### FastMCP CLI Usage
126+
127+
```bash
128+
# Standard HTTP server
129+
fastmcp run mcp_server.py -t streamable-http --port 9000 -l DEBUG
130+
131+
# With custom host
132+
fastmcp run mcp_server.py -t streamable-http --host 0.0.0.0 --port 9000 -l DEBUG
133+
134+
# STDIO transport (for local clients)
135+
fastmcp run mcp_server.py -t stdio
136+
137+
# Development mode with MCP Inspector
138+
fastmcp dev mcp_server.py -t streamable-http --port 9000
139+
```
191140

192141
### Docker Deployment
193142

@@ -197,10 +146,9 @@ src/backend/v3/mcp_server/
197146
docker-compose up --build
198147
```
199148

200-
2. **Access the API**:
201-
- Health check: http://localhost:9000/health
202-
- API docs: http://localhost:9000/docs (when debug=true)
203-
- Tools summary: http://localhost:9000/tools
149+
2. **Access the Server**:
150+
- MCP endpoint: http://localhost:9000/mcp/
151+
- Health check available via custom routes
204152

205153
### VS Code Development
206154

@@ -211,8 +159,8 @@ src/backend/v3/mcp_server/
211159
```
212160

213161
2. **Use Debug Configurations**:
214-
- `Debug MCP Server (MCP mode)`: Run the server in MCP protocol mode
215-
- `Debug HTTP Server (HTTP mode)`: Run the server in HTTP API mode
162+
- `Debug MCP Server (STDIO)`: Run with STDIO transport
163+
- `Debug MCP Server (HTTP)`: Run with HTTP transport
216164
- `Debug Tests`: Run the test suite
217165

218166
## Configuration
@@ -295,40 +243,73 @@ pytest --cov=. src/tests/mcp_server/
295243
pytest src/tests/mcp_server/test_hr_service.py -v
296244
```
297245

298-
## API Endpoints (HTTP Server)
246+
## MCP Client Usage
247+
248+
### Python Client
249+
250+
```python
251+
from fastmcp import Client
299252

300-
### Available Endpoints
253+
# Connect to HTTP server
254+
client = Client("http://localhost:9000")
301255

302-
- `GET /`: Server information
303-
- `GET /health`: Health check
304-
- `GET /tools`: Get all tools summary
305-
- `GET /tools/{domain}`: Get tools for specific domain
256+
async with client:
257+
# List available tools
258+
tools = await client.list_tools()
259+
print(f"Available tools: {[tool.name for tool in tools]}")
260+
261+
# Call a tool
262+
result = await client.call_tool("greet", {"name": "World"})
263+
print(result)
264+
```
306265

307-
### Example API Calls
266+
### Command Line Testing
308267

309268
```bash
310-
# Health check
311-
curl http://localhost:9000/health
269+
# Test the server is running
270+
curl http://localhost:9000/mcp/
312271

313-
# Get tools summary
314-
curl http://localhost:9000/tools
272+
# With FastMCP CLI for testing
273+
fastmcp dev mcp_server.py -t streamable-http --port 9000
274+
```
275+
276+
## Quick Test
277+
278+
**Test STDIO Transport:**
279+
280+
```bash
281+
# Start server in STDIO mode
282+
python mcp_server.py --debug --no-auth
315283

316-
# Get HR tools (with auth if enabled)
317-
curl -H "Authorization: Bearer YOUR_TOKEN" http://localhost:8000/tools/hr
284+
# Test with client_example.py
285+
python client_example.py
318286
```
319287

320-
## MCP Client Usage
288+
**Test HTTP Transport:**
321289

322-
For MCP clients, connect to the FastMCP server:
290+
```bash
291+
# Start HTTP server
292+
python mcp_server.py --transport http --port 9000 --debug --no-auth
323293

324-
```python
294+
# Test with FastMCP client
295+
python -c "
325296
from fastmcp import Client
297+
import asyncio
298+
async def test():
299+
async with Client('http://localhost:9000') as client:
300+
result = await client.call_tool('greet', {'name': 'Test'})
301+
print(result)
302+
asyncio.run(test())
303+
"
304+
```
326305

327-
client = Client("http://localhost:8000")
306+
**Test with FastMCP CLI:**
328307

329-
async with client:
330-
result = await client.call_tool("greet", {"name": "World"})
331-
print(result)
308+
```bash
309+
# Start with FastMCP CLI
310+
fastmcp run mcp_server.py -t streamable-http --port 9000 -l DEBUG
311+
312+
# Server will be available at: http://127.0.0.1:9000/mcp/
332313
```
333314

334315
## Troubleshooting
@@ -337,13 +318,19 @@ async with client:
337318

338319
1. **Import Errors**: Make sure you're in the correct directory and dependencies are installed
339320
2. **Authentication Errors**: Check your Azure AD configuration and tokens
340-
3. **Port Conflicts**: Change the port in configuration if 8000 is already in use
321+
3. **Port Conflicts**: Change the port in configuration if 9000 is already in use
341322
4. **Missing fastmcp**: Install with `pip install fastmcp`
342323

343324
### Debug Mode
344325

345326
Enable debug mode for detailed logging:
346327

328+
```bash
329+
python mcp_server.py --debug --no-auth
330+
```
331+
332+
Or set in environment:
333+
347334
```env
348335
MCP_DEBUG=true
349336
```
@@ -356,6 +343,23 @@ Check container logs:
356343
docker-compose logs mcp-server
357344
```
358345

346+
## Server Arguments
347+
348+
```bash
349+
usage: mcp_server.py [-h] [--transport {stdio,http,streamable-http,sse}]
350+
[--host HOST] [--port PORT] [--debug] [--no-auth]
351+
352+
MACAE MCP Server
353+
354+
options:
355+
-h, --help show this help message and exit
356+
--transport, -t Transport protocol (default: stdio)
357+
--host HOST Host to bind to for HTTP transport (default: 127.0.0.1)
358+
--port, -p PORT Port to bind to for HTTP transport (default: 9000)
359+
--debug Enable debug mode
360+
--no-auth Disable authentication
361+
```
362+
359363
## Contributing
360364

361365
1. Follow the existing code structure and patterns

0 commit comments

Comments
 (0)