"""
MCP Server - Main Entry Point
A general-purpose MCP server using the official Python SDK.
Supports stdio transport (default) with HTTP/SSE planned for later.
Usage:
python -m mcp_server.server
"""
import logging
import sys
from typing import Optional
from mcp.server.fastmcp import FastMCP
from .config import ServerConfig
from .tools import register_all_tools
# Configure logging to stderr (CRITICAL for stdio transport - stdout is protocol data)
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
stream=sys.stderr
)
logger = logging.getLogger(__name__)
def create_server(name: Optional[str] = None, config: Optional[ServerConfig] = None) -> FastMCP:
"""
Create and configure the MCP server instance.
Args:
name: Server name shown to clients. If None, uses config or default.
config: Optional server configuration. If None, loads from environment.
Returns:
Configured FastMCP server instance
"""
config = config or ServerConfig.from_env()
server_name = name or config.server_name
# Set logging level from config
logging.getLogger().setLevel(getattr(logging, config.log_level.upper(), logging.INFO))
# Initialize FastMCP server
mcp = FastMCP(
name=server_name,
)
# Register all tools from the tools module
register_all_tools(mcp, config)
logger.info(f"MCP Server '{server_name}' initialized")
return mcp
def main():
"""
Main entry point - runs the server with stdio transport.
This is called when running:
python -m mcp_server.server
"""
server = create_server()
logger.info("Starting MCP server with stdio transport...")
server.run(transport="stdio")
if __name__ == "__main__":
main()