fastmcp icon indicating copy to clipboard operation
fastmcp copied to clipboard

Calling REST API from MCP tool not working

Open magallardo opened this issue 7 months ago • 8 comments

Description

Hello,

Is there something required to be set before calling a REST API from an MCP Tool? When calling the REST API, seeing some weird errors and the call is not completed.
I have been using the same REST API without any issues but not sure why it is failing when calling from MCP Tools.

Thanks, Marcelo

Example Code

from fastmcp import FastMCP

import httpx

mcp = FastMCP(
    "myserver",
    description="My MCP server",
    host=os.getenv("HOST", "0.0.0.0"),
    port=os.getenv("PORT", "8060"),
)

@mcp.tool()
def list_all_datasources_tool() -> str:
"""
Tool description
"""

   url = "xxxxx"
   data = {
      field1="xxx",
      field2="xxx"
   }
   username = "xxx"
   password = "xxx"
   headers = {
    "Accept": "application/json",
   }

   with httpx.Client() as client:
      try:
         response = client.post(
            url,
            auth=(username, password),
            json=data,
            headers=headers,
            timeout=30.0,
        )
        except httpx.RequestError as exc:
           return ""

    if response.status_code == 200:

        result = response.json()
    else:
        result = ""


if __name__ == "__main__":
    mcp.run(transport="sse")

Version Information

fastmcp>=2.3.0

Additional Context

Please note that I have tried with async tools also and get the same issues when trying to call REST api from the tool.

magallardo avatar May 09 '25 15:05 magallardo

I don't have a way to debug this without more information. Is there an error code? Is it timing out?

jlowin avatar May 09 '25 19:05 jlowin

@jlowin Thanks for looking into this. It is very inconsistent. First it was complaining of invalid character in the URL "/%22http.....".
Now I am getting 401 response with invalid credentials.

Status code: 401 Response text:

TIBCO Data Virtualization
Error: 401 Unauthorized
Description: Invalid login attempt. [security-1900202]

Please note that the same code to interact with the REST API works fine when using non-MCP.

Marcelo

magallardo avatar May 09 '25 20:05 magallardo

It sounds like something is modifying your dictionary with the url and credentials - how else could the url have an invalid character inside the MCP server but work outside it? Is it possible you are passing that information through a different server into the MCP (in which case they would be URL-quoted, which would explain why you see a %22 in the URL)? Maybe we need to unquote somewhere?

jlowin avatar May 09 '25 20:05 jlowin

I don't know what is happening when the API is called as I actually hardcoded the URL string to make sure those characters were not introduced by my code.

I have also tried changing to async and back to sync tools and nothing helps.

Marcelo

magallardo avatar May 09 '25 20:05 magallardo

one more note. I also tried switching to use requests library instead of hpptx and in this case I don't see errors but the post requests never return. I have some debug statements in the code before and after the post request and I can only see the logs before the request. Everything after the post request is not executed.

magallardo avatar May 09 '25 21:05 magallardo

That's bizarre! Is it possible to provide an MRE so I can debug?

jlowin avatar May 10 '25 13:05 jlowin

@jlowin I am attaching a simple weather server (nw.zip) implemented with fastmcp. I am also attaching a simple client (client.zip) to test it.

I tried on my environment and I am not getting any good responses.

Please let me know if you have any question.

client.zip

nw.zip

magallardo avatar May 12 '25 22:05 magallardo

hi @magallardo - can you share an MRE that one wouldn't have to trust / unzip (public repo, or a self-contained script)?

zzstoatzz avatar May 19 '25 19:05 zzstoatzz

The nw server works just fine for me. I am able to get a forecast via the httpx client.

Without the exact error you're seeing it will be hard to assist. Feel free to open a new issue if you would like to keep working on this.

strawgate avatar Aug 13 '25 02:08 strawgate