geqintan commited on
Commit
5c47533
·
1 Parent(s): d41fc87
Files changed (4) hide show
  1. .gitignore +1 -0
  2. app.py +2 -108
  3. apps/app01.py +38 -0
  4. apps/app02.py +38 -0
.gitignore CHANGED
@@ -0,0 +1 @@
 
 
1
+ **/__pycache__/
app.py CHANGED
@@ -1,110 +1,7 @@
1
- import uvicorn
2
  from fastapi import FastAPI
3
- from starlette.routing import Mount
4
- from starlette.requests import Request
5
- from starlette.responses import StreamingResponse
6
- import asyncio
7
- import json
8
-
9
- from mcp.server.fastmcp import FastMCP
10
- from mcp.server.sse import SseServerTransport
11
- from starlette.applications import Starlette
12
- from starlette.routing import Mount, Route
13
-
14
- def create_sse_server(mcp: FastMCP):
15
- """Create a Starlette app that handles SSE connections and message handling"""
16
- transport = SseServerTransport("/messages/")
17
-
18
- # Define handler functions
19
- async def handle_sse(request):
20
- async with transport.connect_sse(
21
- request.scope, request.receive, request._send
22
- ) as streams:
23
- await mcp._mcp_server.run(
24
- streams[0], streams[1], mcp._mcp_server.create_initialization_options()
25
- )
26
-
27
- # Create Starlette routes for SSE and message handling
28
- routes = [
29
- Route("/sse/", endpoint=handle_sse),
30
- Mount("/messages/", app=transport.handle_post_message),
31
- ]
32
-
33
- # Create a Starlette app
34
- return Starlette(routes=routes)
35
-
36
- # 创建 MCP 应用实例
37
- class App01:
38
- def __init__(self):
39
- self.mcp = FastMCP(name="app01", stateless_http=True)
40
- self.register_endpoints()
41
-
42
- def register_endpoints(self):
43
- @self.mcp.resource("app01://info")
44
- def app01_info() -> str:
45
- """Information about App01"""
46
- return "This is App01"
47
-
48
- @self.mcp.tool()
49
- def app01_echo(message: str) -> str:
50
- """Echo a message in App01"""
51
- return f"App01 Echo: {message}"
52
-
53
- @self.mcp.tool()
54
- def app01_add_integers(num1: int, num2: int) -> int:
55
- """Add two integers in App01"""
56
- return num1 + num2
57
-
58
- async def sse_endpoint(self, request: Request):
59
- print("Entering App01 SSE endpoint")
60
- try:
61
- # 这里可以实现 app01 的 SSE 逻辑
62
- yield {"event": "message", "data": "Hello from App01"}
63
- import asyncio
64
- await asyncio.sleep(1) # 示例:每秒发送一条消息
65
- yield {"event": "message", "data": "Another message from App01"}
66
- except Exception as e:
67
- print(f"Error in App01 SSE endpoint: {e}")
68
- # Optionally yield an error event to the client
69
- # yield {"event": "error", "data": str(e)}
70
- print("Exiting App01 SSE endpoint")
71
-
72
-
73
- class App02:
74
- def __init__(self):
75
- self.mcp = FastMCP(name="app02", stateless_http=True)
76
- self.register_endpoints()
77
-
78
- def register_endpoints(self):
79
- @self.mcp.resource("app02://status")
80
- def app02_status() -> str:
81
- """Status of App02"""
82
- return "App02 is running"
83
-
84
- @self.mcp.tool()
85
- def app02_process(data: str) -> str:
86
- """Process data in App02"""
87
- return f"App02 Processing: {data}"
88
-
89
- @self.mcp.tool()
90
- def app02_multiply_integers(num1: int, num2: int) -> int:
91
- """Multiply two integers in App02"""
92
- return num1 * num2
93
-
94
- async def sse_endpoint(self, request: Request):
95
- print("Entering App02 SSE endpoint")
96
- try:
97
- # 这里可以实现 app02 的 SSE 逻辑
98
- yield {"event": "message", "data": "Hello from App02"}
99
- import asyncio
100
- await asyncio.sleep(2) # 示例:每2秒发送一条消息
101
- yield {"event": "message", "data": "Another message from App02"}
102
- except Exception as e:
103
- print(f"Error in App02 SSE endpoint: {e}")
104
- # Optionally yield an error event to the client
105
- # yield {"event": "error", "data": str(e)}
106
- print("Exiting App02 SSE endpoint")
107
 
 
 
108
 
109
  # 初始化应用
110
  app01_instance = App01()
@@ -122,6 +19,3 @@ app.mount("/app02", app=app02_instance.mcp.sse_app())
122
  @app.get("/")
123
  async def read_root():
124
  return {"message": "FastAPI is running"}
125
-
126
- # if __name__ == "__main__":
127
- # uvicorn.run(app, host="0.0.0.0", port=7860)
 
 
1
  from fastapi import FastAPI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
+ from apps.app01 import App01
4
+ from apps.app02 import App02
5
 
6
  # 初始化应用
7
  app01_instance = App01()
 
19
  @app.get("/")
20
  async def read_root():
21
  return {"message": "FastAPI is running"}
 
 
 
apps/app01.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ from fastapi import Request
3
+ from mcp.server.fastmcp import FastMCP
4
+
5
+ # 创建 MCP 应用实例
6
+ class App01:
7
+ def __init__(self):
8
+ self.mcp = FastMCP(name="app01", stateless_http=True)
9
+ self.register_endpoints()
10
+
11
+ def register_endpoints(self):
12
+ @self.mcp.resource("app01://info")
13
+ def app01_info() -> str:
14
+ """Information about App01"""
15
+ return "This is App01"
16
+
17
+ @self.mcp.tool()
18
+ def app01_echo(message: str) -> str:
19
+ """Echo a message in App01"""
20
+ return f"App01 Echo: {message}"
21
+
22
+ @self.mcp.tool()
23
+ def app01_add_integers(num1: int, num2: int) -> int:
24
+ """Add two integers in App01"""
25
+ return num1 + num2
26
+
27
+ async def sse_endpoint(self, request: Request):
28
+ print("Entering App01 SSE endpoint")
29
+ try:
30
+ # 这里可以实现 app01 的 SSE 逻辑
31
+ yield {"event": "message", "data": "Hello from App01"}
32
+ await asyncio.sleep(1) # 示例:每秒发送一条消息
33
+ yield {"event": "message", "data": "Another message from App01"}
34
+ except Exception as e:
35
+ print(f"Error in App01 SSE endpoint: {e}")
36
+ # Optionally yield an error event to the client
37
+ # yield {"event": "error", "data": str(e)}
38
+ print("Exiting App01 SSE endpoint")
apps/app02.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ from fastapi import Request
3
+ from mcp.server.fastmcp import FastMCP
4
+
5
+ # 创建 MCP 应用实例
6
+ class App02:
7
+ def __init__(self):
8
+ self.mcp = FastMCP(name="app02", stateless_http=True)
9
+ self.register_endpoints()
10
+
11
+ def register_endpoints(self):
12
+ @self.mcp.resource("app02://status")
13
+ def app02_status() -> str:
14
+ """Status of App02"""
15
+ return "App02 is running"
16
+
17
+ @self.mcp.tool()
18
+ def app02_process(data: str) -> str:
19
+ """Process data in App02"""
20
+ return f"App02 Processing: {data}"
21
+
22
+ @self.mcp.tool()
23
+ def app02_multiply_integers(num1: int, num2: int) -> int:
24
+ """Multiply two integers in App02"""
25
+ return num1 * num2
26
+
27
+ async def sse_endpoint(self, request: Request):
28
+ print("Entering App02 SSE endpoint")
29
+ try:
30
+ # 这里可以实现 app02 的 SSE 逻辑
31
+ yield {"event": "message", "data": "Hello from App02"}
32
+ await asyncio.sleep(2) # 示例:每2秒发送一条消息
33
+ yield {"event": "message", "data": "Another message from App02"}
34
+ except Exception as e:
35
+ print(f"Error in App02 SSE endpoint: {e}")
36
+ # Optionally yield an error event to the client
37
+ # yield {"event": "error", "data": str(e)}
38
+ print("Exiting App02 SSE endpoint")