Skip to content

How to call the fastapi api in the sub-server from client side? #198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
nightowl54321 opened this issue Apr 18, 2025 · 0 comments
Open

Comments

@nightowl54321
Copy link

from fastapi import FastAPI
from fastmcp import FastMCP

fastapi_app = FastAPI(title="sub fast api")

@fastapi_app.post("/items")
def create_item(name: str, price: float):
return {"id": 1, "name": name, "price": price}

Generate an sub MCP server directly from the FastAPI app

sub_mcp_server = FastMCP.from_fastapi(fastapi_app)

Generate an main MCP server

main_mcp_server = FastMCP(name="MAINSERVER")

main_mcp_server .mount("sub", sub_mcp_server )

if name == "main":
import asyncio
asyncio.run(main_mcp_server.run_sse_async(host="localhost", port=8080, log_level="debug"))

=========client========

client = Client(SSETransport(url="http://localhost:8001/sse"))

how to call the fastapi api in the sub-server?

async def call_auth_test(client: Client) -> list:
response = await client.call_tool(
"items" # what is the correct api name? list_tools does not show this fastapi api.
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant