Skip to content

Commit 49b1fc5

Browse files
authored
Merge pull request #51 from tadata-org/fix/bugs-after-refactor
Fix/bugs after refactor
2 parents ca7b1f4 + c095eb9 commit 49b1fc5

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

examples/filtered_tools_example.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
items.app,
1313
name="Item API MCP - Included Operations",
1414
description="MCP server showing only specific operations",
15-
base_url="http://localhost:8001",
15+
base_url="http://localhost:8000",
1616
include_operations=["get_item", "list_items"],
1717
)
1818

@@ -21,7 +21,7 @@
2121
items.app,
2222
name="Item API MCP - Excluded Operations",
2323
description="MCP server showing all operations except the excluded ones",
24-
base_url="http://localhost:8002",
24+
base_url="http://localhost:8000",
2525
exclude_operations=["create_item", "update_item", "delete_item"],
2626
)
2727

@@ -30,7 +30,7 @@
3030
items.app,
3131
name="Item API MCP - Included Tags",
3232
description="MCP server showing operations with specific tags",
33-
base_url="http://localhost:8003",
33+
base_url="http://localhost:8000",
3434
include_tags=["items"],
3535
)
3636

@@ -39,7 +39,7 @@
3939
items.app,
4040
name="Item API MCP - Excluded Tags",
4141
description="MCP server showing operations except those with specific tags",
42-
base_url="http://localhost:8004",
42+
base_url="http://localhost:8000",
4343
exclude_tags=["search"],
4444
)
4545

@@ -48,7 +48,7 @@
4848
items.app,
4949
name="Item API MCP - Combined Include",
5050
description="MCP server showing operations by combining include filters",
51-
base_url="http://localhost:8005",
51+
base_url="http://localhost:8000",
5252
include_operations=["delete_item"],
5353
include_tags=["search"],
5454
)

examples/shared/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
class LoggingConfig(BaseModel):
77
LOGGER_NAME: str = "fastapi_mcp"
88
LOG_FORMAT: str = "%(levelprefix)s %(asctime)s\t[%(name)s] %(message)s"
9-
LOG_LEVEL: str = logging.getLevelName(logging.INFO)
9+
LOG_LEVEL: str = logging.getLevelName(logging.DEBUG)
1010

1111
version: int = 1
1212
disable_existing_loggers: bool = False

fastapi_mcp/server.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,20 @@ def mount(self, router: Optional[FastAPI | APIRouter] = None, mount_path: str =
159159
if not router:
160160
router = self.fastapi
161161

162-
# Create SSE transport for MCP messages
163-
sse_transport = FastApiSseTransport(f"{mount_path}/messages/")
162+
# Build the base path correctly for the SSE transport
163+
if isinstance(router, FastAPI):
164+
base_path = router.root_path
165+
elif isinstance(router, APIRouter):
166+
base_path = self.fastapi.root_path + router.prefix
167+
else:
168+
raise ValueError(f"Invalid router type: {type(router)}")
169+
170+
messages_path = f"{base_path}{mount_path}/messages/"
171+
172+
sse_transport = FastApiSseTransport(messages_path)
164173

165174
# Route for MCP connection
166-
@router.get(mount_path, include_in_schema=False)
175+
@router.get(mount_path, include_in_schema=False, operation_id="mcp_connection")
167176
async def handle_mcp_connection(request: Request):
168177
async with sse_transport.connect_sse(request.scope, request.receive, request._send) as (reader, writer):
169178
await self.server.run(
@@ -173,10 +182,18 @@ async def handle_mcp_connection(request: Request):
173182
)
174183

175184
# Route for MCP messages
176-
@router.post(f"{mount_path}/messages/", include_in_schema=False)
185+
@router.post(f"{mount_path}/messages/", include_in_schema=False, operation_id="mcp_messages")
177186
async def handle_post_message(request: Request):
178187
return await sse_transport.handle_fastapi_post_message(request)
179188

189+
# HACK: If we got a router and not a FastAPI instance, we need to re-include the router so that
190+
# FastAPI will pick up the new routes we added. The problem with this approach is that we assume
191+
# that the router is a sub-router of self.fastapi, which may not always be the case.
192+
#
193+
# TODO: Find a better way to do this.
194+
if isinstance(router, APIRouter):
195+
self.fastapi.include_router(router)
196+
180197
logger.info(f"MCP server listening at {mount_path}")
181198

182199
async def _execute_api_tool(

0 commit comments

Comments
 (0)