about things
at main 79 lines 3.3 kB view raw view rendered
1# fastmcp 2 3a high-level python framework for building mcp servers. it aims to simplify development by abstracting protocol details, emphasizing developer experience and type safety. 4 5## philosophy 6 7fastmcp is designed to be a fast, simple, and complete solution for mcp development. unlike a low-level sdk, it provides a "pythonic" and declarative way to define mcp servers, handling complex protocol intricacies automatically. it leverages python's type hints and docstrings to generate mcp schemas (for tools, resources, and prompts) automatically. 8 9## basic usage 10 11define your mcp server and expose functionality using decorators: 12 13```python 14from fastmcp import FastMCP, Context 15 16# Initialize the MCP server 17mcp = FastMCP("my-awesome-server", description="A server for managing awesome things.") 18 19@mcp.tool() 20def add_numbers(a: int, b: int) -> int: 21 """Adds two numbers together. 22 23 :param a: The first number. 24 :param b: The second number. 25 :returns: The sum of the two numbers. 26 """ 27 return a + b 28 29@mcp.resource("config://app/settings") 30def get_app_settings() -> dict: 31 """Retrieves the current application settings.""" 32 return {"debug_mode": True, "log_level": "INFO"} 33 34@mcp.prompt 35def create_summary_prompt(text: str) -> str: 36 """Generates a prompt to summarize the given text.""" 37 return f"please summarize the following document:\n\n{text}" 38 39@mcp.tool() 40async def fetch_external_data(ctx: Context, url: str) -> str: 41 """Fetches data from an external URL. 42 43 :param ctx: The MCP context for logging. 44 :param url: The URL to fetch. 45 :returns: The content of the URL as a string. 46 """ 47 ctx.info(f"fetching data from {url}") 48 # In a real scenario, this would use an async http client 49 return f"content from {url}" 50 51# Run the server 52if __name__ == "__main__": 53 mcp.run() # Automatically detects and uses STDIO or HTTP transport 54``` 55 56## key features & patterns 57 58### decorator-based definitions 59tools, resources, and prompts are defined using simple python functions decorated with `@mcp.tool`, `@mcp.resource`, and `@mcp.prompt`. fastmcp infers schemas from type hints and docstrings. 60 61### context injection 62the `Context` object can be injected into tools, resources, or prompts, providing access to mcp session capabilities like logging (`ctx.info()`), sending progress updates (`ctx.session.send_progress()`), and making llm sampling requests (`ctx.session.sample()`) 63 64### flexible transports 65fastmcp abstracts the underlying transport. servers can run over stdio (for local integration) or streamable http (for remote deployments) with minimal code changes. 66 67### server composition & proxying 68fastmcp supports advanced patterns like combining multiple mcp servers into a single endpoint or proxying requests to other mcp servers. 69 70### authentication 71built-in support for various authentication mechanisms, including oauth providers and custom api keys. 72 73### client library 74provides a `fastmcp.Client` for programmatic interaction with any mcp server, supporting diverse transports and server-initiated llm sampling. 75 76## sources 77 78- [github.com/jlowin/fastmcp](https://github.com/jlowin/fastmcp) - fastmcp source code and documentation 79- [pypi.org/project/fastmcp](https://pypi.org/project/fastmcp/) - fastmcp package on pypi