π The fast, Pythonic way to build MCP servers and clients.
remove empty parens from tool
Jeremiah Lowin5a0f57498f10
diff --git a/README.md b/README.mdindex 6ea094b26..e9ce712ef 100644--- a/README.md+++ b/README.md@@ -31,7 +31,7 @@ from fastmcp import FastMCPmcp = FastMCP("Demo π")def add(a: int, b: int) -> int:"""Add two numbers"""return a + b@@ -144,7 +144,7 @@ Learn more in the [**FastMCP Server Documentation**](https://gofastmcp.com/serveTools allow LLMs to perform actions by executing your Python functions (sync or async). Ideal for computations, API calls, or side effects (like `POST`/`PUT`). FastMCP handles schema generation from type hints and docstrings. Tools can return various types, including text, JSON-serializable objects, and even images using the [`fastmcp.Image`](https://gofastmcp.com/servers/tools#return-values) helper.```pythondef multiply(a: float, b: float) -> float:"""Multiplies two numbers."""return a * b@@ -201,7 +201,7 @@ from fastmcp import FastMCP, Contextmcp = FastMCP("My MCP Server")async def process_data(uri: str, ctx: Context):# Log a message to the clientawait ctx.info(f"Processing {uri}...")@@ -321,7 +321,7 @@ from fastmcp import FastMCPmcp = FastMCP("Demo π")def hello(name: str) -> str:return f"Hello, {name}!"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/deployment/asgi.mdx b/docs/deployment/asgi.mdxindex ace7ee136..06da46374 100644--- a/docs/deployment/asgi.mdx+++ b/docs/deployment/asgi.mdx@@ -32,7 +32,7 @@ from fastmcp import FastMCPmcp = FastMCP("MyServer")def hello(name: str) -> str:return f"Hello, {name}!"diff --git a/docs/deployment/running-server.mdx b/docs/deployment/running-server.mdxindex 5f5d758d2..5b6a7eadd 100644--- a/docs/deployment/running-server.mdx+++ b/docs/deployment/running-server.mdx@@ -22,7 +22,7 @@ from fastmcp import FastMCPmcp = FastMCP(name="MyServer")def hello(name: str) -> str:return f"Hello, {name}!"@@ -244,7 +244,7 @@ import asynciomcp = FastMCP(name="MyServer")def hello(name: str) -> str:return f"Hello, {name}!"diff --git a/docs/getting-started/quickstart.mdx b/docs/getting-started/quickstart.mdxindex 13a6bbae5..b3569d486 100644--- a/docs/getting-started/quickstart.mdx+++ b/docs/getting-started/quickstart.mdx@@ -32,7 +32,7 @@ from fastmcp import FastMCPmcp = FastMCP("My MCP Server")def greet(name: str) -> str:return f"Hello, {name}!"```@@ -49,7 +49,7 @@ from fastmcp import FastMCP, Clientmcp = FastMCP("My MCP Server")def greet(name: str) -> str:return f"Hello, {name}!"@@ -76,7 +76,7 @@ from fastmcp import FastMCPmcp = FastMCP("My MCP Server")def greet(name: str) -> str:return f"Hello, {name}!"diff --git a/docs/getting-started/welcome.mdx b/docs/getting-started/welcome.mdxindex f5574217c..d9d921a7a 100644--- a/docs/getting-started/welcome.mdx+++ b/docs/getting-started/welcome.mdx@@ -14,7 +14,7 @@ from fastmcp import FastMCPmcp = FastMCP("Demo π")def add(a: int, b: int) -> int:"""Add two numbers"""return a + bdiff --git a/docs/integrations/anthropic.mdx b/docs/integrations/anthropic.mdxindex 4d39a1afd..041040b00 100644--- a/docs/integrations/anthropic.mdx+++ b/docs/integrations/anthropic.mdx@@ -28,7 +28,7 @@ from fastmcp import FastMCPmcp = FastMCP(name="Dice Roller")def roll_dice(n_dice: int) -> list[int]:"""Roll `n_dice` 6-sided dice and return the results."""return [random.randint(1, 6) for _ in range(n_dice)]@@ -171,7 +171,7 @@ auth = BearerAuthProvider(mcp = FastMCP(name="Dice Roller", auth=auth)def roll_dice(n_dice: int) -> list[int]:"""Roll `n_dice` 6-sided dice and return the results."""return [random.randint(1, 6) for _ in range(n_dice)]diff --git a/docs/integrations/claude-desktop.mdx b/docs/integrations/claude-desktop.mdxindex 7edafa68b..9b8dc5404 100644--- a/docs/integrations/claude-desktop.mdx+++ b/docs/integrations/claude-desktop.mdx@@ -31,7 +31,7 @@ from fastmcp import FastMCPmcp = FastMCP(name="Dice Roller")def roll_dice(n_dice: int) -> list[int]:"""Roll `n_dice` 6-sided dice and return the results."""return [random.randint(1, 6) for _ in range(n_dice)]diff --git a/docs/integrations/gemini.mdx b/docs/integrations/gemini.mdxindex 41668232e..1af4de073 100644--- a/docs/integrations/gemini.mdx+++ b/docs/integrations/gemini.mdx@@ -32,7 +32,7 @@ from fastmcp import FastMCPmcp = FastMCP(name="Dice Roller")def roll_dice(n_dice: int) -> list[int]:"""Roll `n_dice` 6-sided dice and return the results."""return [random.randint(1, 6) for _ in range(n_dice)]diff --git a/docs/integrations/openai.mdx b/docs/integrations/openai.mdxindex 35546d561..a5b1e933f 100644--- a/docs/integrations/openai.mdx+++ b/docs/integrations/openai.mdx@@ -33,7 +33,7 @@ from fastmcp import FastMCPmcp = FastMCP(name="Dice Roller")def roll_dice(n_dice: int) -> list[int]:"""Roll `n_dice` 6-sided dice and return the results."""return [random.randint(1, 6) for _ in range(n_dice)]@@ -166,7 +166,7 @@ auth = BearerAuthProvider(mcp = FastMCP(name="Dice Roller", auth=auth)def roll_dice(n_dice: int) -> list[int]:"""Roll `n_dice` 6-sided dice and return the results."""return [random.randint(1, 6) for _ in range(n_dice)]diff --git a/docs/patterns/cli.mdx b/docs/patterns/cli.mdxindex b46d928d4..19e1ad1ab 100644--- a/docs/patterns/cli.mdx+++ b/docs/patterns/cli.mdx@@ -66,7 +66,7 @@ from fastmcp import FastMCPmcp = FastMCP("MyServer")def hello(name: str) -> str:return f"Hello, {name}!"diff --git a/docs/patterns/decorating-methods.mdx b/docs/patterns/decorating-methods.mdxindex 7337cfe15..3b2c22dd6 100644--- a/docs/patterns/decorating-methods.mdx+++ b/docs/patterns/decorating-methods.mdx@@ -9,7 +9,7 @@ FastMCP's decorator system is designed to work with functions, but you may see u## 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@@ -29,7 +29,7 @@ from fastmcp import FastMCPmcp = FastMCP()class MyClass:- @mcp.tool()+ @mcp.tooldef my_method(self, x: int) -> int:return x * 2@@ -53,7 +53,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```@@ -100,19 +100,19 @@ mcp = FastMCP()class MyClass:@classmethod- @mcp.tool() # This won't work but won't raise an error+ @mcp.tool # This won't work but won't raise an errordef from_string_v1(cls, s):return cls(s)- @mcp.tool()+ @mcp.tool@classmethod # This will raise a helpful ValueErrordef from_string_v2(cls, s):return cls(s)```</Warning>-- If `@classmethod` comes first, then `@mcp.tool()`: No error is raised, but it won't work correctly-- If `@mcp.tool()` comes first, then `@classmethod`: FastMCP will detect this and raise a helpful `ValueError` with guidance+- If `@classmethod` comes first, then `@mcp.tool`: No error is raised, but it won't work correctly+- If `@mcp.tool` comes first, then `@classmethod`: FastMCP will detect this and raise a helpful `ValueError` with guidance<Check>**Do this instead**:@@ -150,7 +150,7 @@ from fastmcp import FastMCPmcp = FastMCP()class MyClass:- @mcp.tool()+ @mcp.tool@staticmethoddef utility(x, y):return x + ydiff --git a/docs/patterns/http-requests.mdx b/docs/patterns/http-requests.mdxindex ceb333e19..c9b412c5d 100644--- a/docs/patterns/http-requests.mdx+++ b/docs/patterns/http-requests.mdx@@ -25,7 +25,7 @@ from starlette.requests import Requestmcp = FastMCP(name="HTTP Request Demo")async def user_agent_info() -> dict:"""Return information about the user agent."""# Get the HTTP request@@ -58,7 +58,7 @@ from fastmcp.server.dependencies import get_http_headersmcp = FastMCP(name="Headers Demo")async def safe_header_info() -> dict:"""Safely get header information without raising errors."""# Get headers (returns empty dict if no request context)diff --git a/docs/patterns/testing.mdx b/docs/patterns/testing.mdxindex c8df2c759..910ac6686 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")def greet(name: str) -> str:return f"Hello, {name}!"diff --git a/docs/servers/auth/bearer.mdx b/docs/servers/auth/bearer.mdxindex a7dc9e5ed..257238bc1 100644--- a/docs/servers/auth/bearer.mdx+++ b/docs/servers/auth/bearer.mdx@@ -160,7 +160,7 @@ Once authenticated, your tools, resources, or prompts can access token informatifrom fastmcp import FastMCP, Context, ToolErrorfrom fastmcp.server.dependencies import get_access_token, AccessTokenasync def get_my_data(ctx: Context) -> dict:access_token: AccessToken = get_access_token()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/context.mdx b/docs/servers/context.mdxindex 555a0c105..2818de3c0 100644--- a/docs/servers/context.mdx+++ b/docs/servers/context.mdx@@ -41,7 +41,7 @@ from fastmcp import FastMCP, Contextmcp = FastMCP(name="ContextDemo")async def process_file(file_uri: str, ctx: Context) -> str:"""Processes a file, using context for logging and resource access."""# Context is available as the ctx parameter@@ -99,7 +99,7 @@ async def process_data(data: list[float]) -> dict:ctx = get_context()await ctx.info(f"Processing {len(data)} data points")async def analyze_dataset(dataset_name: str) -> dict:# Call utility function that uses context internallydata = load_data(dataset_name)@@ -118,7 +118,7 @@ async def analyze_dataset(dataset_name: str) -> dict:Send log messages back to the MCP client. This is useful for debugging and providing visibility into function execution during a request.```pythonasync def analyze_data(data: list[float], ctx: Context) -> dict:"""Analyze numerical data with logging."""await ctx.debug("Starting analysis of numerical data")@@ -149,7 +149,7 @@ async def analyze_data(data: list[float], ctx: Context) -> dict:For long-running operations, notify the client about the progress. This allows clients to display progress indicators and provide a better user experience.```pythonasync def process_items(items: list[str], ctx: Context) -> dict:"""Process a list of items with progress updates."""total = len(items)@@ -182,7 +182,7 @@ Progress reporting requires the client to have sent a `progressToken` in the iniRead data from resources registered with your FastMCP server. This allows functions to access files, configuration, or dynamically generated content.```pythonasync def summarize_document(document_uri: str, ctx: Context) -> str:"""Summarize a document by its resource URI."""# Read the document content@@ -222,7 +222,7 @@ The returned content is typically accessed via `content_list[0].content` and canRequest the client's LLM to generate text based on provided messages. This is useful when your function needs to leverage the LLM's capabilities to process data or generate responses.```pythonasync def analyze_sentiment(text: str, ctx: Context) -> dict:"""Analyze the sentiment of a text using the client's LLM."""# Create a sampling prompt asking for sentiment analysis@@ -258,7 +258,7 @@ async def analyze_sentiment(text: str, ctx: Context) -> dict:When providing a simple string, it's treated as a user message. For more complex scenarios, you can provide a list of messages with different roles.```pythonasync def generate_example(concept: str, ctx: Context) -> str:"""Generate a Python code example for a given concept."""# Using a system prompt and a user message@@ -280,7 +280,7 @@ See [Client Sampling](/clients/client#llm-sampling) for more details on how clieAccess metadata about the current request and client.```pythonasync def request_info(ctx: Context) -> dict:"""Return information about the current request."""return {@@ -300,7 +300,7 @@ async def request_info(ctx: Context) -> dict:#### FastMCP Server and Sessions```pythonasync def advanced_tool(ctx: Context) -> str:"""Demonstrate advanced context access."""# Access the FastMCP server instance@@ -326,7 +326,7 @@ See the [HTTP Requests pattern](/patterns/http-requests) for more details.For web applications, you can access the underlying HTTP request:```pythonasync def handle_web_request(ctx: Context) -> dict:"""Access HTTP request information from the Starlette request."""request = ctx.get_http_request()diff --git a/docs/servers/fastmcp.mdx b/docs/servers/fastmcp.mdxindex 598d056b6..40808fb95 100644--- a/docs/servers/fastmcp.mdx+++ b/docs/servers/fastmcp.mdx@@ -47,7 +47,7 @@ FastMCP servers expose several types of components to the client:Tools are functions that the client can call to perform actions or access external systems.```pythondef multiply(a: float, b: float) -> float:"""Multiplies two numbers together."""return a * b@@ -106,7 +106,7 @@ from fastmcp import FastMCPmcp = FastMCP(name="MyServer")def greet(name: str) -> str:"""Greet a user by name."""return f"Hello, {name}!"@@ -145,7 +145,7 @@ import asynciomain = FastMCP(name="Main")sub = FastMCP(name="Sub")def hello():return "hi"@@ -216,7 +216,7 @@ def yaml_serializer(data):# Create a server with the custom serializermcp = FastMCP(name="MyServer", tool_serializer=yaml_serializer)def get_config():"""Returns configuration in YAML format."""return {"api_key": "abc123", "debug": True, "rate_limit": 100}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/docs/servers/tools.mdx b/docs/servers/tools.mdxindex 6b1a54356..00fa13252 100644--- a/docs/servers/tools.mdx+++ b/docs/servers/tools.mdx@@ -31,7 +31,7 @@ from fastmcp import FastMCPmcp = FastMCP(name="CalculatorServer")def add(a: int, b: int) -> int:"""Adds two integer numbers together."""return a + b@@ -61,7 +61,7 @@ Type annotations for parameters are essential for proper tool functionality. TheUse standard Python type annotations for parameters:```pythondef analyze_text(text: str,max_tokens: int = 100,@@ -79,7 +79,7 @@ You can provide additional metadata about parameters using Pydantic's `Field` clfrom typing import Annotatedfrom pydantic import Fielddef process_image(image_url: Annotated[str, Field(description="URL of the image to process")],resize: Annotated[bool, Field(description="Whether to resize the image")] = False,@@ -97,7 +97,7 @@ def process_image(You can also use the Field as a default value, though the Annotated approach is preferred:```pythondef search_database(query: str = Field(description="Search query string"),limit: int = Field(10, description="Maximum number of results", ge=1, le=100)@@ -137,7 +137,7 @@ For additional type annotations not listed here, see the [Parameter Types](#paraFastMCP follows Python's standard function parameter conventions. Parameters without default values are required, while those with default values are optional.```pythondef search_products(query: str, # Required - no default valuemax_results: int = 10, # Optional - has default value@@ -197,14 +197,14 @@ FastMCP seamlessly supports both standard (`def`) and asynchronous (`async def`)```python# Synchronous tool (suitable for CPU-bound or quick tasks)def calculate_distance(lat1: float, lon1: float, lat2: float, lon2: float) -> float:"""Calculate the distance between two coordinates."""# Implementation...return 42.5# Asynchronous tool (ideal for I/O-bound operations)async def fetch_weather(city: str) -> dict:"""Retrieve current weather conditions for a city."""# Use 'async def' for operations involving network calls, file I/O, etc.@@ -244,7 +244,7 @@ except ImportError:mcp = FastMCP("Image Demo")def generate_image(width: int, height: int, color: str) -> Image:"""Generates a solid color image."""# Create image using Pillow@@ -258,7 +258,7 @@ def generate_image(width: int, height: int, color: str) -> Image:# Return using FastMCP's Image helperreturn Image(data=img_bytes, format="png")def do_nothing() -> None:"""This tool performs an action but returns no data."""print("Performing a side effect...")@@ -285,7 +285,7 @@ mcp = FastMCP(name="SecureServer", mask_error_details=True)from fastmcp import FastMCPfrom fastmcp.exceptions import ToolErrordef divide(a: float, b: float) -> float:"""Divide a by b."""@@ -351,7 +351,7 @@ from fastmcp import FastMCP, Contextmcp = FastMCP(name="ContextDemo")async def process_data(data_uri: str, ctx: Context) -> dict:"""Process data from a resource with progress reporting."""await ctx.info(f"Processing data from {data_uri}")@@ -396,7 +396,7 @@ FastMCP supports **type coercion** when possible. This means that if a client seThe most common parameter types are Python's built-in scalar types:```pythondef process_values(name: str, # Text datacount: int, # Integer numbers@@ -416,7 +416,7 @@ FastMCP supports various date and time types from the `datetime` module:```pythonfrom datetime import datetime, date, timedeltadef process_date_time(event_date: date, # ISO format date string or date objectevent_time: datetime, # ISO format datetime string or datetime object@@ -440,7 +440,7 @@ def process_date_time(FastMCP supports all standard Python collection types:```pythondef analyze_data(values: list[float], # List of numbersproperties: dict[str, str], # Dictionary with string keys and values@@ -465,7 +465,7 @@ Collection types can be nested and combined to represent complex data structuresFor parameters that can accept multiple types or may be omitted:```pythondef flexible_search(query: str | int, # Can be either string or integerfilters: dict[str, str] | None = None, # Optional dictionary@@ -488,7 +488,7 @@ Literals constrain parameters to a specific set of values:```pythonfrom typing import Literaldef sort_data(data: list[float],order: Literal["ascending", "descending"] = "ascending",@@ -516,7 +516,7 @@ class Color(Enum):GREEN = "green"BLUE = "blue"def process_image(image_path: str,color_filter: Color = Color.RED@@ -539,7 +539,7 @@ There are two approaches to handling binary data in tool parameters:#### Bytes```pythondef process_binary(data: bytes):"""Process binary data directly.@@ -563,7 +563,7 @@ FastMCP does not automatically decode base64-encoded strings for bytes parameterfrom typing import Annotatedfrom pydantic import Fielddef process_image_data(image_data: Annotated[str, Field(description="Base64-encoded image data")]):@@ -587,7 +587,7 @@ The `Path` type from the `pathlib` module can be used for file system paths:```pythonfrom pathlib import Pathdef process_file(path: Path) -> str:"""Process a file at the given path."""assert isinstance(path, Path) # Path is properly converted@@ -603,7 +603,7 @@ The `UUID` type from the `uuid` module can be used for unique identifiers:```pythonimport uuiddef process_item(item_id: uuid.UUID # String UUID or UUID object) -> str:@@ -628,7 +628,7 @@ class User(BaseModel):age: int | None = Noneis_active: bool = Truedef create_user(user: User):"""Create a new user in the system."""# The input is automatically validated against the User model@@ -657,7 +657,7 @@ Note that fields can be used *outside* Pydantic models to provide metadata and vfrom typing import Annotatedfrom pydantic import Fielddef analyze_metrics(# Numbers with range constraintscount: Annotated[int, Field(ge=0, le=100)], # 0 <= count <= 100@@ -682,7 +682,7 @@ def analyze_metrics(You can also use `Field` as a default value, though the `Annotated` approach is preferred:```pythondef validate_data(# Value constraintsage: int = Field(ge=0, lt=120), # 0 <= age < 120@@ -727,7 +727,7 @@ mcp = FastMCP(on_duplicate_tools="error")def my_tool(): return "Version 1"# This will now raise a ValueError because 'my_tool' already exists@@ -754,7 +754,7 @@ from fastmcp import FastMCPmcp = FastMCP(name="DynamicToolServer")def calculate_sum(a: int, b: int) -> int:"""Add two numbers together."""return a + b