π The fast, Pythonic way to build MCP servers and clients.
Remove empty parens
Jeremiah Lowin18615bc48b12
diff --git a/docs/clients/transports.mdx b/docs/clients/transports.mdxindex 6ed9fc489..94c94833c 100644--- a/docs/clients/transports.mdx+++ b/docs/clients/transports.mdx@@ -359,7 +359,7 @@ import asyncio# 1. Create your FastMCP server instanceserver = FastMCP(name="InMemoryServer")def ping():return "pong"diff --git a/docs/patterns/decorating-methods.mdx b/docs/patterns/decorating-methods.mdxindex 906142c44..b4ac48072 100644--- a/docs/patterns/decorating-methods.mdx+++ b/docs/patterns/decorating-methods.mdx@@ -5,11 +5,11 @@ description: Properly use instance methods, class methods, and static methods wiicon: at----FastMCP's decorator system is designed to work with functions, but you may see unexpected behavior if you try to decorate an instance or class method. This guide explains the correct approach for using methods with all FastMCP decorators (`@tool()`, `@resource()`, and `@prompt()`).+FastMCP's decorator system is designed to work with functions, but you may see unexpected behavior if you try to decorate an instance or class method. This guide explains the correct approach for using methods with all FastMCP decorators (`@tool`, `@resource()`, and `@prompt()`).## Why Are Methods Hard?-When you apply a FastMCP decorator like `@tool()`, `@resource()`, or `@prompt()` to a method, the decorator captures the function at decoration time. For instance methods and class methods, this poses a challenge because:+When you apply a FastMCP decorator like `@tool`, `@resource()`, or `@prompt()` to a method, the decorator captures the function at decoration time. For instance methods and class methods, this poses a challenge because:1. For instance methods: The decorator gets the unbound method before any instance exists2. For class methods: The decorator gets the function before it's bound to the class@@ -28,7 +28,7 @@ from fastmcp import FastMCPmcp = FastMCP()class MyClass:- @mcp.tool() # This won't work correctly+ @mcp.tool # This won't work correctlydef add(self, x, y):return x + y@@ -83,7 +83,7 @@ mcp = FastMCP()class MyClass:@classmethod- @mcp.tool() # This won't work correctly+ @mcp.tool # This won't work correctlydef from_string(cls, s):return cls(s)```@@ -122,7 +122,7 @@ mcp = FastMCP()class MyClass:@staticmethod- @mcp.tool() # This works!+ @mcp.tool # This works!def utility(x, y):return x + y@@ -194,7 +194,7 @@ The class automatically registers its methods during initialization, ensuring thWhile FastMCP's decorator pattern works seamlessly with regular functions and static methods, for instance methods and class methods, you should add them after creating the instance or class. This ensures that the methods are properly bound before being registered.These patterns apply to all FastMCP decorators and registration methods:-- `@tool()` and `add_tool()`+- `@tool()` and `add_tool`- `@resource()` and `add_resource_fn()`- `@prompt()` and `add_prompt()`diff --git a/docs/patterns/testing.mdx b/docs/patterns/testing.mdxindex 34c05a6f8..adc3f516a 100644--- a/docs/patterns/testing.mdx+++ b/docs/patterns/testing.mdx@@ -22,7 +22,7 @@ from fastmcp import FastMCP, Clientdef mcp_server():server = FastMCP("TestServer")- @server.tool()+ @server.tooldef greet(name: str) -> str:return f"Hello, {name}!"diff --git a/docs/servers/composition.mdx b/docs/servers/composition.mdxindex b1519b3f3..04dc559d0 100644--- a/docs/servers/composition.mdx+++ b/docs/servers/composition.mdx@@ -50,7 +50,7 @@ import asyncio# Define subserversweather_mcp = FastMCP(name="WeatherService")-@weather_mcp.tool()+@weather_mcp.tooldef get_forecast(city: str) -> dict:"""Get weather forecast."""return {"city": city, "forecast": "Sunny"}@@ -102,7 +102,7 @@ from fastmcp import FastMCP, Client# Define subserverdynamic_mcp = FastMCP(name="DynamicService")-@dynamic_mcp.tool()+@dynamic_mcp.tooldef initial_tool():"""Initial tool demonstration."""return "Initial Tool Exists"@@ -112,7 +112,7 @@ main_mcp = FastMCP(name="MainAppLive")main_mcp.mount("dynamic", dynamic_mcp)# Add a tool AFTER mounting - it will be accessible through main_mcp-@dynamic_mcp.tool()+@dynamic_mcp.tooldef added_later():"""Tool added after mounting."""return "Tool Added Dynamically!"diff --git a/docs/servers/fastmcp.mdx b/docs/servers/fastmcp.mdxindex ebb3fcb64..40808fb95 100644--- a/docs/servers/fastmcp.mdx+++ b/docs/servers/fastmcp.mdx@@ -145,7 +145,7 @@ import asynciomain = FastMCP(name="Main")sub = FastMCP(name="Sub")def hello():return "hi"diff --git a/docs/servers/proxy.mdx b/docs/servers/proxy.mdxindex d78d6d694..5a9bffccd 100644--- a/docs/servers/proxy.mdx+++ b/docs/servers/proxy.mdx@@ -90,7 +90,7 @@ from fastmcp import FastMCP# Original serveroriginal_server = FastMCP(name="Original")-@original_server.tool()+@original_server.tooldef tool_a() -> str:return "A"diff --git a/examples/mount_example.py b/examples/mount_example.pyindex 821794a39..7720f0eb2 100644--- a/examples/mount_example.py+++ b/examples/mount_example.py@@ -16,7 +16,7 @@ from fastmcp import FastMCPweather_app = FastMCP("Weather App")-@weather_app.tool()+@weather_app.tooldef get_weather_forecast(location: str) -> str:"""Get the weather forecast for a location."""return f"Sunny skies for {location} today!"@@ -32,7 +32,7 @@ async def weather_data():news_app = FastMCP("News App")-@news_app.tool()+@news_app.tooldef get_news_headlines() -> list[str]:"""Get the latest news headlines."""return [@@ -58,7 +58,7 @@ app = FastMCP()def check_app_status() -> dict[str, str]:"""Check the status of the main application."""return {"status": "running", "version": "1.0.0", "uptime": "3h 24m"}diff --git a/examples/serializer.py b/examples/serializer.pyindex 72e60843d..4ee5ca17a 100644--- a/examples/serializer.py+++ b/examples/serializer.py@@ -14,7 +14,7 @@ def custom_dict_serializer(data: Any) -> str:server = FastMCP(name="CustomSerializerExample", tool_serializer=custom_dict_serializer)def get_example_data() -> dict:"""Returns some example data."""return {"name": "Test", "value": 123, "status": True}diff --git a/examples/smart_home/src/smart_home/hub.py b/examples/smart_home/src/smart_home/hub.pyindex 55239b8be..ff91576fc 100644--- a/examples/smart_home/src/smart_home/hub.py+++ b/examples/smart_home/src/smart_home/hub.py@@ -16,7 +16,7 @@ hub_mcp.mount("hue", lights_mcp)# Add a status check for the hub-@hub_mcp.tool()+@hub_mcp.tooldef hub_status() -> str:"""Checks the status of the main hub and connections."""try:diff --git a/examples/smart_home/src/smart_home/lights/server.py b/examples/smart_home/src/smart_home/lights/server.pyindex c95382cc0..87e8d4455 100644--- a/examples/smart_home/src/smart_home/lights/server.py+++ b/examples/smart_home/src/smart_home/lights/server.py@@ -43,7 +43,7 @@ lights_mcp = FastMCP()-@lights_mcp.tool()+@lights_mcp.tooldef read_all_lights() -> list[str]:"""Lists the names of all available Hue lights using phue2."""if not (bridge := _get_bridge()):@@ -59,7 +59,7 @@ def read_all_lights() -> list[str]:# --- Tools ----@lights_mcp.tool()+@lights_mcp.tooldef toggle_light(light_name: str, state: bool) -> dict[str, Any]:"""Turns a specific light on (true) or off (false) using phue2."""if not (bridge := _get_bridge()):@@ -76,7 +76,7 @@ def toggle_light(light_name: str, state: bool) -> dict[str, Any]:return handle_phue_error(light_name, "toggle_light", e)-@lights_mcp.tool()+@lights_mcp.tooldef set_brightness(light_name: str, brightness: int) -> dict[str, Any]:"""Sets the brightness of a specific light (0-254) using phue2."""if not (bridge := _get_bridge()):@@ -100,7 +100,7 @@ def set_brightness(light_name: str, brightness: int) -> dict[str, Any]:return handle_phue_error(light_name, "set_brightness", e)-@lights_mcp.tool()+@lights_mcp.tooldef list_groups() -> list[str]:"""Lists the names of all available Hue light groups."""if not (bridge := _get_bridge()):@@ -113,7 +113,7 @@ def list_groups() -> list[str]:return [f"Error listing groups: {e}"]-@lights_mcp.tool()+@lights_mcp.tooldef list_scenes() -> dict[str, list[str]] | list[str]:"""Lists Hue scenes, grouped by the light group they belong to.@@ -154,7 +154,7 @@ def list_scenes() -> dict[str, list[str]] | list[str]:return [f"Error listing scenes by group: {e}"]-@lights_mcp.tool()+@lights_mcp.tooldef activate_scene(group_name: str, scene_name: str) -> dict[str, Any]:"""Activates a specific scene within a specified light group, verifying the scene belongs to the group."""if not (bridge := _get_bridge()):@@ -215,7 +215,7 @@ def activate_scene(group_name: str, scene_name: str) -> dict[str, Any]:return handle_phue_error(f"{group_name}/{scene_name}", "activate_scene", e)-@lights_mcp.tool()+@lights_mcp.tooldef set_light_attributes(light_name: str, attributes: HueAttributes) -> dict[str, Any]:"""Sets multiple attributes (e.g., hue, sat, bri, ct, xy, transitiontime) for a specific light."""if not (bridge := _get_bridge()):@@ -242,7 +242,7 @@ def set_light_attributes(light_name: str, attributes: HueAttributes) -> dict[strreturn handle_phue_error(light_name, "set_light_attributes", e)-@lights_mcp.tool()+@lights_mcp.tooldef set_group_attributes(group_name: str, attributes: HueAttributes) -> dict[str, Any]:"""Sets multiple attributes for all lights within a specific group."""if not (bridge := _get_bridge()):@@ -267,7 +267,7 @@ def set_group_attributes(group_name: str, attributes: HueAttributes) -> dict[strreturn handle_phue_error(group_name, "set_group_attributes", e)-@lights_mcp.tool()+@lights_mcp.tooldef list_lights_by_group() -> dict[str, list[str]] | list[str]:"""Lists Hue lights, grouped by the room/group they belong to.diff --git a/src/fastmcp/server/context.py b/src/fastmcp/server/context.pyindex 7dc77f4d1..97a05c672 100644--- a/src/fastmcp/server/context.py+++ b/src/fastmcp/server/context.py@@ -49,7 +49,7 @@ class Context:To use context in a tool function, add a parameter with the Context type annotation:```python- @server.tool()+ @server.tooldef my_tool(x: int, ctx: Context) -> str:# Log messages to the clientctx.info(f"Processing {x}")diff --git a/src/fastmcp/server/server.py b/src/fastmcp/server/server.pyindex 86f418488..19f4deac4 100644--- a/src/fastmcp/server/server.py+++ b/src/fastmcp/server/server.py@@ -529,7 +529,7 @@ class FastMCP(Generic[LifespanResultT]):This decorator supports multiple calling patterns:- @server.tool (without parentheses)- - @server.tool() (with empty parentheses)+ - @server.tool (with empty parentheses)- @server.tool("custom_name") (with name as first argument)- @server.tool(name="custom_name") (with name as keyword argument)- server.tool(function, name="custom_name") (direct function call)@@ -547,7 +547,7 @@ class FastMCP(Generic[LifespanResultT]):def my_tool(x: int) -> str:return str(x)- @server.tool()+ @server.tooldef my_tool(x: int) -> str:return str(x)@@ -594,7 +594,7 @@ class FastMCP(Generic[LifespanResultT]):)tool_name = name_or_fnelif name_or_fn is None:- # Case 4: @tool() or @tool(name="something") - use keyword name+ # Case 4: @tool or @tool(name="something") - use keyword nametool_name = nameelse:raise TypeError(diff --git a/tests/auth/test_oauth_client.py b/tests/auth/test_oauth_client.pyindex be3328a07..292f6c4af 100644--- a/tests/auth/test_oauth_client.py+++ b/tests/auth/test_oauth_client.py@@ -24,7 +24,7 @@ def fastmcp_server(issuer_url: str):),)- @server.tool()+ @server.tooldef add(a: int, b: int) -> int:"""Add two numbers together."""return a + bdiff --git a/tests/client/test_client.py b/tests/client/test_client.pyindex bfb75e492..d496bb8a5 100644--- a/tests/client/test_client.py+++ b/tests/client/test_client.py@@ -25,18 +25,18 @@ def fastmcp_server():server = FastMCP("TestServer")# Add a tool- @server.tool()+ @server.tooldef greet(name: str) -> str:"""Greet someone by name."""return f"Hello, {name}!"# Add a second tool- @server.tool()+ @server.tooldef add(a: int, b: int) -> int:"""Add two numbers together."""return a + b- @server.tool()+ @server.toolasync def sleep(seconds: float) -> str:"""Sleep for a given number of seconds."""await asyncio.sleep(seconds)@@ -347,7 +347,7 @@ async def test_concurrent_client_context_managers():# Create a simple serverserver = FastMCP("Test Server")- @server.tool()+ @server.tooldef echo(text: str) -> str:"""Echo tool"""return textdiff --git a/tests/client/test_sse.py b/tests/client/test_sse.pyindex 39b556dda..b0960eb14 100644--- a/tests/client/test_sse.py+++ b/tests/client/test_sse.py@@ -21,18 +21,18 @@ def fastmcp_server():server = FastMCP("TestServer")# Add a tool- @server.tool()+ @server.tooldef greet(name: str) -> str:"""Greet someone by name."""return f"Hello, {name}!"# Add a second tool- @server.tool()+ @server.tooldef add(a: int, b: int) -> int:"""Add two numbers together."""return a + b- @server.tool()+ @server.toolasync def sleep(seconds: float) -> str:"""Sleep for a given number of seconds."""await asyncio.sleep(seconds)diff --git a/tests/client/test_streamable_http.py b/tests/client/test_streamable_http.pyindex a806f7200..da8502c63 100644--- a/tests/client/test_streamable_http.py+++ b/tests/client/test_streamable_http.py@@ -21,18 +21,18 @@ def fastmcp_server():server = FastMCP("TestServer")# Add a tool- @server.tool()+ @server.tooldef greet(name: str) -> str:"""Greet someone by name."""return f"Hello, {name}!"# Add a second tool- @server.tool()+ @server.tooldef add(a: int, b: int) -> int:"""Add two numbers together."""return a + b- @server.tool()+ @server.toolasync def sleep(seconds: float) -> str:"""Sleep for a given number of seconds."""await asyncio.sleep(seconds)diff --git a/tests/deprecated/test_deprecated.py b/tests/deprecated/test_deprecated.pyindex 50caf018a..fcb6fe2ed 100644--- a/tests/deprecated/test_deprecated.py+++ b/tests/deprecated/test_deprecated.py@@ -100,7 +100,7 @@ def test_mount_tool_separator_deprecation_warning():main_app.mount("sub", sub_app, tool_separator="-")# Verify the separator is ignored and the default is used- @sub_app.tool()+ @sub_app.tooldef test_tool():return "test"diff --git a/tests/deprecated/test_mount_separators.py b/tests/deprecated/test_mount_separators.pyindex b088b5ef7..514de02cf 100644--- a/tests/deprecated/test_mount_separators.py+++ b/tests/deprecated/test_mount_separators.py@@ -20,7 +20,7 @@ def test_mount_tool_separator_deprecation_warning():main_app.mount("sub", sub_app, tool_separator="-")# Verify the separator is ignored and the default is used- @sub_app.tool()+ @sub_app.tooldef test_tool():return "test"diff --git a/tests/server/http/test_http_dependencies.py b/tests/server/http/test_http_dependencies.pyindex 580ceabd3..60ebbb1c6 100644--- a/tests/server/http/test_http_dependencies.py+++ b/tests/server/http/test_http_dependencies.py@@ -14,7 +14,7 @@ def fastmcp_server():server = FastMCP()# Add a tool- @server.tool()+ @server.tooldef get_headers_tool() -> dict[str, str]:"""Get the HTTP headers from the request."""request = get_http_request()diff --git a/tests/server/test_import_server.py b/tests/server/test_import_server.pyindex a6f7bccef..c903cfa8a 100644--- a/tests/server/test_import_server.py+++ b/tests/server/test_import_server.py@@ -13,7 +13,7 @@ async def test_import_basic_functionality():sub_app = FastMCP("SubApp")# Add a tool to the sub-app- @sub_app.tool()+ @sub_app.tooldef sub_tool() -> str:return "This is from the sub app"@@ -40,11 +40,11 @@ async def test_import_multiple_apps():news_app = FastMCP("NewsApp")# Add tools to each sub-app- @weather_app.tool()+ @weather_app.tooldef get_forecast() -> str:return "Weather forecast"- @news_app.tool()+ @news_app.tooldef get_headlines() -> str:return "News headlines"@@ -65,11 +65,11 @@ async def test_import_combines_tools():second_app = FastMCP("SecondApp")# Add tools to each sub-app- @first_app.tool()+ @first_app.tooldef first_tool() -> str:return "First app tool"- @second_app.tool()+ @second_app.tooldef second_tool() -> str:return "Second app tool"@@ -294,7 +294,7 @@ async def test_import_with_proxy_tools():main_app = FastMCP("MainApp")api_app = FastMCP("APIApp")- @api_app.tool()+ @api_app.tooldef get_data(query: str) -> str:return f"Data for query: {query}"diff --git a/tests/server/test_mount.py b/tests/server/test_mount.pyindex 17fdf3a68..e94f19b18 100644--- a/tests/server/test_mount.py+++ b/tests/server/test_mount.py@@ -21,7 +21,7 @@ class TestBasicMount:sub_app = FastMCP("SubApp")# Add a tool to the sub-app- @sub_app.tool()+ @sub_app.tooldef sub_tool() -> str:return "This is from the sub app"@@ -41,7 +41,7 @@ class TestBasicMount:main_app = FastMCP("MainApp")sub_app = FastMCP("SubApp")- @sub_app.tool()+ @sub_app.tooldef greet(name: str) -> str:return f"Hello, {name}!"@@ -77,7 +77,7 @@ class TestBasicMount:main_app = FastMCP("MainApp")sub_app = FastMCP("SubApp")- @sub_app.tool()+ @sub_app.tooldef sub_tool() -> str:return "This is from the sub app"@@ -103,7 +103,7 @@ class TestBasicMount:main_app = FastMCP("MainApp")sub_app = FastMCP("SubApp")- @sub_app.tool()+ @sub_app.tooldef sub_tool() -> str:return "This is from the sub app"@@ -124,11 +124,11 @@ class TestMultipleServerMount:weather_app = FastMCP("WeatherApp")news_app = FastMCP("NewsApp")- @weather_app.tool()+ @weather_app.tooldef get_forecast() -> str:return "Weather forecast"- @news_app.tool()+ @news_app.tooldef get_headlines() -> str:return "News headlines"@@ -154,11 +154,11 @@ class TestMultipleServerMount:first_app = FastMCP("FirstApp")second_app = FastMCP("SecondApp")- @first_app.tool()+ @first_app.tooldef first_tool() -> str:return "First app tool"- @second_app.tool()+ @second_app.tooldef second_tool() -> str:return "Second app tool"@@ -186,7 +186,7 @@ class TestMultipleServerMount:main_app = FastMCP("MainApp")working_app = FastMCP("WorkingApp")- @working_app.tool()+ @working_app.tooldef working_tool() -> str:return "Working tool"@@ -267,7 +267,7 @@ class TestDynamicChanges:assert not any(key.startswith("sub_") for key in tools)# Add a tool to the sub-app after mounting- @sub_app.tool()+ @sub_app.tooldef dynamic_tool() -> str:return "Added after mounting"@@ -284,7 +284,7 @@ class TestDynamicChanges:main_app = FastMCP("MainApp")sub_app = FastMCP("SubApp")- @sub_app.tool()+ @sub_app.tooldef temp_tool() -> str:return "Temporary tool"@@ -431,7 +431,7 @@ class TestProxyServer:# Create original serveroriginal_server = FastMCP("OriginalServer")- @original_server.tool()+ @original_server.tooldef get_data(query: str) -> str:return f"Data for {query}"@@ -467,7 +467,7 @@ class TestProxyServer:main_app.mount("proxy", proxy_server)# Add a tool to the original server- @original_server.tool()+ @original_server.tooldef dynamic_data() -> str:return "Dynamic data"@@ -602,7 +602,7 @@ class TestAsProxyKwarg:assert len(await mcp.get_tools()) == 0- @sub.tool()+ @sub.tooldef hello():return "hi"@@ -619,7 +619,7 @@ class TestAsProxyKwarg:mcp = FastMCP("Main")sub = FastMCP("Sub", lifespan=lifespan)- @sub.tool()+ @sub.tooldef hello():return "hi"diff --git a/tests/server/test_proxy.py b/tests/server/test_proxy.pyindex f5ef12bf3..8ad2450d6 100644--- a/tests/server/test_proxy.py+++ b/tests/server/test_proxy.py@@ -26,21 +26,21 @@ def fastmcp_server():# --- Tools ---- @server.tool()+ @server.tooldef greet(name: str) -> str:"""Greet someone by name."""return f"Hello, {name}!"- @server.tool()+ @server.tooldef tool_without_description() -> str:return "Hello?"- @server.tool()+ @server.tooldef add(a: int, b: int) -> int:"""Add two numbers together."""return a + b- @server.tool()+ @server.tooldef error_tool():"""This tool always raises an error."""raise ValueError("This is a test error")diff --git a/tests/server/test_run_server.py b/tests/server/test_run_server.pyindex 5109a27bc..8e631c3a4 100644--- a/tests/server/test_run_server.py+++ b/tests/server/test_run_server.py@@ -22,17 +22,17 @@# # --- Tools ----# @server.tool()+# @server.tool# def greet(name: str) -> str:# """Greet someone by name."""# return f"Hello, {name}!"-# @server.tool()+# @server.tool# def add(a: int, b: int) -> int:# """Add two numbers together."""# return a + b-# @server.tool()+# @server.tool# def error_tool():# """This tool always raises an error."""# raise ValueError("This is a test error")diff --git a/tests/test_servers/fastmcp_server.py b/tests/test_servers/fastmcp_server.pyindex f24bbfeef..6e13d2ddc 100644--- a/tests/test_servers/fastmcp_server.py+++ b/tests/test_servers/fastmcp_server.py@@ -14,19 +14,19 @@ server = FastMCP("TestServer")# --- Tools ---def greet(name: str) -> str:"""Greet someone by name."""return f"Hello, {name}!"def add(a: int, b: int) -> int:"""Add two numbers together."""return a + bdef error_tool():"""This tool always raises an error."""raise ValueError("This is a test error")