Skip to content

Commit 443a35a

Browse files
committed
fixup! 💩(mcp) add a local MCP server configuration
1 parent b7ea79c commit 443a35a

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

src/mcp_server/docs_mcp_server/mcp_server.py

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
"""The core of the MCP server for the Docs API."""
22

33
import asyncio
4+
from contextlib import asynccontextmanager
45

56
import httpx
67
from fastmcp import FastMCP
8+
from starlette.requests import Request
9+
from starlette.responses import PlainTextResponse
710

811
from . import settings, utils
912
from .auth.forwarder import HeaderForwarderAuthentication
1013
from .auth.token import UserTokenAuthentication
1114

12-
# Create a server instance from the OpenAPI spec
13-
mcp = FastMCP(name="Docs MCP Server")
15+
# Global variable for the API client
16+
api_client = None
1417

1518

16-
def setup_mcp_server():
17-
"""Configure the tools and resources for the MCP server."""
18-
print("#" * 70)
19+
@asynccontextmanager
20+
async def lifespan(app: FastMCP):
21+
"""
22+
Manage the lifecycle of resources for the MCP server.
23+
24+
This context manager initializes resources on startup and
25+
ensures proper cleanup on shutdown.
26+
"""
27+
global api_client
28+
29+
# Initialize API client on startup
1930
if settings.DOCS_API_TOKEN:
2031
print("Setting up Docs MCP Server with user token authentication")
2132
api_client = httpx.AsyncClient(
@@ -24,8 +35,23 @@ def setup_mcp_server():
2435
)
2536
else:
2637
print("Setting up Docs MCP Server with header forwarder authentication")
27-
api_client = httpx.AsyncClient(base_url=settings.DOCS_API_URL, auth=HeaderForwarderAuthentication())
28-
print("#" * 70)
38+
api_client = httpx.AsyncClient(
39+
base_url=settings.DOCS_API_URL, auth=HeaderForwarderAuthentication()
40+
)
41+
42+
print("API client initialized")
43+
yield
44+
# Cleanup resources on shutdown
45+
print("Closing API client")
46+
await api_client.aclose()
47+
48+
49+
# Create a server instance from the OpenAPI spec with lifespan
50+
mcp = FastMCP(name="Docs MCP Server", lifespan=lifespan)
51+
52+
53+
def setup_mcp_server():
54+
"""Configure the tools and resources for the MCP server."""
2955

3056
@mcp.tool()
3157
async def create_document_tool(document_title: str, document_content: str) -> None:
@@ -37,6 +63,9 @@ async def create_document_tool(document_title: str, document_content: str) -> No
3763
document_content: The content of the document (required)
3864
3965
"""
66+
if api_client is None:
67+
raise RuntimeError("API client is not initialized")
68+
4069
# Get current user information
4170
user_response = await api_client.get("/api/v1.0/users/me/")
4271
user_response.raise_for_status()
@@ -57,6 +86,11 @@ async def create_document_tool(document_title: str, document_content: str) -> No
5786
)
5887
create_response.raise_for_status()
5988

89+
# Add health check endpoint
90+
@mcp.custom_route("/health", methods=["GET"])
91+
async def health_check(request: Request) -> PlainTextResponse:
92+
return PlainTextResponse("OK")
93+
6094

6195
if __name__ == "__main__":
6296
setup_mcp_server()

0 commit comments

Comments
 (0)