fastapi_mcp icon indicating copy to clipboard operation
fastapi_mcp copied to clipboard

Allow passing custom arguments to http client + Add better documentation on the subject

Open davidmezzetti opened this issue 8 months ago • 3 comments

Hello,

I'm aware of the ability to set a custom http_client parameter but that is now requiring a bit more.

Looking at the code now:

        self._http_client = http_client or httpx.AsyncClient(
            transport=httpx.ASGITransport(app=self.fastapi, raise_app_exceptions=False),
            base_url=self._base_url,
            timeout=10.0,
        )

The latest version requires copying the base url and ASGITransport call. In my use case, I'd like to just override the timeout.

Could we add a new parameter such as client_args with a dictionary of options to overlay when creating the client. For example:

        self._http_client = http_client or httpx.AsyncClient(
          **{{
            transport=httpx.ASGITransport(app=self.fastapi, raise_app_exceptions=False),
            base_url=self._base_url,
            timeout=10.0,
          }, **client_args}
        )

This would have the benefit of allowing a user to override a single http client parameter without having to know what else to pass.

davidmezzetti avatar Apr 22 '25 16:04 davidmezzetti

Sure. That's a good idea :) Just need to make sure that client_args has a strict type definition that matches what httpx.AsyncClient expects. And also maybe call it http_client_args to be more specific

shahar4499 avatar Apr 22 '25 16:04 shahar4499

Sounds great! That all makes sense.

davidmezzetti avatar Apr 22 '25 16:04 davidmezzetti

added a pull request #98

AB-stack-cmd avatar Aug 18 '25 13:08 AB-stack-cmd