mseep-fastapi-mcp-sse 0.1.1__py3-none-any.whl → 0.1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
app.py ADDED
@@ -0,0 +1,57 @@
1
+ from fastapi import FastAPI, Request
2
+ from mcp.server.sse import SseServerTransport
3
+ from starlette.routing import Mount
4
+ from weather import mcp
5
+
6
+ # Create FastAPI application with metadata
7
+ app = FastAPI(
8
+ title="FastAPI MCP SSE",
9
+ description="A demonstration of Server-Sent Events with Model Context "
10
+ "Protocol integration",
11
+ version="0.1.0",
12
+ )
13
+
14
+ # Create SSE transport instance for handling server-sent events
15
+ sse = SseServerTransport("/messages/")
16
+
17
+ # Mount the /messages path to handle SSE message posting
18
+ app.router.routes.append(Mount("/messages", app=sse.handle_post_message))
19
+
20
+
21
+ # Add documentation for the /messages endpoint
22
+ @app.get("/messages", tags=["MCP"], include_in_schema=True)
23
+ def messages_docs():
24
+ """
25
+ Messages endpoint for SSE communication
26
+
27
+ This endpoint is used for posting messages to SSE clients.
28
+ Note: This route is for documentation purposes only.
29
+ The actual implementation is handled by the SSE transport.
30
+ """
31
+ pass # This is just for documentation, the actual handler is mounted above
32
+
33
+
34
+ @app.get("/sse", tags=["MCP"])
35
+ async def handle_sse(request: Request):
36
+ """
37
+ SSE endpoint that connects to the MCP server
38
+
39
+ This endpoint establishes a Server-Sent Events connection with the client
40
+ and forwards communication to the Model Context Protocol server.
41
+ """
42
+ # Use sse.connect_sse to establish an SSE connection with the MCP server
43
+ async with sse.connect_sse(request.scope, request.receive, request._send) as (
44
+ read_stream,
45
+ write_stream,
46
+ ):
47
+ # Run the MCP server with the established streams
48
+ await mcp._mcp_server.run(
49
+ read_stream,
50
+ write_stream,
51
+ mcp._mcp_server.create_initialization_options(),
52
+ )
53
+
54
+
55
+ # Import routes at the end to avoid circular imports
56
+ # This ensures all routes are registered to the app
57
+ import routes # noqa
@@ -0,0 +1,15 @@
1
+ Metadata-Version: 2.4
2
+ Name: mseep-fastapi-mcp-sse
3
+ Version: 0.1.3
4
+ Summary: A working example to create a FastAPI server with SSE-based MCP support
5
+ Author-email: mseep <support@skydeck.ai>
6
+ Requires-Python: >=3.13
7
+ Description-Content-Type: text/plain
8
+ License-File: LICENSE
9
+ Requires-Dist: fastapi>=0.115.11
10
+ Requires-Dist: httpx>=0.28.1
11
+ Requires-Dist: mcp[cli]>=1.3.0
12
+ Requires-Dist: unicorn>=2.1.3
13
+ Dynamic: license-file
14
+
15
+ Package managed by MseeP.ai
@@ -0,0 +1,10 @@
1
+ app.py,sha256=fMeFF-d12bqR74Tk3kIXkUgGeXK8ArLDSNaIB6qtObg,1888
2
+ routes.py,sha256=TCpBJgXnsRjLoj9KVx54mMJ-fyDpv46jlt8SIUS1k98,1119
3
+ server.py,sha256=qIYfgFLHMId2vw0j0tnpOEFncZABuNV2-WjCZAUQFh0,318
4
+ weather.py,sha256=L7ObmiVZO0jmlUmsCAnA7cjFt-gzJe7w9NibSiNyzHI,2942
5
+ mseep_fastapi_mcp_sse-0.1.3.dist-info/licenses/LICENSE,sha256=3jx_ovGaWR_Y9wL1s0njbZj7IyL5klS_5VvxO9EGbiE,1065
6
+ mseep_fastapi_mcp_sse-0.1.3.dist-info/METADATA,sha256=spERKmuqErrAtlLFisfO-GYqErW_qdlFoGRqPn3NnMc,444
7
+ mseep_fastapi_mcp_sse-0.1.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
8
+ mseep_fastapi_mcp_sse-0.1.3.dist-info/entry_points.txt,sha256=lWiP8hdv1rciwJn9kTxOBAVugY9fgMmwBgBVvp79EVg,37
9
+ mseep_fastapi_mcp_sse-0.1.3.dist-info/top_level.txt,sha256=xpnmJRuceitZrR4gZafC8URF4ChnCe3hSmHapNAlox8,26
10
+ mseep_fastapi_mcp_sse-0.1.3.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (80.7.1)
2
+ Generator: setuptools (80.9.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -0,0 +1,4 @@
1
+ app
2
+ routes
3
+ server
4
+ weather
routes.py ADDED
@@ -0,0 +1,40 @@
1
+ from fastapi.responses import HTMLResponse, JSONResponse, PlainTextResponse
2
+ from fastapi import APIRouter
3
+ from app import app
4
+
5
+ # Create a router with a general tag for API documentation organization
6
+ router = APIRouter(tags=["General"])
7
+
8
+
9
+ @router.get("/")
10
+ async def homepage():
11
+ """Root endpoint that returns a simple HTML welcome page"""
12
+ html_content = (
13
+ "<h1>FastAPI MCP SSE</h1>"
14
+ "<p>Welcome to the SSE demo with MCP integration.</p>"
15
+ )
16
+ return HTMLResponse(html_content)
17
+
18
+
19
+ @router.get("/about")
20
+ async def about():
21
+ """About endpoint that returns information about the application"""
22
+ return PlainTextResponse(
23
+ "About FastAPI MCP SSE: A demonstration of Server-Sent Events "
24
+ "with Model Context Protocol integration."
25
+ )
26
+
27
+
28
+ @router.get("/status")
29
+ async def status():
30
+ """Status endpoint that returns the current server status"""
31
+ status_info = {
32
+ "status": "running",
33
+ "server": "FastAPI MCP SSE",
34
+ "version": "0.1.0",
35
+ }
36
+ return JSONResponse(status_info)
37
+
38
+
39
+ # Include the router in the main application
40
+ app.include_router(router)
server.py ADDED
@@ -0,0 +1,16 @@
1
+ import uvicorn
2
+ import os
3
+ from app import app
4
+
5
+ # Environment variable configuration
6
+ HOST = os.getenv("HOST", "0.0.0.0")
7
+ PORT = int(os.getenv("PORT", "8000"))
8
+
9
+
10
+ def run():
11
+ """Start the FastAPI server with uvicorn"""
12
+ uvicorn.run(app, host=HOST, port=PORT, log_level="info")
13
+
14
+
15
+ if __name__ == "__main__":
16
+ run()
weather.py ADDED
@@ -0,0 +1,96 @@
1
+ from typing import Any
2
+ import httpx
3
+ from mcp.server.fastmcp import FastMCP
4
+
5
+ # Initialize FastMCP server
6
+ mcp = FastMCP("weather")
7
+
8
+ # Constants
9
+ NWS_API_BASE = "https://api.weather.gov"
10
+ USER_AGENT = "weather-app/1.0"
11
+
12
+
13
+ async def make_nws_request(url: str) -> dict[str, Any] | None:
14
+ """Make a request to the NWS API with proper error handling."""
15
+ headers = {"User-Agent": USER_AGENT, "Accept": "application/geo+json"}
16
+ async with httpx.AsyncClient() as client:
17
+ try:
18
+ response = await client.get(url, headers=headers, timeout=30.0)
19
+ response.raise_for_status()
20
+ return response.json()
21
+ except Exception:
22
+ return None
23
+
24
+
25
+ def format_alert(feature: dict) -> str:
26
+ """Format an alert feature into a readable string."""
27
+ props = feature["properties"]
28
+ return f"""
29
+ Event: {props.get('event', 'Unknown')}
30
+ Area: {props.get('areaDesc', 'Unknown')}
31
+ Severity: {props.get('severity', 'Unknown')}
32
+ Description: {props.get('description', 'No description available')}
33
+ Instructions: {props.get('instruction', 'No specific instructions provided')}
34
+ """
35
+
36
+
37
+ @mcp.tool()
38
+ async def get_alerts(state: str) -> str:
39
+ """Get weather alerts for a US state.
40
+
41
+ Args:
42
+ state: Two-letter US state code (e.g. CA, NY)
43
+ """
44
+ url = f"{NWS_API_BASE}/alerts/active/area/{state}"
45
+ data = await make_nws_request(url)
46
+
47
+ if not data or "features" not in data:
48
+ return "Unable to fetch alerts or no alerts found."
49
+
50
+ if not data["features"]:
51
+ return "No active alerts for this state."
52
+
53
+ alerts = [format_alert(feature) for feature in data["features"]]
54
+ return "\n---\n".join(alerts)
55
+
56
+
57
+ @mcp.tool()
58
+ async def get_forecast(latitude: float, longitude: float) -> str:
59
+ """Get weather forecast for a location.
60
+
61
+ Args:
62
+ latitude: Latitude of the location
63
+ longitude: Longitude of the location
64
+ """
65
+ # First get the forecast grid endpoint
66
+ points_url = f"{NWS_API_BASE}/points/{latitude},{longitude}"
67
+ points_data = await make_nws_request(points_url)
68
+
69
+ if not points_data:
70
+ return "Unable to fetch forecast data for this location."
71
+
72
+ # Get the forecast URL from the points response
73
+ forecast_url = points_data["properties"]["forecast"]
74
+ forecast_data = await make_nws_request(forecast_url)
75
+
76
+ if not forecast_data:
77
+ return "Unable to fetch detailed forecast."
78
+
79
+ # Format the periods into a readable forecast
80
+ periods = forecast_data["properties"]["periods"]
81
+ forecasts = []
82
+ for period in periods[:5]: # Only show next 5 periods
83
+ forecast = f"""
84
+ {period['name']}:
85
+ Temperature: {period['temperature']}°{period['temperatureUnit']}
86
+ Wind: {period['windSpeed']} {period['windDirection']}
87
+ Forecast: {period['detailedForecast']}
88
+ """
89
+ forecasts.append(forecast)
90
+
91
+ return "\n---\n".join(forecasts)
92
+
93
+
94
+ if __name__ == "__main__":
95
+ # Initialize and run the server
96
+ mcp.run(transport="sse")
@@ -1,200 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: mseep-fastapi-mcp-sse
3
- Version: 0.1.1
4
- Summary: A working example to create a FastAPI server with SSE-based MCP support
5
- Home-page:
6
- Author: mseep
7
- Author-email: mseep <support@skydeck.ai>
8
- Maintainer-email: mseep <support@skydeck.ai>
9
- Requires-Python: >=3.6
10
- Description-Content-Type: text/markdown
11
- License-File: LICENSE
12
- Requires-Dist: fastapi>=0.115.11
13
- Requires-Dist: httpx>=0.28.1
14
- Requires-Dist: mcp[cli]>=1.3.0
15
- Requires-Dist: unicorn>=2.1.3
16
- Dynamic: author
17
- Dynamic: license-file
18
- Dynamic: requires-python
19
-
20
- # FastAPI MCP SSE
21
-
22
- <p align="center">
23
- <strong>English</strong> | <a href="/README.zh-CN.md">简体中文</a>
24
- </p>
25
-
26
- A Server-Sent Events (SSE) implementation using FastAPI framework with [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) integration.
27
-
28
- ## What is MCP?
29
-
30
- The [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) is an open standard that enables AI models to interact with external tools and data sources. MCP solves several key challenges in AI development:
31
-
32
- - **Context limitations**: Allows models to access up-to-date information beyond their training data
33
- - **Tool integration**: Provides a standardized way for models to use external tools and APIs
34
- - **Interoperability**: Creates a common interface between different AI models and tools
35
- - **Extensibility**: Makes it easy to add new capabilities to AI systems without retraining
36
-
37
- This project demonstrates how to implement MCP using Server-Sent Events (SSE) in a FastAPI web application.
38
-
39
- ## Description
40
-
41
- This project demonstrates how to implement Server-Sent Events (SSE) using the FastAPI framework while integrating Model Context Protocol (MCP) functionality. The key feature is the seamless integration of MCP's SSE capabilities within a full-featured FastAPI web application that includes custom routes.
42
-
43
- ## Features
44
-
45
- - Server-Sent Events (SSE) implementation with MCP
46
- - FastAPI framework integration with custom routes
47
- - Unified web application with both MCP and standard web endpoints
48
- - Customizable route structure
49
- - Clean separation of concerns between MCP and web functionality
50
-
51
- ## Architecture
52
-
53
- This project showcases a modular architecture that:
54
-
55
- 1. Integrates MCP SSE endpoints (`/sse` and `/messages/`) into a FastAPI application
56
- 2. Provides standard web routes (`/`, `/about`, `/status`, `/docs`, `/redoc`)
57
- 3. Demonstrates how to maintain separation between MCP functionality and web routes
58
-
59
- ## Installation & Usage Options
60
-
61
- ### Prerequisites
62
-
63
- Install [UV Package Manager](https://docs.astral.sh/uv/) - A fast Python package installer written in Rust:
64
-
65
- ```cmd
66
- powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
67
- ```
68
-
69
- ### Option 1: Quick Run Without Installation
70
-
71
- Run the application directly without cloning the repository using UV's execution tool:
72
-
73
- ```cmd
74
- uvx --from git+https://github.com/panz2018/fastapi_mcp_sse.git start
75
- ```
76
-
77
- ### Option 2: Full Installation
78
-
79
- #### Create Virtual Environment
80
-
81
- Create an isolated Python environment for the project:
82
-
83
- ```cmd
84
- uv venv
85
- ```
86
-
87
- #### Activate Virtual Environment
88
-
89
- Activate the virtual environment to use it:
90
-
91
- ```cmd
92
- .venv\Scripts\activate
93
- ```
94
-
95
- #### Install Dependencies
96
-
97
- Install all required packages:
98
-
99
- ```cmd
100
- uv pip install -r pyproject.toml
101
- ```
102
-
103
- #### Start the Integrated Server
104
-
105
- Launch the integrated FastAPI server with MCP SSE functionality:
106
-
107
- ```cmd
108
- python src/server.py
109
- ```
110
-
111
- or
112
-
113
- ```cmd
114
- uv run start
115
- ```
116
-
117
- ### Available Endpoints
118
-
119
- After starting the server (using either Option 1 or Option 2), the following endpoints will be available:
120
-
121
- - Main server: http://localhost:8000
122
- - Standard web routes:
123
- - Home page: http://localhost:8000/
124
- - About page: http://localhost:8000/about
125
- - Status API: http://localhost:8000/status
126
- - Documentation (Swagger UI): http://localhost:8000/docs
127
- - Documentation (ReDoc): http://localhost:8000/redoc
128
- - MCP SSE endpoints:
129
- - SSE endpoint: http://localhost:8000/sse
130
- - Message posting: http://localhost:8000/messages/
131
-
132
- ### Debug with MCP Inspector
133
-
134
- For testing and debugging MCP functionality, use the MCP Inspector:
135
-
136
- ```cmd
137
- mcp dev ./src/weather.py
138
- ```
139
-
140
- ### Connect to MCP Inspector
141
-
142
- 1. Open MCP Inspector at http://localhost:5173
143
- 2. Configure the connection:
144
- - Set Transport Type to `SSE`
145
- - Enter URL: http://localhost:8000/sse
146
- - Click `Connect`
147
-
148
- ### Test the Functions
149
-
150
- 1. Navigate to `Tools` section
151
- 2. Click `List Tools` to see available functions:
152
- - `get_alerts` : Get weather alerts
153
- - `get_forcast` : Get weather forecast
154
- 3. Select a function
155
- 4. Enter required parameters
156
- 5. Click `Run Tool` to execute
157
-
158
- ## Extending the Application
159
-
160
- ### Adding Custom Routes
161
-
162
- The application structure makes it easy to add new routes using FastAPI's APIRouter:
163
-
164
- 1. Define new route handlers in routes.py using the APIRouter:
165
-
166
- ```python
167
- @router.get("/new-route")
168
- async def new_route():
169
- return {"message": "This is a new route"}
170
- ```
171
-
172
- 2. All routes defined with the router will be automatically included in the main application
173
-
174
- ### Customizing MCP Integration
175
-
176
- The MCP SSE functionality is integrated in server.py through:
177
-
178
- - Creating an SSE transport
179
- - Setting up an SSE handler
180
- - Adding MCP routes to the FastAPI application
181
-
182
- ## Integration with [Continue](https://www.continue.dev/)
183
-
184
- To use this MCP server with the Continue VS Code extension, add the following configuration to your Continue settings:
185
-
186
- ```json
187
- {
188
- "experimental": {
189
- "modelContextProtocolServers": [
190
- {
191
- "transport": {
192
- "name": "weather",
193
- "type": "sse",
194
- "url": "http://localhost:8000/sse"
195
- }
196
- }
197
- ]
198
- }
199
- }
200
- ```
@@ -1,6 +0,0 @@
1
- mseep_fastapi_mcp_sse-0.1.1.dist-info/licenses/LICENSE,sha256=3jx_ovGaWR_Y9wL1s0njbZj7IyL5klS_5VvxO9EGbiE,1065
2
- mseep_fastapi_mcp_sse-0.1.1.dist-info/METADATA,sha256=mHcfIa3My1iy44B3bgrCnOTkmM_eDtaAVMOel3pu86A,5622
3
- mseep_fastapi_mcp_sse-0.1.1.dist-info/WHEEL,sha256=Nw36Djuh_5VDukK0H78QzOX-_FQEo6V37m3nkm96gtU,91
4
- mseep_fastapi_mcp_sse-0.1.1.dist-info/entry_points.txt,sha256=lWiP8hdv1rciwJn9kTxOBAVugY9fgMmwBgBVvp79EVg,37
5
- mseep_fastapi_mcp_sse-0.1.1.dist-info/top_level.txt,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
6
- mseep_fastapi_mcp_sse-0.1.1.dist-info/RECORD,,