published fresh until

MCP Python SDK: wrap app functions as typed tools

Summary Expose an application function through a typed adapter, derive input and output schemas, and call it from a Python stdio client. This example pins mcp==2.3.0

Not for Python MCP server setup and Inspector basics; see build-first-mcp-server-python-mcpserver-fastmcp-uv-inspector

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Wrap the application call in an explicitly typed @mcp.tool(structured_output=True) adapter, then inspect its schemas and call it with the SDK's Client.

Do first: run python -c "from importlib.metadata import version; print(version('mcp'))" in the project environment: is this the official mcp package at 2.3.0? The branches below target that version, as of November 2026 (reference date: 2026-11-24). If adopting this example, pin python -m pip install "mcp==2.3.0"; an existing 1.x project needs its migration first. The 2.3.0 release, dated October 2, 2026, is the version boundary here.

1. If your application function has no MCP interface

For mcp 2.3.0, follow the versioned Tools guide: function name becomes tool name, docstring becomes description, and parameters become the input schema. Keep the application logic callable separately.

For a complete small example, save this application function as application.py:

def calculate_total(unit_cents: int, quantity: int) -> int:
    return unit_cents * quantity

Save the MCP adapter as server.py:

from typing import Annotated

from pydantic import BaseModel, Field
from mcp.server import MCPServer

from application import calculate_total

mcp = MCPServer("Order totals")


class TotalResult(BaseModel):
    total_cents: int


@mcp.tool(structured_output=True)
def order_total(
    unit_cents: Annotated[int, Field(ge=0)],
    quantity: Annotated[int, Field(ge=1, le=100)] = 1,
) -> TotalResult:
    """Calculate an order total in cents."""
    return TotalResult(
        total_cents=calculate_total(unit_cents, quantity)
    )


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

The 2.3.0 server runner guide documents the main guard and stdio transport. Replace calculate_total with your application's call while retaining an explicit boundary signature.

Confirmation in your project: branch 3 should show unit_cents in required, quantity with default 1, and its bounds 1 and 100. Giving a parameter a default makes it optional to supply; constraints become schema bounds and input validation.

2. If callers need named result fields instead of parsing text

For mcp 2.3.0, follow Structured Output at the same tag. The adapter's -> TotalResult publishes an object schema with total_cents; its value appears directly in structured_content. Returning a scalar such as -> int instead produces {"result": 1250}.

Keep structured_output=True when an output schema is required: an unsupported return annotation then fails during registration instead of silently becoming text. Confirmation: branch 3's output_schema assertion should find total_cents, and its call assertion should receive {"total_cents": 1250}. The SDK also renders the result as text in content.

3. If the adapter exists but you need a small client call

For mcp 2.3.0, use the versioned client guide and stdio client example. Save client.py beside the other two files:

import asyncio
import sys
from pathlib import Path

from mcp import Client, StdioServerParameters


async def main() -> None:
    server = StdioServerParameters(
        command=sys.executable,
        args=[str(Path(__file__).with_name("server.py").resolve())],
    )
    async with Client(server) as client:
        tools = (await client.list_tools()).tools
        tool = next(t for t in tools if t.name == "order_total")

        schema = tool.input_schema
        assert schema["required"] == ["unit_cents"]
        quantity = schema["properties"]["quantity"]
        assert quantity["default"] == 1
        assert quantity["minimum"] == 1
        assert quantity["maximum"] == 100
        assert tool.output_schema is not None
        assert "total_cents" in tool.output_schema["properties"]

        result = await client.call_tool(
            "order_total", {"unit_cents": 250, "quantity": 5}
        )
        if result.is_error:
            raise RuntimeError(result.content)
        assert result.structured_content == {"total_cents": 1250}
        print(result.structured_content)


asyncio.run(main())

Run python client.py with the same environment used for the pin. The client launches the server subprocess; async with opens and closes it, per the 2.3.0 transport guide. Confirmation is the schema assertions passing and the printed dictionary. Read is_error before consuming structured data: a tool failure can return an error result without raising a client exception.

4. If the tool should deliberately return only text

For mcp 2.3.0, use the structured-output opt-out: set @mcp.tool(structured_output=False) on a function returning a str. Confirmation through the same client methods: tool.output_schema is None, result.structured_content is None, and the text is in result.content. Remove the object-result assertions when choosing this branch.

Evidence and limits

The versioned docs establish the API and schema rules. In issue #3745, opened October 23, 2026, benjamin-di reports on mcp 2.3.0 that changing typing.Dict[str, Any] to dict[str, Any] changed the output shape and caused a client with a cached schema to reject results. That report supports inspecting the published schema when changing annotations; it does not establish an outcome for this application adapter.

These examples target the official SDK mcp 2.3.0. A separate fastmcp installation has its own API. Schema validation does not establish that an application's calculation or external side effects are correct. This is a direct client invocation; it does not include an LLM host or a remote authentication flow.

Not for: Python MCP server setup and Inspector basics; see build-first-mcp-server-python-mcpserver-fastmcp-uv-inspector

Started from a post by @Model Context Protocol, packaged by the editor. Original post

Subjects

More in model context protocol

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# MCP Python SDK: wrap app functions as typed tools
Summary: Expose an application function through a typed adapter, derive input and output schemas, and call it from a Python stdio client. This example pins `mcp==2.3.0`
Copy for agent
get the whatworks thread mcp-python-sdk-wrap-app-functions-typed-tool-schemas-client and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread mcp-python-sdk-wrap-app-functions-typed-tool-schemas-client
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks