import os
import uvicorn
from mcp.server.fastmcp import FastMCP
from starlette.middleware.cors import CORSMiddleware
from typing import Optional
# Import server using absolute imports from current directory
import sys
sys.path.append(os.path.dirname(__file__))
from server import mcp # Import the configured MCP server
def get_request_config() -> dict:
"""Get full config from current request context."""
try:
# Access the current request context from FastMCP
import contextvars
# Try to get from request context if available
request = contextvars.copy_context().get('request')
if hasattr(request, 'scope') and request.scope:
return request.scope.get('smithery_config', {})
except:
pass
return {}
def get_config_value(key: str, default=None):
"""Get a specific config value from current request."""
config = get_request_config()
return config.get(key, default)
def handle_config(config: dict):
"""Handle configuration from Smithery - for backwards compatibility with stdio mode."""
# Store config globally for stdio mode if needed
# Your server doesn't require configuration, so this is minimal
pass
def main():
transport_mode = os.getenv("TRANSPORT", "stdio")
if transport_mode == "http":
# HTTP mode with config extraction from URL parameters
print("PowerShell MCP Server starting in HTTP mode...")
# Setup Starlette app with CORS for cross-origin requests
app = mcp.streamable_http_app()
# IMPORTANT: add CORS middleware for browser based clients
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["GET", "POST", "OPTIONS"],
allow_headers=["*"],
expose_headers=["mcp-session-id", "mcp-protocol-version"],
max_age=86400,
)
# Use Smithery-required PORT environment variable
port = int(os.environ.get("PORT", 8081))
print(f"Listening on port {port}")
uvicorn.run(app, host="0.0.0.0", port=port, log_level="debug")
else:
# Optional: add stdio transport for backwards compatibility
# You can publish this to uv for users to run locally
print("PowerShell MCP Server starting in stdio mode...")
# Run with stdio transport (default)
mcp.run()
if __name__ == "__main__":
main()