forked from microsoft/mcp-for-beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
53 lines (46 loc) · 1.9 KB
/
server.py
File metadata and controls
53 lines (46 loc) · 1.9 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
# server.py
from fastapi import FastAPI
from fastapi.responses import StreamingResponse, HTMLResponse
from mcp.server.fastmcp import FastMCP, Context
from mcp.types import (
TextContent
)
import asyncio
import uvicorn
import os
# Create an MCP server
mcp = FastMCP("Streamable DEMO")
app = FastAPI()
@app.get("/", response_class=HTMLResponse)
async def root():
html_path = os.path.join(os.path.dirname(__file__), "welcome.html")
with open(html_path, "r", encoding="utf-8") as f:
html_content = f.read()
return HTMLResponse(content=html_content)
async def event_stream(message: str):
for i in range(1, 4):
yield f"Processing file {i}/3...\n"
await asyncio.sleep(1)
yield f"Here's the file content: {message}\n"
@app.get("/stream")
async def stream(message: str = "hello"):
return StreamingResponse(event_stream(message), media_type="text/plain")
@mcp.tool(description="A tool that simulates file processing and sends progress notifications")
async def process_files(message: str, ctx: Context) -> TextContent:
files = [f"file_{i}.txt" for i in range(1, 4)]
for idx, file in enumerate(files, 1):
await ctx.info(f"Processing {file} ({idx}/{len(files)})...")
await asyncio.sleep(1)
await ctx.info("All files processed!")
return TextContent(type="text", text=f"Processed files: {', '.join(files)} | Message: {message}")
if __name__ == "__main__":
import sys
if "mcp" in sys.argv:
# Configure MCP server with streamable-http transport
print("Starting MCP server with streamable-http transport...")
# MCP server will create its own FastAPI app with the /mcp endpoint
mcp.run(transport="streamable-http")
else:
# Start FastAPI app for classic HTTP streaming
print("Starting FastAPI server for classic HTTP streaming...")
uvicorn.run("server:app", host="127.0.0.1", port=8000, reload=True)