Spaces:
Sleeping
Sleeping
Add MCP server
Browse files- Dockerfile +19 -0
- app.py +57 -0
- requirements.txt +3 -0
Dockerfile
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Base image
|
2 |
+
FROM python:3.10-slim
|
3 |
+
|
4 |
+
# Set working directory
|
5 |
+
WORKDIR /code
|
6 |
+
|
7 |
+
# Install FastAPI and Uvicorn
|
8 |
+
COPY requirements.txt .
|
9 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
10 |
+
|
11 |
+
# Copy app code
|
12 |
+
COPY app.py .
|
13 |
+
|
14 |
+
# Expose the port (Spaces default to 7860)
|
15 |
+
EXPOSE 7860
|
16 |
+
|
17 |
+
# Start the FastAPI app
|
18 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
19 |
+
|
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Request
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
+
from pydantic import BaseModel
|
4 |
+
from typing import Dict
|
5 |
+
|
6 |
+
app = FastAPI()
|
7 |
+
|
8 |
+
|
9 |
+
class MCPMessage(BaseModel):
|
10 |
+
session: str
|
11 |
+
id: str
|
12 |
+
command: str
|
13 |
+
args: Dict[str, str]
|
14 |
+
|
15 |
+
|
16 |
+
@app.post("/mcp")
|
17 |
+
async def mcp_endpoint(message: MCPMessage, request: Request):
|
18 |
+
print(f"[MCP Server] Received: {message.command} | Args: {message.args}")
|
19 |
+
|
20 |
+
if message.command == "mcp-negotiate":
|
21 |
+
return {
|
22 |
+
"id": message.id,
|
23 |
+
"status": "ok",
|
24 |
+
"message": "Negotiation successful",
|
25 |
+
"session": message.session,
|
26 |
+
"version": message.args.get("version", "1.0"),
|
27 |
+
}
|
28 |
+
|
29 |
+
elif message.command == "mcp-message":
|
30 |
+
return {
|
31 |
+
"id": message.id,
|
32 |
+
"status": "ok",
|
33 |
+
"message": f"Delivered to {message.args.get('to')}",
|
34 |
+
"echo": message.args.get("body", ""),
|
35 |
+
}
|
36 |
+
|
37 |
+
elif message.command == "mcp-echo-upper":
|
38 |
+
original = message.args.get("body", "")
|
39 |
+
return {
|
40 |
+
"id": message.id,
|
41 |
+
"status": "ok",
|
42 |
+
"message": "Uppercase echo",
|
43 |
+
"original": original,
|
44 |
+
"upper": original.upper()
|
45 |
+
}
|
46 |
+
|
47 |
+
return JSONResponse(status_code=400, content={
|
48 |
+
"id": message.id,
|
49 |
+
"status": "error",
|
50 |
+
"message": f"Unknown command: {message.command}"
|
51 |
+
})
|
52 |
+
|
53 |
+
|
54 |
+
@app.get("/")
|
55 |
+
def root():
|
56 |
+
return {"message": "MCP Test Server is running"}
|
57 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn
|
3 |
+
|