Skip to content

Tool.from_tool: a transform_fn that declares ctx: Context fails on every call #5368

Closed

Description

@chrikrah

What happened?

A transform_fn never gets its injected parameters resolved. They are stripped from the schema, and then the call fails. An ordinary tool with the same signature works.

$ python mre.py
ordinary:    6
transformed: ToolError: Error calling tool 'transformed': custom() missing 1 required positional argument: 'ctx'

The documented forms fail the same way: ctx: Context = CurrentContext() arrives as the _CurrentContext marker, and k: int = Depends(dep) as _Depends. Only get_context() works inside a transform_fn.

Example Code

import asyncio
from fastmcp import FastMCP, Client, Context
from fastmcp.tools import Tool

mcp = FastMCP()

@mcp.tool
def base(x: int) -> int:
    return x

@mcp.tool
async def ordinary(x: int, ctx: Context) -> int:
    return x * 2

async def custom(x: int, ctx: Context) -> int:
    return x * 2

async def demo():
    mcp.add_tool(Tool.from_tool(await mcp.get_tool("base"), name="transformed", transform_fn=custom))
    async with Client(mcp) as client:
        print("ordinary:   ", (await client.call_tool("ordinary", {"x": 3})).data)
        try:
            await client.call_tool("transformed", {"x": 3})
        except Exception as e:
            print("transformed:", f"{type(e).__name__}: {e}")

if __name__ == "__main__":
    asyncio.run(demo())

Version Information

FastMCP main at 9488a26 (source checkout)
MCP version: 2.0.0
Python version: 3.12.3
Platform: Linux-7.0.0-1013-aws-x86_64-with-glibc2.39

@zzstoatzz, should Tool.from_tool resolve these for a transform_fn the way it does for a function tool? cc @jlowin, since the design of tool_transform.py is yours.

Activity

  1. added
    bug
    on Oct 1, 2026
  2. added a commit that references this issue on Oct 5, 2026
    b2b1c44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions