Trying to fix CORS
This commit is contained in:
parent
dd9e178e72
commit
15415080da
2 changed files with 66 additions and 29 deletions
10
README.md
10
README.md
|
|
@ -465,6 +465,16 @@ geoguessr-mcp/
|
||||||
└── Dockerfile
|
└── Dockerfile
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### MCP Inspector Tool Test
|
||||||
|
|
||||||
|
Tool to inspect MCP server and test connectivity:
|
||||||
|
Using the [Inspector](https://github.com/modelcontextprotocol/inspector) tool from the MCP project.
|
||||||
|
Via Docker:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run --rm -p 6274:6274 -p 6277:6277 -e HOST=0.0.0.0 ghcr.io/modelcontextprotocol/inspector:latest
|
||||||
|
```
|
||||||
|
|
||||||
## 🤝 Contributing
|
## 🤝 Contributing
|
||||||
|
|
||||||
Contributions are welcome! Please:
|
Contributions are welcome! Please:
|
||||||
|
|
|
||||||
|
|
@ -47,32 +47,7 @@ class RequestLoggingMiddleware(BaseHTTPMiddleware):
|
||||||
def main():
|
def main():
|
||||||
"""Main entry point for the server."""
|
"""Main entry point for the server."""
|
||||||
|
|
||||||
# Prepare middleware list
|
# Create the MCP server instance
|
||||||
from starlette.middleware import Middleware
|
|
||||||
|
|
||||||
middleware_list = []
|
|
||||||
|
|
||||||
# Add request logging middleware for debugging (first in chain)
|
|
||||||
if settings.LOG_LEVEL == "DEBUG":
|
|
||||||
middleware_list.append(Middleware(RequestLoggingMiddleware))
|
|
||||||
|
|
||||||
# Always add CORS middleware for browser compatibility
|
|
||||||
middleware_list.append(
|
|
||||||
Middleware(
|
|
||||||
CORSMiddleware,
|
|
||||||
allow_origins=["*"],
|
|
||||||
allow_credentials=True,
|
|
||||||
allow_methods=["*"],
|
|
||||||
allow_headers=["*"],
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Add authentication middleware if enabled
|
|
||||||
if settings.MCP_AUTH_ENABLED:
|
|
||||||
logger.info("Setting up authentication middleware")
|
|
||||||
middleware_list.append(Middleware(AuthenticationMiddleware))
|
|
||||||
|
|
||||||
# Create the MCP server instance with middleware
|
|
||||||
mcp = FastMCP(
|
mcp = FastMCP(
|
||||||
"GeoGuessr MCP",
|
"GeoGuessr MCP",
|
||||||
instructions="""
|
instructions="""
|
||||||
|
|
@ -106,6 +81,59 @@ def main():
|
||||||
# Register all tools
|
# Register all tools
|
||||||
register_all_tools(mcp)
|
register_all_tools(mcp)
|
||||||
|
|
||||||
|
# Wrap the streamable_http_app method to inject middleware
|
||||||
|
_original_streamable_http_app = mcp.streamable_http_app
|
||||||
|
|
||||||
|
def _streamable_http_app_with_middleware():
|
||||||
|
"""Wrap app creation to inject middleware."""
|
||||||
|
|
||||||
|
app = _original_streamable_http_app()
|
||||||
|
|
||||||
|
# Add request logging middleware for debugging (first in chain)
|
||||||
|
if settings.LOG_LEVEL == "DEBUG":
|
||||||
|
app.add_middleware(RequestLoggingMiddleware)
|
||||||
|
|
||||||
|
# Always add CORS middleware for browser compatibility
|
||||||
|
app.add_middleware(
|
||||||
|
CORSMiddleware,
|
||||||
|
allow_origins=["*"],
|
||||||
|
allow_credentials=True,
|
||||||
|
allow_methods=["*"],
|
||||||
|
allow_headers=["*"],
|
||||||
|
)
|
||||||
|
# Add authentication middleware if enabled
|
||||||
|
if settings.MCP_AUTH_ENABLED:
|
||||||
|
app.add_middleware(AuthenticationMiddleware)
|
||||||
|
|
||||||
|
return app
|
||||||
|
|
||||||
|
# Replace the method with our wrapper
|
||||||
|
mcp.streamable_http_app = _streamable_http_app_with_middleware
|
||||||
|
|
||||||
|
# Also wrap sse_app for SSE transport
|
||||||
|
if hasattr(mcp, "sse_app"):
|
||||||
|
_original_sse_app = mcp.sse_app
|
||||||
|
|
||||||
|
def _sse_app_with_middleware():
|
||||||
|
"""Wrap SSE app creation to inject middleware."""
|
||||||
|
app = _original_sse_app()
|
||||||
|
if settings.LOG_LEVEL == "DEBUG":
|
||||||
|
app.add_middleware(RequestLoggingMiddleware)
|
||||||
|
|
||||||
|
app.add_middleware(
|
||||||
|
CORSMiddleware,
|
||||||
|
allow_origins=["*"],
|
||||||
|
allow_credentials=True,
|
||||||
|
allow_methods=["*"],
|
||||||
|
allow_headers=["*"],
|
||||||
|
|
||||||
|
)
|
||||||
|
if settings.MCP_AUTH_ENABLED:
|
||||||
|
app.add_middleware(AuthenticationMiddleware)
|
||||||
|
return app
|
||||||
|
|
||||||
|
mcp.sse_app = _sse_app_with_middleware
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
f"Starting GeoGuessr MCP Server on {settings.HOST}:{settings.PORT} "
|
f"Starting GeoGuessr MCP Server on {settings.HOST}:{settings.PORT} "
|
||||||
f"with {settings.TRANSPORT} transport"
|
f"with {settings.TRANSPORT} transport"
|
||||||
|
|
@ -116,7 +144,6 @@ def main():
|
||||||
logger.info(f"MCP server authentication is ENABLED with {api_key_count} API key(s)")
|
logger.info(f"MCP server authentication is ENABLED with {api_key_count} API key(s)")
|
||||||
else:
|
else:
|
||||||
logger.warning("MCP server authentication is DISABLED - server is publicly accessible")
|
logger.warning("MCP server authentication is DISABLED - server is publicly accessible")
|
||||||
|
|
||||||
if settings.DEFAULT_NCFA_COOKIE:
|
if settings.DEFAULT_NCFA_COOKIE:
|
||||||
logger.info("Default GeoGuessr authentication cookie configured from environment")
|
logger.info("Default GeoGuessr authentication cookie configured from environment")
|
||||||
else:
|
else:
|
||||||
|
|
@ -125,8 +152,8 @@ def main():
|
||||||
"Users will need to login or provide a cookie."
|
"Users will need to login or provide a cookie."
|
||||||
)
|
)
|
||||||
|
|
||||||
# Run the server with middleware support
|
# Run the server - middleware will be applied via our wrapper
|
||||||
mcp.run(transport=settings.TRANSPORT, middleware=middleware_list)
|
mcp.run(transport=settings.TRANSPORT)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue