Skip to content

Commit d7b371a

Browse files
committed
fix refactor
1 parent d6e9e0f commit d7b371a

File tree

8 files changed

+30
-16
lines changed

8 files changed

+30
-16
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ You can now configure middleware for your MCP server using a dedicated `config.p
4040
### Example: config.py
4141

4242
```python
43-
from pymcp.server import MiddlewareConfig
43+
from pymcp.middleware import MiddlewareConfig
4444

4545
middleware_config = MiddlewareConfig(
4646
cors={
@@ -65,8 +65,8 @@ middleware_config = MiddlewareConfig(
6565
### Example: run_server.py
6666

6767
```python
68-
from example.config import middleware_config
69-
from pymcp.server import create_app
68+
from config import middleware_config
69+
from pymcp.applications import create_app
7070

7171
app = create_app(middleware_config=middleware_config)
7272
```

example/config.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111
"level": "DEBUG",
1212
"format": "%(asctime)s %(levelname)s %(message)s",
1313
},
14-
compression={
15-
"enabled": True
16-
},
14+
compression={"enabled": True},
1715
custom=[
1816
# Add custom middleware classes here if needed
19-
]
20-
)
17+
],
18+
)

example/run_server.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
logging.basicConfig(level=logging.DEBUG)
88

9+
910
@tool_registry.register
1011
def addNumbersTool(a: float, b: float) -> str:
1112
"""Adds two numbers 'a' and 'b' and returns their sum."""
@@ -41,5 +42,6 @@ def promptEchoTool(prompt: str) -> str:
4142

4243
if __name__ == "__main__":
4344
import uvicorn
45+
4446
app = create_app(middleware_config=middleware_config)
4547
uvicorn.run(app, host="0.0.0.0", port=8088)

guide.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ For best practice, create a `config.py` file in your project root to define your
133133
### Example: config.py
134134

135135
```python
136-
from pymcp.server import MiddlewareConfig
136+
137+
from pymcp.applications import create_app
138+
from pymcp.middleware import MiddlewareConfig
137139

138140
middleware_config = MiddlewareConfig(
139141
cors={

pymcp/applications.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
App factory for the MCP framework.
33
Provides the create_app function to instantiate and configure the FastAPI app.
44
"""
5+
56
from typing import Optional
67

78
from fastapi import FastAPI
@@ -21,4 +22,4 @@ def create_app(middleware_config: Optional[MiddlewareConfig] = None, **kwargs):
2122
)
2223
setup_middleware(app, config)
2324
app.include_router(router)
24-
return app
25+
return app

pymcp/middleware.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Middleware configuration and setup for the MCP framework.
33
"""
4-
import logging
4+
55
from typing import Any, Dict, Optional
66

77
from fastapi.middleware.cors import CORSMiddleware
@@ -12,6 +12,7 @@ class MiddlewareConfig:
1212
"""
1313
Configuration class for setting up middleware in the MCP server.
1414
"""
15+
1516
def __init__(
1617
self,
1718
cors: Optional[Dict[str, Any]] = None,
@@ -26,7 +27,10 @@ def __init__(
2627
"allow_methods": ["*"],
2728
"allow_headers": ["*"],
2829
}
29-
self.logging = logging or {"level": "INFO", "format": "%(asctime)s %(levelname)s %(message)s"}
30+
self.logging = logging or {
31+
"level": "INFO",
32+
"format": "%(asctime)s %(levelname)s %(message)s",
33+
}
3034
self.error_handling = error_handling or {}
3135
self.compression = compression or {"enabled": False}
3236
self.custom = custom or []
@@ -44,12 +48,13 @@ def setup_middleware(app, config: MiddlewareConfig):
4448
allow_methods=config.cors["allow_methods"],
4549
allow_headers=config.cors["allow_headers"],
4650
)
47-
# Logging
48-
logging.basicConfig(level=config.logging["level"], format=config.logging["format"])
51+
4952
# Compression
5053
if config.compression.get("enabled", False):
5154
app.add_middleware(GZipMiddleware)
5255
# Custom middleware
5356
for mw in config.custom:
57+
if not hasattr(mw, "__call__"):
58+
raise ValueError(f"Custom middleware {mw} is not callable")
5459
app.add_middleware(mw)
55-
# Error handling can be added here as needed
60+
# Error handling can be added here as needed

pymcp/server.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Main server logic for the MCP framework.
33
"""
4+
45
import asyncio
56
import json
67
from uuid import uuid4
@@ -13,15 +14,18 @@
1314

1415
router = APIRouter()
1516

17+
1618
def get_sessions(app):
1719
if not hasattr(app.state, "sessions"):
1820
app.state.sessions = {}
1921
return app.state.sessions
2022

23+
2124
@router.get("/")
2225
async def root():
2326
return {"status": "ok"}
2427

28+
2529
@router.get("/sse-cursor")
2630
async def sse_cursor(request: Request):
2731
app = request.app
@@ -46,6 +50,7 @@ async def event_stream():
4650

4751
return StreamingResponse(event_stream(), media_type="text/event-stream")
4852

53+
4954
@router.post("/message")
5055
async def message(request: Request):
5156
app = request.app

pymcp/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Utility functions for the MCP framework.
33
"""
4+
45
import json
56
import logging
67

@@ -80,4 +81,4 @@ async def handle_rpc_method(method, data, session_id, rpc_id, sessions):
8081
"id": rpc_id,
8182
"error": {"code": -32601, "message": f"Method '{method}' not recognized"},
8283
}
83-
await queue.put(json.dumps(error))
84+
await queue.put(json.dumps(error))

0 commit comments

Comments
 (0)