Repository navigation
Migrating from mcp to fastmcp
#3340
|
Hi, I was doing some personal POC to explore wghat is MCP and what are different features and created a repository here: github.com/yarnabrina/learn-model-context-protocol Now, I am curious to try out Will appreciate some guidance. |
Replies: 1 comment 1 reply
|
Great question — this tripped me up too when migrating. In FastMCP v2+ the from fastmcp import FastMCP
from fastmcp.tools import Tool
mcp = FastMCP("my-server")
def greet(name: str) -> str:
"""Say hello to someone."""
return f"Hello, {name}!"
# Create a FunctionTool from a plain function
tool = Tool.from_function(greet)
mcp.add_tool(tool)
tool = Tool.from_function(
greet,
name="say_hello", # override the inferred name
description="Custom desc", # override the docstring
tags={"greeting"},
timeout=5.0,
)
mcp.add_tool(tool)This is equivalent to: @mcp.tool(name="say_hello", description="Custom desc", tags={"greeting"}, timeout=5.0)
def greet(name: str) -> str:
...The decorator path is just sugar over |
Great question — this tripped me up too when migrating. In FastMCP v2+ the
add_toolmethod expects aToolobject (specifically aFunctionTool), not a bare callable. The easiest way to create one without using the@mcp.tool()decorator isTool.from_function():Tool.from_function()accepts all the same optional kwargs as the decorator:name,description,title,tags,annotations,timeout, etc.