fastapi_mcp icon indicating copy to clipboard operation
fastapi_mcp copied to clipboard

[BUG] return so many \n

Open NestZhou opened this issue 8 months ago • 3 comments

I created a simple MCP server that returns JSON data. When I use the MCP client to retrieve data, I get output like ['{\n "msg": "hello jax"\n}']. Why are there so many \n characters?

NestZhou avatar Apr 24 '25 08:04 NestZhou

Can you share a code snippet that produces this?

shahar4499 avatar Apr 24 '25 09:04 shahar4499

Can you share a code snippet that produces this?

from fastapi import FastAPI
from fastapi_mcp import FastApiMCP
import uvicorn


app = FastAPI(title="MCP Test API")


@app.get("/say_hello/", tags=["mcp"], operation_id="say_hello")
async def say_hello(name: str):
    return {"msg": f"hello {name}"}


mcp = FastApiMCP(
    app,
    include_tags=["mcp"],
)
mcp.mount()

if __name__ == "__main__":
    uvicorn.run("test_mcp:app", host="0.0.0.0", port=8001)

NestZhou avatar Apr 24 '25 09:04 NestZhou

when I use fastmcp, no problem return ['{"msg": "hello jax"}']

import uvicorn
from fastapi import FastAPI
from mcp.server.fastmcp import FastMCP
from starlette.routing import Mount
from starlette.applications import Starlette

api = FastAPI(title="MCP Test API")
mcp = FastMCP()


@mcp.tool()
@api.get("/say_hello")
async def say_hello(name: str):
    return {"msg": f"hello {name}"}


app = Starlette(routes=[Mount("/api", app=api), Mount("/", app=mcp.sse_app())])

if __name__ == "__main__":
    uvicorn.run("test_mcp2:app", host="0.0.0.0", port=8002)

NestZhou avatar Apr 24 '25 09:04 NestZhou