commit a3fced51dfc5e600d6e304711767755556b0361b Author: Jeremiah Lowin <153965+jlowin@users.noreply.github.com> Date: Tue Sep 30 16:38:51 2025 -0400 Improve env vars for marvin (#1972) diff --git a/.ccignore b/.ccignore new file mode 100644 index 000000000..14561791f --- /dev/null +++ b/.ccignore @@ -0,0 +1,7 @@ +.pre-commit-config.yaml +.github/ +docs/changelog.mdx +docs/python-sdk/ +examples/ +src/fastmcp/contrib/ +tests/contrib/ diff --git a/.cursor/rules/core-mcp-objects.mdc b/.cursor/rules/core-mcp-objects.mdc new file mode 100644 index 000000000..c8cc92818 --- /dev/null +++ b/.cursor/rules/core-mcp-objects.mdc @@ -0,0 +1,13 @@ +--- +description: +globs: +alwaysApply: true +--- +There are four major MCP object types: + +- Tools (src/tools/) +- Resources (src/resources/) +- Resource Templates (src/resources/) +- Prompts (src/prompts) + +While these have slightly different semantics and implementations, in general changes that affect interactions with any one (like adding tags, importing, etc.) will need to be adopted, applied, and tested on all others. Be sure to look at not only the object definition but also the related `Manager` (e.g. `ToolManager`, `ResourceManager`, and `PromptManager`). Also note that while resources and resource templates are different objects, they both are handled by the `ResourceManager`. \ No newline at end of file diff --git a/.github/ISSUE_TEMPLATE/bug.yml b/.github/ISSUE_TEMPLATE/bug.yml new file mode 100644 index 000000000..3d9a53394 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug.yml @@ -0,0 +1,71 @@ +name: ๐Ÿ› Bug Report +description: Report a bug or unexpected behavior in FastMCP +labels: [bug, pending] + +body: + - type: markdown + attributes: + value: Thanks for contributing to FastMCP! ๐Ÿ™ + + - type: markdown + attributes: + value: | + ### Before you submit + + To help us help you, please: + + - ๐Ÿ”„ **Make sure you're testing on the latest version of FastMCP** - many issues are already fixed in newer versions + - ๐Ÿ” **Check if someone else has already reported this issue** or if it's been fixed on the main branch + - ๐Ÿ“‹ **You MUST include a copy/pasteable and properly formatted MRE** (minimal reproducible example) below or your issue may be closed without response + + Thanks for helping to make FastMCP better! ๐Ÿš€ + + - type: textarea + id: description + attributes: + label: Description + description: | + Please explain what you're experiencing and what you would expect to happen instead. + + Provide as much detail as possible to help us understand and solve your problem quickly. + validations: + required: true + + - type: textarea + id: example + attributes: + label: Example Code + description: > + If applicable, please provide a self-contained, + [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) + demonstrating the bug. If possible, your example should be a single-file script. + + placeholder: | + import asyncio + from fastmcp import FastMCP, Client + + mcp = FastMCP() + + async def demo(): + async with Client(mcp) as client: + ... # show the bug here + + if __name__ == "__main__": + asyncio.run(demo()) + render: Python + + - type: textarea + id: version + attributes: + label: Version Information + description: | + Please tell us about your FastMCP version, MCP version, Python version, and OS, as well as any other relevant details about your environment. + + To get the basic information, run the following command in your terminal and paste the output below: + + ```bash + fastmcp version --copy + ``` + render: Text + validations: + required: true diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 000000000..7c4bef52e --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,8 @@ +blank_issues_enabled: false +contact_links: + - name: FastMCP Documentation + url: https://gofastmcp.com + about: Please review the documentation before opening an issue. + - name: MCP Python SDK + url: https://github.com/modelcontextprotocol/python-sdk/issues + about: Issues related to the low-level MCP Python SDK, including the FastMCP 1.0 module that is included in the `mcp` package, should be filed on the official MCP repository. diff --git a/.github/ISSUE_TEMPLATE/enhancement.yml b/.github/ISSUE_TEMPLATE/enhancement.yml new file mode 100644 index 000000000..43b8b4de9 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/enhancement.yml @@ -0,0 +1,34 @@ +name: ๐Ÿ’ก Enhancement Request +description: Suggest an idea or improvement for FastMCP +labels: [enhancement, pending] + +body: + - type: markdown + attributes: + value: Thanks for contributing to FastMCP! ๐Ÿ™ + + - type: markdown + attributes: + value: | + ### Before you submit + + To help us evaluate your enhancement request: + + - ๐Ÿ” **Check if this has already been requested** - search existing issues first + - ๐Ÿ’ญ **Think about the broader impact** - how would this affect other users? + - ๐Ÿ“‹ **Consider implementation complexity** - is this a small change or a major feature? + + Thanks for helping to make FastMCP better! ๐Ÿš€ + + - type: textarea + id: description + attributes: + label: Enhancement + description: | + Please describe the enhancement: + + - What problem or use case would it solve? + - How would it improve your workflow or experience with FastMCP? + - Are there any alternative solutions you've considered? + validations: + required: true diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md new file mode 120000 index 000000000..be77ac83a --- /dev/null +++ b/.github/copilot-instructions.md @@ -0,0 +1 @@ +../AGENTS.md \ No newline at end of file diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 000000000..d7be3b7fc --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,20 @@ +version: 2 +updates: + - package-ecosystem: "uv" + directory: "/" + schedule: + interval: "daily" + labels: + - "dependencies" + - package-ecosystem: "pip" + directory: "/" + schedule: + interval: "daily" + labels: + - "dependencies" + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" + labels: + - "dependencies" diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 000000000..68212c72f --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,28 @@ +## Description + + +**Contributors Checklist** + + +- [ ] My change closes #(issue number) +- [ ] I have followed the repository's development workflow +- [ ] I have tested my changes manually and by adding relevant tests +- [ ] I have performed all required documentation updates + +**Review Checklist** + + +- [ ] I have self-reviewed my changes +- [ ] My Pull Request is ready for review + +--- diff --git a/.github/release.yml b/.github/release.yml new file mode 100644 index 000000000..5ff95aace --- /dev/null +++ b/.github/release.yml @@ -0,0 +1,47 @@ +changelog: + exclude: + labels: + - ignore in release notes + + categories: + - title: New Features ๐ŸŽ‰ + labels: + - feature + + - title: Enhancements ๐Ÿ”ง + labels: + - enhancement + exclude: + labels: + - breaking change + + - title: Fixes ๐Ÿž + labels: + - bug + exclude: + labels: + - contrib + + - title: Breaking Changes ๐Ÿ›ซ + labels: + - breaking change + exclude: + labels: + - contrib + + - title: Docs ๐Ÿ“š + labels: + - documentation + + - title: Examples & Contrib ๐Ÿ’ก + labels: + - example + - contrib + + - title: Dependencies ๐Ÿ“ฆ + labels: + - dependencies + + - title: Other Changes ๐Ÿฆพ + labels: + - "*" diff --git a/.github/workflows/auto-close-duplicates.yml b/.github/workflows/auto-close-duplicates.yml new file mode 100644 index 000000000..03bef97fb --- /dev/null +++ b/.github/workflows/auto-close-duplicates.yml @@ -0,0 +1,28 @@ +name: Auto-close duplicate issues +description: Auto-closes issues that are duplicates of existing issues +on: + schedule: + - cron: "0 9 * * *" # Run daily at 9 AM UTC + workflow_dispatch: + +jobs: + auto-close-duplicates: + runs-on: ubuntu-latest + timeout-minutes: 10 + permissions: + contents: read + issues: write + + steps: + - name: Checkout repository + uses: actions/checkout@v5 + + - name: Install uv + uses: astral-sh/setup-uv@v6 + + - name: Auto-close duplicate issues + run: uv run scripts/auto_close_duplicates.py + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITHUB_REPOSITORY_OWNER: ${{ github.repository_owner }} + GITHUB_REPOSITORY_NAME: ${{ github.event.repository.name }} diff --git a/.github/workflows/marvin-dedupe-issues.yml b/.github/workflows/marvin-dedupe-issues.yml new file mode 100644 index 000000000..3da916bf2 --- /dev/null +++ b/.github/workflows/marvin-dedupe-issues.yml @@ -0,0 +1,92 @@ +name: Marvin Issue Dedupe +description: Automatically dedupe GitHub issues using Marvin +on: + issues: + types: [opened] + workflow_dispatch: + inputs: + issue_number: + description: "Issue number to process for duplicate detection" + required: true + type: string + +jobs: + marvin-dedupe-issues: + runs-on: ubuntu-latest + timeout-minutes: 10 + permissions: + contents: read + issues: write + id-token: write + + steps: + - name: Checkout repository + uses: actions/checkout@v5 + + - name: Generate Marvin App token + id: marvin-token + uses: actions/create-github-app-token@v2 + with: + app-id: ${{ secrets.MARVIN_APP_ID }} + private-key: ${{ secrets.MARVIN_APP_PRIVATE_KEY }} + + - name: Set dedupe prompt + id: dedupe-prompt + run: | + cat >> $GITHUB_OUTPUT << 'EOF' + PROMPT<> $GITHUB_OUTPUT << 'EOF' + PROMPT<" + committer: "marvin-context-protocol[bot] <225465937+marvin-context-protocol[bot]@users.noreply.github.com>" + + - name: Summary + run: | + echo "โœ… Config schema generation workflow completed" + echo "PR will be created if there are changes, or closed if schema is already up to date" diff --git a/.github/workflows/update-sdk-docs.yml b/.github/workflows/update-sdk-docs.yml new file mode 100644 index 000000000..83ffe7859 --- /dev/null +++ b/.github/workflows/update-sdk-docs.yml @@ -0,0 +1,74 @@ +name: Update SDK Documentation + +# This workflow runs on merges to main to automatically update SDK docs +# by creating a PR when changes are needed. + +on: + push: + branches: ["main"] + paths: + - "src/**" + - "pyproject.toml" + workflow_dispatch: + +permissions: + contents: write + pull-requests: write + +jobs: + update-sdk-docs: + timeout-minutes: 5 + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v5 + + - name: Generate Marvin App token + id: marvin-token + uses: actions/create-github-app-token@v2 + with: + app-id: ${{ secrets.MARVIN_APP_ID }} + private-key: ${{ secrets.MARVIN_APP_PRIVATE_KEY }} + + - name: Install uv + uses: astral-sh/setup-uv@v6 + with: + enable-cache: true + cache-dependency-glob: "uv.lock" + + - name: Install dependencies + run: uv sync --python 3.12 + + - name: Install just + uses: extractions/setup-just@v3 + + - name: Generate SDK documentation + run: | + echo "๐Ÿ”„ Generating SDK documentation..." + just api-ref-all + + - name: Create Pull Request + uses: peter-evans/create-pull-request@v7 + with: + token: ${{ steps.marvin-token.outputs.token }} + commit-message: "chore: Update SDK documentation" + title: "chore: Update SDK documentation" + body: | + This PR updates the auto-generated SDK documentation to reflect the latest source code changes. + + ๐Ÿ“š Documentation is automatically generated from the source code docstrings and type annotations. + + **Note:** This PR is fully automated and will update itself with any subsequent changes to the SDK, or close automatically if the documentation becomes up-to-date through other means. Feel free to leave it open until you're ready to merge. + + ๐Ÿค– Generated by Marvin + branch: marvin/update-sdk-docs + labels: | + ignore in release notes + delete-branch: true + author: "marvin-context-protocol[bot] <225465937+marvin-context-protocol[bot]@users.noreply.github.com>" + committer: "marvin-context-protocol[bot] <225465937+marvin-context-protocol[bot]@users.noreply.github.com>" + + - name: Summary + run: | + echo "โœ… SDK documentation generation workflow completed" + echo "PR will be created if there are changes, or closed if documentation is already up to date" diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..887877553 --- /dev/null +++ b/.gitignore @@ -0,0 +1,76 @@ +# Python-generated files +__pycache__/ +*.py[cod] +*$py.class +build/ +dist/ +wheels/ +*.egg-info/ +*.egg +MANIFEST +.pytest_cache/ +.coverage +htmlcov/ +.tox/ +nosetests.xml +coverage.xml +*.cover + +# Virtual environments +.venv +venv/ +env/ +ENV/ +.env + +# System files +.DS_Store + +# Version file +src/fastmcp/_version.py + +# Editors and IDEs +.cursorrules +.vscode/ +.idea/ +*.swp +*.swo +*~ +.project +.pydevproject +.settings/ + +# Jupyter Notebook +.ipynb_checkpoints + +# Type checking +.mypy_cache/ +.dmypy.json +dmypy.json +.pyre/ +.pytype/ + +# Local development +.python-version +.envrc +.direnv/ + +# Logs and databases +*.log +*.sqlite +*.db +*.ddb + +# Claude worktree management +.claude-wt/worktrees + +# Agents +/PLAN.md +/TODO.md +/STATUS.md + +# Common FastMCP test files +/test.py +/server.py +/client.py +/test.json diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 000000000..548696b55 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,48 @@ +fail_fast: false + +repos: + - repo: https://github.com/abravalheri/validate-pyproject + rev: v0.24.1 + hooks: + - id: validate-pyproject + + - repo: https://github.com/pre-commit/mirrors-prettier + rev: v3.1.0 + hooks: + - id: prettier + types_or: [yaml, json5] + + - repo: https://github.com/astral-sh/ruff-pre-commit + # Ruff version. + rev: v0.12.1 + hooks: + # Run the linter. + - id: ruff-check + args: [--fix, --exit-non-zero-on-fix] + # Run the formatter. + - id: ruff-format + + - repo: local + hooks: + - id: ty + name: ty check + entry: uv run ty check + language: system + types: [python] + files: ^src/|^tests/ + pass_filenames: false + require_serial: true + + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v6.0.0 + hooks: + - id: no-commit-to-branch + name: prevent commits to main + args: [--branch, main] + + - repo: https://github.com/codespell-project/codespell + rev: v2.4.1 + hooks: + - id: codespell # See pyproject.toml for args + additional_dependencies: + - tomli diff --git a/.python-version b/.python-version new file mode 100644 index 000000000..e4fba2183 --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +3.12 diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 000000000..cdb5bee7a --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,265 @@ +# FastMCP Development Guidelines + +> **Audience**: LLM-driven engineering agents and human developers + +FastMCP is a comprehensive Python framework (Python โ‰ฅ3.10) for building Model Context Protocol (MCP) servers and clients. This is the actively maintained v2.0 providing a complete toolkit for the MCP ecosystem. + +## Required Development Workflow + +**CRITICAL**: Always run these commands in sequence before committing: + +```bash +uv sync # Install dependencies +uv run pre-commit run --all-files # Ruff + Prettier + ty +uv run pytest # Run full test suite +``` + +**All three must pass** - this is enforced by CI. Alternative: `just build && just typecheck && just test` + +**Tests must pass and lint/typing must be clean before committing.** + +## Repository Structure + +| Path | Purpose | +| ------------------ | --------------------------------------------------- | +| `src/fastmcp/` | Library source code (Python โ‰ฅ 3.10) | +| `โ”œโ”€server/` | Server implementation, `FastMCP`, auth, networking | +| `โ”‚ โ”œโ”€auth/` | Authentication providers (Bearer, JWT, WorkOS) | +| `โ”‚ โ””โ”€middleware/` | Error handling, logging, rate limiting | +| `โ”œโ”€client/` | High-level client SDK + transports | +| `โ”‚ โ””โ”€auth/` | Client authentication (Bearer, OAuth) | +| `โ”œโ”€tools/` | Tool implementations + `ToolManager` | +| `โ”œโ”€resources/` | Resources, templates + `ResourceManager` | +| `โ”œโ”€prompts/` | Prompt templates + `PromptManager` | +| `โ”œโ”€cli/` | FastMCP CLI commands (`run`, `dev`, `install`) | +| `โ”œโ”€contrib/` | Community contributions (bulk caller, mixins) | +| `โ”œโ”€experimental/` | Experimental features (new OpenAPI parser) | +| `โ””โ”€utilities/` | Shared utilities (logging, JSON schema, HTTP) | +| `tests/` | Comprehensive pytest suite with markers | +| `docs/` | Mintlify documentation (published to gofastmcp.com) | +| `examples/` | Runnable demo servers (echo, smart_home, atproto) | + +## Core MCP Objects + +When modifying MCP functionality, changes typically need to be applied across all object types: + +- **Tools** (`src/tools/` + `ToolManager`) +- **Resources** (`src/resources/` + `ResourceManager`) +- **Resource Templates** (`src/resources/` + `ResourceManager`) +- **Prompts** (`src/prompts/` + `PromptManager`) + +## Writing Style + +- Be brief and to the point. Do not regurgitate information that can easily be gleaned from the code, except to guide the reader to where the code is located. +- **NEVER** use "This isn't..." or "not just..." constructions. State what something IS directly. Avoid defensive writing patterns like: + - "This isn't X, it's Y" or "Not just X, but Y" โ†’ Just say "This is Y" + - "Not just about X" โ†’ State the actual purpose + - "We're not doing X, we're doing Y" โ†’ Just explain what you're doing + - Any variation of explaining what something isn't before what it is + +## Testing Best Practices + +### Testing Standards + +- Every test: atomic, self-contained, single functionality +- Use parameterization for multiple examples of same functionality +- Use separate tests for different functionality pieces +- **ALWAYS** Put imports at the top of the file, not in the test body +- **NEVER** add `@pytest.mark.asyncio` to tests - `asyncio_mode = "auto"` is set globally +- **ALWAYS** run pytest after significant changes + +### Inline Snapshots + +FastMCP uses `inline-snapshot` for testing complex data structures. On first run with empty `snapshot()`, pytest will auto-populate the expected value when running `pytest --inline-snapshot=create`. To update snapshots after intentional changes, run `pytest --inline-snapshot=fix`. This is particularly useful for testing JSON schemas and API responses. + +### Always Use In-Memory Transport + +Pass FastMCP servers directly to clients for testing: + +```python +mcp = FastMCP("TestServer") + +@mcp.tool +def greet(name: str) -> str: + return f"Hello, {name}!" + +# Direct connection - no network complexity +async with Client(mcp) as client: + result = await client.call_tool("greet", {"name": "World"}) +``` + +Only use HTTP transport when explicitly testing network features: + +```python +# Network testing only +async with Client(transport=StreamableHttpTransport(server_url)) as client: + result = await client.ping() +``` + +## Development Rules + +### Git & CI + +- Pre-commit hooks are required (run automatically on commits) +- Never amend commits to fix pre-commit failures +- Apply PR labels: bugs/breaking/enhancements/features +- Improvements = enhancements (not features) unless specified +- **NEVER** force-push on collaborative repos +- **ALWAYS** run pre-commit before PRs + +### Commit Messages and Agent Attribution + +- **Agents NOT acting on behalf of @jlowin MUST identify themselves** (e.g., "๐Ÿค– Generated with Claude Code" in commits/PRs) +- Keep commit messages brief - ideally just headlines, not detailed messages +- Focus on what changed, not how or why +- Always read issue comments for follow-up information (treat maintainers as authoritative) + +### PR Messages - Required Structure + +- 1-2 paragraphs: problem/tension + solution (PRs are documentation!) +- Focused code example showing key capability +- **Avoid:** bullet summaries, exhaustive change lists, verbose closes/fixes, marketing language +- **Do:** Be opinionated about why change matters, show before/after scenarios +- Minor fixes: keep body short and concise +- No "test plan" sections or testing summaries + +### Code Standards + +- Python โ‰ฅ 3.10 with full type annotations +- Follow existing patterns and maintain consistency +- **Prioritize readable, understandable code** - clarity over cleverness +- Avoid obfuscated or confusing patterns even if they're shorter +- Use `# type: ignore[attr-defined]` in tests for MCP results instead of type assertions +- Each feature needs corresponding tests + +### Documentation + +- Uses Mintlify framework +- Files must be in docs.json to be included +- Never modify `docs/python-sdk/**` (auto-generated) +- **Core Principle:** A feature doesn't exist unless it is documented! + +### Documentation Guidelines + +- **Code Examples:** Explain before showing code, make blocks fully runnable (include imports) +- **Structure:** Headers form navigation guide, logical H2/H3 hierarchy +- **Content:** User-focused sections, motivate features (why) before mechanics (how) +- **Style:** Prose over code comments for important information + +## Code Review Guidelines + +### Philosophy + +Code review is about maintaining a healthy codebase while helping contributors succeed. The burden of proof is on the PR to demonstrate it adds value in the intended way. Your job is to help it get there through actionable feedback. + +**Critical**: A perfectly written PR that adds unwanted functionality must still be rejected. The code must advance the codebase in the intended direction, not just be well-written. When rejecting, provide clear guidance on how to align with project goals. + +Be friendly and welcoming while maintaining high standards. Call out what works well - this reinforces good patterns. When code needs improvement, be specific about why and how to fix it. Remember that PRs serve as documentation for future developers. + +### Focus On + +- **Does this advance the codebase in the intended direction?** (Even perfect code for unwanted features should be rejected) +- **API design and naming clarity** - Identify confusing patterns (e.g., parameter values that contradict defaults) or non-idiomatic code (mutable defaults, etc.). Contributed code will need to be maintained indefinitely, and by someone other than the author (unless the author is a maintainer). +- **Suggest specific improvements**, not generic "add more tests" comments +- **Think about API ergonomics and learning curve** from a user perspective + +### For Agent Reviewers + +- **Read the full context**: Always examine related files, tests, and documentation before reviewing +- **Check against established patterns**: Look for consistency with existing codebase conventions +- **Verify functionality claims**: Don't just read code - understand what it actually does +- **Consider edge cases**: Think through error conditions and boundary scenarios + +### Avoid + +- Generic feedback without specifics +- Hypothetical problems unlikely to occur +- Nitpicking organizational choices without strong reason +- Summarizing what the PR already describes +- Star ratings or excessive emojis +- Bikeshedding style preferences when functionality is correct +- Requesting changes without suggesting solutions +- Focusing on personal coding style over project conventions + +### Tone + +- Acknowledge good decisions ("This API design is clean") +- Be direct but respectful +- Explain impact ("This will confuse users because...") +- Remember: Someone else maintains this code forever + +### Decision Framework + +Before approving, ask yourself: + +1. Does this PR achieve its stated purpose? +2. Is that purpose aligned with where the codebase should go? +3. Would I be comfortable maintaining this code? +4. Have I actually understood what it does, not just what it claims? +5. Does this change introduce technical debt? + +If something needs work, your review should help it get there through specific, actionable feedback. If it's solving the wrong problem, say so clearly. + +### Review Comment Examples + +**Good Review Comments:** + +โŒ "Add more tests" +โœ… "The `handle_timeout` method needs tests for the edge case where timeout=0" + +โŒ "This API is confusing" +โœ… "The parameter name `data` is ambiguous - consider `message_content` to match the MCP specification" + +โŒ "This could be better" +โœ… "This approach works but creates a circular dependency. Consider moving the validation to `utils/validators.py`" + +### Review Checklist + +Before approving, verify: + +- [ ] All required development workflow steps completed (uv sync, pre-commit, pytest) +- [ ] Changes align with repository patterns and conventions +- [ ] API changes are documented and backwards-compatible where possible +- [ ] Error handling follows project patterns (specific exception types) +- [ ] Tests cover new functionality and edge cases + +## Key Tools & Commands + +### Environment Setup + +```bash +git clone +cd fastmcp +uv sync # Installs all deps including dev tools +``` + +### Validation Commands (Run Frequently) + +- **Linting**: `uv run ruff check` (or with `--fix`) +- **Type Checking**: `uv run ty check` +- **All Checks**: `uv run pre-commit run --all-files` + +### Testing + +- **Standard**: `uv run pytest` +- **Integration**: `uv run pytest -m "integration"` +- **Excluding markers**: `uv run pytest -m "not integration and not client_process"` + +### CLI Usage + +- **Run server**: `uv run fastmcp run server.py` +- **Inspect server**: `uv run fastmcp inspect server.py` + +## Critical Patterns + +### Error Handling + +- Never use bare `except` - be specific with exception types +- Use `# type: ignore[attr-defined]` in tests for MCP results + +### Build Issues (Common Solutions) + +1. **Dependencies**: Always `uv sync` first +2. **Pre-commit fails**: Run `uv run pre-commit run --all-files` to see failures +3. **Type errors**: Use `uv run ty check` directly, check `pyproject.toml` config +4. **Test timeouts**: Default 3s - optimize or mark as integration tests diff --git a/CLAUDE.md b/CLAUDE.md new file mode 120000 index 000000000..47dc3e3d8 --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1 @@ +AGENTS.md \ No newline at end of file diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 000000000..026b0a2cd --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,128 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our +community a harassment-free experience for everyone, regardless of age, body +size, visible or invisible disability, ethnicity, sex characteristics, gender +identity and expression, level of experience, education, socio-economic status, +nationality, personal appearance, race, religion, or sexual identity +and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, +diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our +community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, + and learning from the experience +* Focusing on what is best not just for us as individuals, but for the + overall community + +Examples of unacceptable behavior include: + +* The use of sexualized language or imagery, and sexual attention or + advances of any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email + address, without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of +acceptable behavior and will take appropriate and fair corrective action in +response to any behavior that they deem inappropriate, threatening, offensive, +or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, and will communicate reasons for moderation +decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when +an individual is officially representing the community in public spaces. +Examples of representing our community include using an official e-mail address, +posting via an official social media account, or acting as an appointed +representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported to the community leaders responsible for enforcement at +chris@prefect.io. +All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the +reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining +the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed +unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing +clarity around the nature of the violation and an explanation of why the +behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series +of actions. + +**Consequence**: A warning with consequences for continued behavior. No +interaction with the people involved, including unsolicited interaction with +those enforcing the Code of Conduct, for a specified period of time. This +includes avoiding interactions in community spaces as well as external channels +like social media. Violating these terms may lead to a temporary or +permanent ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including +sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public +communication with the community for a specified period of time. No public or +private interaction with the people involved, including unsolicited interaction +with those enforcing the Code of Conduct, is allowed during this period. +Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community +standards, including sustained inappropriate behavior, harassment of an +individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within +the community. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], +version 2.0, available at +https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. + +Community Impact Guidelines were inspired by [Mozilla's code of conduct +enforcement ladder](https://github.com/mozilla/diversity). + +[homepage]: https://www.contributor-covenant.org + +For answers to common questions about this code of conduct, see the FAQ at +https://www.contributor-covenant.org/faq. Translations are available at +https://www.contributor-covenant.org/translations. diff --git a/LICENSE b/LICENSE new file mode 100644 index 000000000..f49a4e16e --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 000000000..e96b243e4 --- /dev/null +++ b/README.md @@ -0,0 +1,507 @@ +
+ + + + + + + FastMCP Logo + + +# FastMCP v2 ๐Ÿš€ + +The fast, Pythonic way to build MCP servers and clients. + +*Made with โ˜•๏ธ by [Prefect](https://www.prefect.io/)* + +[![Docs](https://img.shields.io/badge/docs-gofastmcp.com-blue)](https://gofastmcp.com) +[![PyPI - Version](https://img.shields.io/pypi/v/fastmcp.svg)](https://pypi.org/project/fastmcp) +[![Tests](https://github.com/jlowin/fastmcp/actions/workflows/run-tests.yml/badge.svg)](https://github.com/jlowin/fastmcp/actions/workflows/run-tests.yml) +[![License](https://img.shields.io/github/license/jlowin/fastmcp.svg)](https://github.com/jlowin/fastmcp/blob/main/LICENSE) + +jlowin%2Ffastmcp | Trendshift +
+ +> [!Note] +> +> #### FastMCP 2.0: The Standard Framework +> +> FastMCP pioneered Python MCP development, and FastMCP 1.0 was incorporated into the [official MCP SDK](https://github.com/modelcontextprotocol/python-sdk) in 2024. +> +> **This is FastMCP 2.0** โ€” the actively maintained, production-ready framework that extends far beyond basic protocol implementation. While the SDK provides core functionality, FastMCP 2.0 delivers everything needed for production: advanced MCP patterns (server composition, proxying, OpenAPI/FastAPI generation, tool transformation), enterprise auth (Google, GitHub, WorkOS, Azure, Auth0, and more), deployment tools, testing utilities, and comprehensive client libraries. +> +> **For production MCP applications, install FastMCP:** `pip install fastmcp` + +--- + +**FastMCP is the standard framework for building MCP applications**, providing the fastest path from idea to production. + +The [Model Context Protocol (MCP)](https://modelcontextprotocol.io) is a standardized way to provide context and tools to LLMs. FastMCP makes building production-ready MCP servers simple, with enterprise auth, deployment tools, and a complete ecosystem built in. + +```python +# server.py +from fastmcp import FastMCP + +mcp = FastMCP("Demo ๐Ÿš€") + +@mcp.tool +def add(a: int, b: int) -> int: + """Add two numbers""" + return a + b + +if __name__ == "__main__": + mcp.run() +``` + +Run the server locally: + +```bash +fastmcp run server.py +``` + +### ๐Ÿ“š Documentation + +FastMCP's complete documentation is available at **[gofastmcp.com](https://gofastmcp.com)**, including detailed guides, API references, and advanced patterns. This readme provides only a high-level overview. + +Documentation is also available in [llms.txt format](https://llmstxt.org/), which is a simple markdown standard that LLMs can consume easily. + +There are two ways to access the LLM-friendly documentation: + +- [`llms.txt`](https://gofastmcp.com/llms.txt) is essentially a sitemap, listing all the pages in the documentation. +- [`llms-full.txt`](https://gofastmcp.com/llms-full.txt) contains the entire documentation. Note this may exceed the context window of your LLM. + +--- + + +## Table of Contents + +- [FastMCP v2 ๐Ÿš€](#fastmcp-v2-) + - [๐Ÿ“š Documentation](#-documentation) + - [What is MCP?](#what-is-mcp) + - [Why FastMCP?](#why-fastmcp) + - [Installation](#installation) + - [Core Concepts](#core-concepts) + - [The `FastMCP` Server](#the-fastmcp-server) + - [Tools](#tools) + - [Resources \& Templates](#resources--templates) + - [Prompts](#prompts) + - [Context](#context) + - [MCP Clients](#mcp-clients) + - [Authentication](#authentication) + - [Enterprise Authentication, Zero Configuration](#enterprise-authentication-zero-configuration) + - [Deployment](#deployment) + - [From Development to Production](#from-development-to-production) + - [Advanced Features](#advanced-features) + - [Proxy Servers](#proxy-servers) + - [Composing MCP Servers](#composing-mcp-servers) + - [OpenAPI \& FastAPI Generation](#openapi--fastapi-generation) + - [Running Your Server](#running-your-server) + - [Contributing](#contributing) + - [Prerequisites](#prerequisites) + - [Setup](#setup) + - [Unit Tests](#unit-tests) + - [Static Checks](#static-checks) + - [Pull Requests](#pull-requests) + +--- + +## What is MCP? + +The [Model Context Protocol (MCP)](https://modelcontextprotocol.io) lets you build servers that expose data and functionality to LLM applications in a secure, standardized way. It is often described as "the USB-C port for AI", providing a uniform way to connect LLMs to resources they can use. It may be easier to think of it as an API, but specifically designed for LLM interactions. MCP servers can: + +- Expose data through **Resources** (think of these sort of like GET endpoints; they are used to load information into the LLM's context) +- Provide functionality through **Tools** (sort of like POST endpoints; they are used to execute code or otherwise produce a side effect) +- Define interaction patterns through **Prompts** (reusable templates for LLM interactions) +- And more! + +FastMCP provides a high-level, Pythonic interface for building, managing, and interacting with these servers. + +## Why FastMCP? + +FastMCP handles all the complex protocol details so you can focus on building. In most cases, decorating a Python function is all you need โ€” FastMCP handles the rest. + +๐Ÿš€ **Fast:** High-level interface means less code and faster development + +๐Ÿ€ **Simple:** Build MCP servers with minimal boilerplate + +๐Ÿ **Pythonic:** Feels natural to Python developers + +๐Ÿ” **Complete:** Everything for production โ€” enterprise auth (Google, GitHub, Azure, Auth0, WorkOS), deployment tools, testing frameworks, client libraries, and more + +FastMCP provides the shortest path from idea to production. Deploy locally, to the cloud with [FastMCP Cloud](https://fastmcp.cloud), or to your own infrastructure. + +## Installation + +We recommend installing FastMCP with [uv](https://docs.astral.sh/uv/): + +```bash +uv pip install fastmcp +``` + +For full installation instructions, including verification, upgrading from the official MCPSDK, and developer setup, see the [**Installation Guide**](https://gofastmcp.com/getting-started/installation). + +## Core Concepts + +These are the building blocks for creating MCP servers and clients with FastMCP. + +### The `FastMCP` Server + +The central object representing your MCP application. It holds your tools, resources, and prompts, manages connections, and can be configured with settings like authentication. + +```python +from fastmcp import FastMCP + +# Create a server instance +mcp = FastMCP(name="MyAssistantServer") +``` + +Learn more in the [**FastMCP Server Documentation**](https://gofastmcp.com/servers/fastmcp). + +### Tools + +Tools 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 or audio aided by the FastMCP media helper classes. + +```python +@mcp.tool +def multiply(a: float, b: float) -> float: + """Multiplies two numbers.""" + return a * b +``` + +Learn more in the [**Tools Documentation**](https://gofastmcp.com/servers/tools). + +### Resources & Templates + +Resources expose read-only data sources (like `GET` requests). Use `@mcp.resource("your://uri")`. Use `{placeholders}` in the URI to create dynamic templates that accept parameters, allowing clients to request specific data subsets. + +```python +# Static resource +@mcp.resource("config://version") +def get_version(): + return "2.0.1" + +# Dynamic resource template +@mcp.resource("users://{user_id}/profile") +def get_profile(user_id: int): + # Fetch profile for user_id... + return {"name": f"User {user_id}", "status": "active"} +``` + +Learn more in the [**Resources & Templates Documentation**](https://gofastmcp.com/servers/resources). + +### Prompts + +Prompts define reusable message templates to guide LLM interactions. Decorate functions with `@mcp.prompt`. Return strings or `Message` objects. + +```python +@mcp.prompt +def summarize_request(text: str) -> str: + """Generate a prompt asking for a summary.""" + return f"Please summarize the following text:\n\n{text}" +``` + +Learn more in the [**Prompts Documentation**](https://gofastmcp.com/servers/prompts). + +### Context + +Access MCP session capabilities within your tools, resources, or prompts by adding a `ctx: Context` parameter. Context provides methods for: + +- **Logging:** Log messages to MCP clients with `ctx.info()`, `ctx.error()`, etc. +- **LLM Sampling:** Use `ctx.sample()` to request completions from the client's LLM. +- **HTTP Request:** Use `ctx.http_request()` to make HTTP requests to other servers. +- **Resource Access:** Use `ctx.read_resource()` to access resources on the server +- **Progress Reporting:** Use `ctx.report_progress()` to report progress to the client. +- and more... + +To access the context, add a parameter annotated as `Context` to any mcp-decorated function. FastMCP will automatically inject the correct context object when the function is called. + +```python +from fastmcp import FastMCP, Context + +mcp = FastMCP("My MCP Server") + +@mcp.tool +async def process_data(uri: str, ctx: Context): + # Log a message to the client + await ctx.info(f"Processing {uri}...") + + # Read a resource from the server + data = await ctx.read_resource(uri) + + # Ask client LLM to summarize the data + summary = await ctx.sample(f"Summarize: {data.content[:500]}") + + # Return the summary + return summary.text +``` + +Learn more in the [**Context Documentation**](https://gofastmcp.com/servers/context). + +### MCP Clients + +Interact with *any* MCP server programmatically using the `fastmcp.Client`. It supports various transports (Stdio, SSE, In-Memory) and often auto-detects the correct one. The client can also handle advanced patterns like server-initiated **LLM sampling requests** if you provide an appropriate handler. + +Critically, the client allows for efficient **in-memory testing** of your servers by connecting directly to a `FastMCP` server instance via the `FastMCPTransport`, eliminating the need for process management or network calls during tests. + +```python +from fastmcp import Client + +async def main(): + # Connect via stdio to a local script + async with Client("my_server.py") as client: + tools = await client.list_tools() + print(f"Available tools: {tools}") + result = await client.call_tool("add", {"a": 5, "b": 3}) + print(f"Result: {result.content[0].text}") + + # Connect via SSE + async with Client("http://localhost:8000/sse") as client: + # ... use the client + pass +``` + +To use clients to test servers, use the following pattern: + +```python +from fastmcp import FastMCP, Client + +mcp = FastMCP("My MCP Server") + +async def main(): + # Connect via in-memory transport + async with Client(mcp) as client: + # ... use the client +``` + +FastMCP also supports connecting to multiple servers through a single unified client using the standard MCP configuration format: + +```python +from fastmcp import Client + +# Standard MCP configuration with multiple servers +config = { + "mcpServers": { + "weather": {"url": "https://weather-api.example.com/mcp"}, + "assistant": {"command": "python", "args": ["./assistant_server.py"]} + } +} + +# Create a client that connects to all servers +client = Client(config) + +async def main(): + async with client: + # Access tools and resources with server prefixes + forecast = await client.call_tool("weather_get_forecast", {"city": "London"}) + answer = await client.call_tool("assistant_answer_question", {"query": "What is MCP?"}) +``` + +Learn more in the [**Client Documentation**](https://gofastmcp.com/clients/client) and [**Transports Documentation**](https://gofastmcp.com/clients/transports). + +## Authentication + +### Enterprise Authentication, Zero Configuration + +FastMCP provides comprehensive authentication support that sets it apart from basic MCP implementations. Secure your servers and authenticate your clients with the same enterprise-grade providers used by major corporations. + +**Built-in OAuth Providers:** + +- **Google** +- **GitHub** +- **Microsoft Azure** +- **Auth0** +- **WorkOS** +- **Descope** +- **JWT/Custom** +- **API Keys** + +Protecting a server takes just two lines: + +```python +from fastmcp.server.auth import GoogleProvider + +auth = GoogleProvider(client_id="...", client_secret="...", base_url="https://myserver.com") +mcp = FastMCP("Protected Server", auth=auth) +``` + +Connecting to protected servers is even simpler: + +```python +async with Client("https://protected-server.com/mcp", auth="oauth") as client: + # Automatic browser-based OAuth flow + result = await client.call_tool("protected_tool") +``` + +**Why FastMCP Auth Matters:** + +- **Production-Ready:** Persistent storage, token refresh, comprehensive error handling +- **Zero-Config OAuth:** Just pass `auth="oauth"` for automatic setup +- **Enterprise Integration:** WorkOS SSO, Azure Active Directory, Auth0 tenants +- **Developer Experience:** Automatic browser launch, local callback server, environment variable support +- **Advanced Architecture:** Full OIDC support, Dynamic Client Registration (DCR), and unique OAuth proxy pattern that enables DCR with any provider + +*Authentication this comprehensive is unique to FastMCP 2.0.* + +Learn more in the **Authentication Documentation** for [servers](https://gofastmcp.com/servers/auth) and [clients](https://gofastmcp.com/clients/auth). + +## Deployment + +### From Development to Production + +FastMCP supports every deployment scenario from local development to global scale: + +**Development:** Run locally with a single command + +```bash +fastmcp run server.py +``` + +**Production:** Deploy to [**FastMCP Cloud**](https://fastmcp.cloud) โ€” Remote MCP that just works + +- Instant HTTPS endpoints +- Built-in authentication +- Zero configuration +- Free for personal servers + +**Self-Hosted:** Use HTTP or SSE transports for your own infrastructure + +```python +mcp.run(transport="http", host="0.0.0.0", port=8000) +``` + +Learn more in the [**Deployment Documentation**](https://gofastmcp.com/deployment). + +## Advanced Features + +FastMCP introduces powerful ways to structure and compose your MCP applications. + +### Proxy Servers + +Create a FastMCP server that acts as an intermediary for another local or remote MCP server using `FastMCP.as_proxy()`. This is especially useful for bridging transports (e.g., remote SSE to local Stdio) or adding a layer of logic to a server you don't control. + +Learn more in the [**Proxying Documentation**](https://gofastmcp.com/patterns/proxy). + +### Composing MCP Servers + +Build modular applications by mounting multiple `FastMCP` instances onto a parent server using `mcp.mount()` (live link) or `mcp.import_server()` (static copy). + +Learn more in the [**Composition Documentation**](https://gofastmcp.com/patterns/composition). + +### OpenAPI & FastAPI Generation + +Automatically generate FastMCP servers from existing OpenAPI specifications (`FastMCP.from_openapi()`) or FastAPI applications (`FastMCP.from_fastapi()`), instantly bringing your web APIs to the MCP ecosystem. + +Learn more: [**OpenAPI Integration**](https://gofastmcp.com/integrations/openapi) | [**FastAPI Integration**](https://gofastmcp.com/integrations/fastapi). + +## Running Your Server + +The main way to run a FastMCP server is by calling the `run()` method on your server instance: + +```python +# server.py +from fastmcp import FastMCP + +mcp = FastMCP("Demo ๐Ÿš€") + +@mcp.tool +def hello(name: str) -> str: + return f"Hello, {name}!" + +if __name__ == "__main__": + mcp.run() # Default: uses STDIO transport +``` + +FastMCP supports three transport protocols: + +**STDIO (Default)**: Best for local tools and command-line scripts. + +```python +mcp.run(transport="stdio") # Default, so transport argument is optional +``` + +**Streamable HTTP**: Recommended for web deployments. + +```python +mcp.run(transport="http", host="127.0.0.1", port=8000, path="/mcp") +``` + +**SSE**: For compatibility with existing SSE clients. + +```python +mcp.run(transport="sse", host="127.0.0.1", port=8000) +``` + +See the [**Running Server Documentation**](https://gofastmcp.com/deployment/running-server) for more details. + +## Contributing + +Contributions are the core of open source! We welcome improvements and features. + +### Prerequisites + +- Python 3.10+ +- [uv](https://docs.astral.sh/uv/) (Recommended for environment management) + +### Setup + +1. Clone the repository: + + ```bash + git clone https://github.com/jlowin/fastmcp.git + cd fastmcp + ``` + +2. Create and sync the environment: + + ```bash + uv sync + ``` + + This installs all dependencies, including dev tools. + +3. Activate the virtual environment (e.g., `source .venv/bin/activate` or via your IDE). + +### Unit Tests + +FastMCP has a comprehensive unit test suite. All PRs must introduce or update tests as appropriate and pass the full suite. + +Run tests using pytest: + +```bash +pytest +``` + +or if you want an overview of the code coverage + +```bash +uv run pytest --cov=src --cov=examples --cov-report=html +``` + +### Static Checks + +FastMCP uses `pre-commit` for code formatting, linting, and type-checking. All PRs must pass these checks (they run automatically in CI). + +Install the hooks locally: + +```bash +uv run pre-commit install +``` + +The hooks will now run automatically on `git commit`. You can also run them manually at any time: + +```bash +pre-commit run --all-files +# or via uv +uv run pre-commit run --all-files +``` + +### Pull Requests + +1. Fork the repository on GitHub. +2. Create a feature branch from `main`. +3. Make your changes, including tests and documentation updates. +4. Ensure tests and pre-commit hooks pass. +5. Commit your changes and push to your fork. +6. Open a pull request against the `main` branch of `jlowin/fastmcp`. + +Please open an issue or discussion for questions or suggestions before starting significant work! diff --git a/README_OPENAPI.md b/README_OPENAPI.md new file mode 100644 index 000000000..cb0d5f9c0 --- /dev/null +++ b/README_OPENAPI.md @@ -0,0 +1,246 @@ +# FastMCP OpenAPI Integration + +This document explains how FastMCP's OpenAPI integration works, what features are supported, and how to extend it. The OpenAPI functionality is split across two main files: + +- `server/openapi.py` - High-level FastMCP server implementation and MCP component creation +- `utilities/openapi.py` - Low-level OpenAPI parsing and intermediate representation + +## Architecture Overview + +``` +OpenAPI Spec โ†’ Parse โ†’ HTTPRoute IR โ†’ Create MCP Components โ†’ FastMCP Server +``` + +### 1. Parsing Phase (`utilities/openapi.py`) + +OpenAPI specifications are parsed into an intermediate representation (IR) that normalizes differences between OpenAPI 3.0 and 3.1: + +- **Input**: Raw OpenAPI spec (dict) +- **Output**: List of `HTTPRoute` objects with normalized parameter information +- **Key Classes**: + - `HTTPRoute` - Represents a single operation + - `ParameterInfo` - Represents a parameter with location, style, explode, etc. + - `RequestBodyInfo` - Represents request body information + - `ResponseInfo` - Represents response information + +### 2. Component Creation Phase (`server/openapi.py`) + +HTTPRoute objects are converted into FastMCP components based on route mapping rules: + +- **Tools** (`OpenAPITool`) - HTTP operations that can be called +- **Resources** (`OpenAPIResource`) - HTTP endpoints that return data +- **Resource Templates** (`OpenAPIResourceTemplate`) - Parameterized resources + +## Parameter Handling + +FastMCP supports various OpenAPI parameter serialization styles and formats: + +### Supported Parameter Locations +- `query` - Query string parameters +- `path` - Path parameters +- `header` - HTTP headers +- `cookie` - Cookie parameters (parsed but not used in requests) + +### Supported Parameter Styles + +#### Query Parameters +- **`form`** (default) - Standard query parameter format + - `explode=true` (default): `?tags=red&tags=blue` + - `explode=false`: `?tags=red,blue` +- **`deepObject`** - Object parameters with bracket notation + - `explode=true`: `?filter[name]=John&filter[age]=30` + - `explode=false`: Falls back to JSON string (non-standard, logs warning) + +#### Path Parameters +- **`simple`** (default) - Comma-separated for arrays: `/users/1,2,3` + +#### Header Parameters +- **`simple`** (default) - Standard header format + +### Parameter Type Support + +#### Arrays +- String arrays with `explode=true/false` +- Number arrays with `explode=true/false` +- Boolean arrays with `explode=true/false` +- Complex object arrays (basic support, may not handle all cases) + +#### Objects +- Objects with `deepObject` style and `explode=true` +- Objects with other styles fall back to JSON serialization + +#### Primitives +- Strings, numbers, booleans +- Enums +- Default values + +## Request Body Handling + +### Supported Content Types +- `application/json` - JSON request bodies + +### Schema Support +- Object schemas with properties +- Array schemas +- Primitive schemas +- Schema references (`$ref` to local schemas only) +- Required properties +- Default values + +## Response Handling + +### Content Type Detection +- `application/json` - Parsed as JSON +- `text/*` - Returned as text +- `application/xml` - Returned as text +- Other types - Returned as binary + +### Output Schema Generation +- Success response schemas (200, 201, 202, 204) +- Object response wrapping for MCP compliance +- Schema compression (removes unused `$defs`) + +## Route Mapping + +Routes are mapped to MCP component types using `RouteMap` configurations: + +```python +RouteMap( + methods=["GET", "POST"], # HTTP methods to match + pattern=r"/api/users/.*", # Regex pattern for path + mcp_type=MCPType.RESOURCE_TEMPLATE, # Target component type + tags={"user"}, # OpenAPI tags to match (AND condition) + mcp_tags={"fastmcp-user"} # Tags to add to created components +) +``` + +### Default Behavior +- All routes become **Tools** by default +- Use route maps to override specific patterns + +### Component Types +- `MCPType.TOOL` - Callable operations +- `MCPType.RESOURCE` - Static data endpoints +- `MCPType.RESOURCE_TEMPLATE` - Parameterized data endpoints +- `MCPType.EXCLUDE` - Skip route entirely + +## Known Limitations & Edge Cases + +### Parameter Edge Cases +1. **Parameter Name Collisions** - When path/query parameters have same names as request body properties, non-body parameters get `__location` suffixes +2. **Complex Array Serialization** - Limited support for arrays containing objects +3. **Cookie Parameters** - Parsed but not used in requests +4. **Non-standard Combinations** - e.g., `deepObject` with `explode=false` + +### Request Body Edge Cases +1. **Content Type Priority** - Only first available content type is used +2. **Nested Objects** - Deep nesting may not serialize correctly +3. **Binary Content** - No support for file uploads or binary data + +### Response Edge Cases +1. **Multiple Content Types** - Only JSON-compatible types are used for output schemas +2. **Error Responses** - Not used for MCP output schema generation +3. **Response Headers** - Not captured or exposed + +### Schema Edge Cases +1. **External References** - `$ref` to external files not supported +2. **Circular References** - May cause issues in schema processing +3. **Polymorphism** - `oneOf`/`anyOf`/`allOf` limited support + +## Debugging Tips + +### Common Issues +1. **"Unknown tool/resource"** - Check route mapping configuration +2. **Parameter not found** - Check for name collisions or incorrect style/explode +3. **Invalid request format** - Check parameter serialization and content types +4. **Schema validation errors** - Check for external refs or complex schemas + +### Debugging Tools +```python +# Parse routes to inspect intermediate representation +routes = parse_openapi_to_http_routes(openapi_spec) +for route in routes: + print(f"{route.method} {route.path}") + for param in route.parameters: + print(f" {param.name} ({param.location}): style={param.style}, explode={param.explode}") + +# Check component creation +server = FastMCP.from_openapi(openapi_spec, client) +tools = await server.get_tools() +print(f"Created {len(tools)} tools: {list(tools.keys())}") +``` + +### Logging +- Set `FASTMCP_LOG_LEVEL=DEBUG` to see detailed parameter processing +- Look for warnings about non-standard parameter combinations +- Check for schema parsing errors in logs + +## Extension Points + +### Adding New Parameter Styles +1. Add style handling in `utilities/openapi.py` - `ParameterInfo` class +2. Implement serialization logic in `server/openapi.py` - `OpenAPITool.run()` +3. Add tests for parsing and serialization + +### Adding New Content Types +1. Extend request body handling in `OpenAPITool.run()` +2. Add response parsing logic for new types +3. Update content type priority in utilities + +### Custom Route Mapping +Use `route_map_fn` for complex routing logic: + +```python +def custom_mapper(route: HTTPRoute, current_type: MCPType) -> MCPType: + if route.path.startswith("/admin"): + return MCPType.EXCLUDE + return current_type + +server = FastMCP.from_openapi(spec, client, route_map_fn=custom_mapper) +``` + +## Testing Patterns + +### Unit Tests +- Test parameter parsing with various styles/explode combinations +- Test route mapping with different patterns and tags +- Test schema generation and compression + +### Integration Tests +- Mock HTTP client to verify actual request parameters +- Test end-to-end component creation and execution +- Test error handling and edge cases + +### Example Test Pattern +```python +async def test_parameter_style(): + # 1. Create OpenAPI spec with specific parameter configuration + spec = {"openapi": "3.1.0", ...} + + # 2. Parse and create components + routes = parse_openapi_to_http_routes(spec) + tool = OpenAPITool(mock_client, routes[0], ...) + + # 3. Execute and verify request parameters + await tool.run({"param": "value"}) + actual_params = mock_client.request.call_args.kwargs["params"] + assert actual_params == expected_params +``` + +## Testing + +OpenAPI functionality is tested across multiple files in `tests/server/openapi/`: + +- `test_basic_functionality.py` - Core component creation and execution +- `test_explode_integration.py` - Parameter explode behavior +- `test_deepobject_style.py` - DeepObject style parameter encoding +- `test_parameter_collisions.py` - Parameter name collision handling +- `test_openapi_path_parameters.py` - Path parameter serialization +- `test_configuration.py` - Route mapping and MCP names +- `test_description_propagation.py` - Schema and description handling + +When adding new OpenAPI features, create focused test files rather than adding to existing monolithic files. + +--- + +*This document should be updated when new OpenAPI features are added or when edge cases are discovered and addressed.* \ No newline at end of file diff --git a/Windows_Notes.md b/Windows_Notes.md new file mode 100644 index 000000000..f2f9445eb --- /dev/null +++ b/Windows_Notes.md @@ -0,0 +1,58 @@ +# Getting your development environment set up properly +To get your environment up and running properly, you'll need a slightly different set of commands that are windows specific: +```bash +uv venv +.venv\Scripts\activate +uv pip install -e ".[dev]" +``` + +This will install the package in editable mode, and install the development dependencies. + + +# Fixing `AttributeError: module 'collections' has no attribute 'Callable'` +- open `.venv\Lib\site-packages\pyreadline\py3k_compat.py` +- change `return isinstance(x, collections.Callable)` to +``` +from collections.abc import Callable +return isinstance(x, Callable) +``` + +# Helpful notes +For developing FastMCP +## Install local development version of FastMCP into a local FastMCP project server +- ensure +- change directories to your FastMCP Server location so you can install it in your .venv +- run `.venv\Scripts\activate` to activate your virtual environment +- Then run a series of commands to uninstall the old version and install the new +```bash +# First uninstall +uv pip uninstall fastmcp + +# Clean any build artifacts in your fastmcp directory +cd C:\path\to\fastmcp +del /s /q *.egg-info + +# Then reinstall in your weather project +cd C:\path\to\new\fastmcp_server +uv pip install --no-cache-dir -e C:\Users\justj\PycharmProjects\fastmcp + +# Check that it installed properly and has the correct git hash +pip show fastmcp +``` + +## Running the FastMCP server with Inspector +MCP comes with a node.js application called Inspector that can be used to inspect the FastMCP server. To run the inspector, you'll need to install node.js and npm. Then you can run the following commands: +```bash +fastmcp dev server.py +``` +This will launch a web app on http://localhost:5173/ that you can use to inspect the FastMCP server. + +## If you start development before creating a fork - your get out of jail free card +- Add your fork as a new remote to your local repository `git remote add fork git@github.com:YOUR-USERNAME/REPOSITORY-NAME.git` + - This will add your repo, short named 'fork', as a remote to your local repository +- Verify that it was added correctly by running `git remote -v` +- Commit your changes +- Push your changes to your fork `git push fork ` +- Create your pull request on GitHub + + diff --git a/docs/.ccignore b/docs/.ccignore new file mode 100644 index 000000000..ad117c3ee --- /dev/null +++ b/docs/.ccignore @@ -0,0 +1,2 @@ +changelog.mdx +python-sdk/ diff --git a/docs/.cursor/rules/mintlify.mdc b/docs/.cursor/rules/mintlify.mdc new file mode 100644 index 000000000..503fe1647 --- /dev/null +++ b/docs/.cursor/rules/mintlify.mdc @@ -0,0 +1,364 @@ +--- +description: +globs: *.mdx +alwaysApply: false +--- +# Mintlify technical writing assistant + +You are an AI writing assistant specialized in creating exceptional technical documentation using Mintlify components and following industry-leading technical writing practices. + +## Core writing principles + +### Language and style requirements +- Use clear, direct language appropriate for technical audiences +- Write in second person ("you") for instructions and procedures +- Use active voice over passive voice +- Employ present tense for current states, future tense for outcomes +- Maintain consistent terminology throughout all documentation +- Keep sentences concise while providing necessary context +- Use parallel structure in lists, headings, and procedures + +### Content organization standards +- Lead with the most important information (inverted pyramid structure) +- Use progressive disclosure: basic concepts before advanced ones +- Break complex procedures into numbered steps +- Include prerequisites and context before instructions +- Provide expected outcomes for each major step +- End sections with next steps or related information +- Use descriptive, keyword-rich headings for navigation and SEO + +### User-centered approach +- Focus on user goals and outcomes rather than system features +- Anticipate common questions and address them proactively +- Include troubleshooting for likely failure points +- Provide multiple pathways when appropriate (beginner vs advanced), but offer an opinionated path for people to follow to avoid overwhelming with options + +## Mintlify component reference + +### Callout components + +#### Note - Additional helpful information + + +Supplementary information that supports the main content without interrupting flow + + +#### Tip - Best practices and pro tips + + +Expert advice, shortcuts, or best practices that enhance user success + + +#### Warning - Important cautions + + +Critical information about potential issues, breaking changes, or destructive actions + + +#### Info - Neutral contextual information + + +Background information, context, or neutral announcements + + +#### Check - Success confirmations + + +Positive confirmations, successful completions, or achievement indicators + + +### Code components + +#### Single code block + +```javascript config.js +const apiConfig = { +baseURL: 'https://api.example.com', +timeout: 5000, +headers: { + 'Authorization': `Bearer ${process.env.API_TOKEN}` +} +}; +``` + +#### Code group with multiple languages + + +```javascript Node.js +const response = await fetch('/api/endpoint', { + headers: { Authorization: `Bearer ${apiKey}` } +}); +``` + +```python Python +import requests +response = requests.get('/api/endpoint', + headers={'Authorization': f'Bearer {api_key}'}) +``` + +```curl cURL +curl -X GET '/api/endpoint' \ + -H 'Authorization: Bearer YOUR_API_KEY' +``` + + +#### Request/Response examples + + +```bash cURL +curl -X POST 'https://api.example.com/users' \ + -H 'Content-Type: application/json' \ + -d '{"name": "John Doe", "email": "john@example.com"}' +``` + + + +```json Success +{ + "id": "user_123", + "name": "John Doe", + "email": "john@example.com", + "created_at": "2024-01-15T10:30:00Z" +} +``` + + +### Structural components + +#### Steps for procedures + + + + Run `npm install` to install required packages. + + + Verify installation by running `npm list`. + + + + + Create a `.env` file with your API credentials. + + ```bash + API_KEY=your_api_key_here + ``` + + + Never commit API keys to version control. + + + + +#### Tabs for alternative content + + + + ```bash + brew install node + npm install -g package-name + ``` + + + + ```powershell + choco install nodejs + npm install -g package-name + ``` + + + + ```bash + sudo apt install nodejs npm + npm install -g package-name + ``` + + + +#### Accordions for collapsible content + + + + - **Firewall blocking**: Ensure ports 80 and 443 are open + - **Proxy configuration**: Set HTTP_PROXY environment variable + - **DNS resolution**: Try using 8.8.8.8 as DNS server + + + + ```javascript + const config = { + performance: { cache: true, timeout: 30000 }, + security: { encryption: 'AES-256' } + }; + ``` + + + +### API documentation components + +#### Parameter fields + + +Unique identifier for the user. Must be a valid UUID v4 format. + + + +User's email address. Must be valid and unique within the system. + + + +Maximum number of results to return. Range: 1-100. + + + +Bearer token for API authentication. Format: `Bearer YOUR_API_KEY` + + +#### Response fields + + +Unique identifier assigned to the newly created user. + + + +ISO 8601 formatted timestamp of when the user was created. + + + +List of permission strings assigned to this user. + + +#### Expandable nested fields + + +Complete user object with all associated data. + + + + User profile information including personal details. + + + + User's first name as entered during registration. + + + + URL to user's profile picture. Returns null if no avatar is set. + + + + + + +### Interactive components + +#### Cards for navigation + + +Complete walkthrough from installation to your first API call in under 10 minutes. + + + + + Learn how to authenticate requests using API keys or JWT tokens. + + + + Understand rate limits and best practices for high-volume usage. + + + +### Media and advanced components + +#### Frames for images + +Wrap all images in frames. + + +Main dashboard showing analytics overview + + + +Analytics dashboard with charts + + +#### Tooltips and updates + + +API + + + +## New features +- Added bulk user import functionality +- Improved error messages with actionable suggestions + +## Bug fixes +- Fixed pagination issue with large datasets +- Resolved authentication timeout problems + + +## Required page structure + +Every documentation page must begin with YAML frontmatter: + +```yaml +--- +title: "Clear, specific, keyword-rich title" +description: "Concise description explaining page purpose and value" +--- +``` + +## Content quality standards + +### Code examples requirements +- Always include complete, runnable examples that users can copy and execute +- Show proper error handling and edge case management +- Use realistic data instead of placeholder values +- Include expected outputs and results for verification +- Test all code examples thoroughly before publishing +- Specify language and include filename when relevant +- Add explanatory comments for complex logic + +### API documentation requirements +- Document all parameters including optional ones with clear descriptions +- Show both success and error response examples with realistic data +- Include rate limiting information with specific limits +- Provide authentication examples showing proper format +- Explain all HTTP status codes and error handling +- Cover complete request/response cycles + +### Accessibility requirements +- Include descriptive alt text for all images and diagrams +- Use specific, actionable link text instead of "click here" +- Ensure proper heading hierarchy starting with H2 +- Provide keyboard navigation considerations +- Use sufficient color contrast in examples and visuals +- Structure content for easy scanning with headers and lists + +## AI assistant instructions + +### Component selection logic +- Use **Steps** for procedures, tutorials, setup guides, and sequential instructions +- Use **Tabs** for platform-specific content or alternative approaches +- Use **CodeGroup** when showing the same concept in multiple languages +- Use **Accordions** for supplementary information that might interrupt flow +- Use **Cards and CardGroup** for navigation, feature overviews, and related resources +- Use **RequestExample/ResponseExample** specifically for API endpoint documentation +- Use **ParamField** for API parameters, **ResponseField** for API responses +- Use **Expandable** for nested object properties or hierarchical information + +### Quality assurance checklist +- Verify all code examples are syntactically correct and executable +- Test all links to ensure they are functional and lead to relevant content +- Validate Mintlify component syntax with all required properties +- Confirm proper heading hierarchy with H2 for main sections, H3 for subsections +- Ensure content flows logically from basic concepts to advanced topics +- Check for consistency in terminology, formatting, and component usage + +### Error prevention strategies +- Always include realistic error handling in code examples +- Provide dedicated troubleshooting sections for complex procedures +- Explain prerequisites clearly before beginning instructions +- Include verification and testing steps with expected outcomes +- Add appropriate warnings for destructive or security-sensitive actions +- Validate all technical information through testing before publication \ No newline at end of file diff --git a/docs/assets/brand/blue-logo.png b/docs/assets/brand/blue-logo.png new file mode 100644 index 000000000..18c3bc6f9 Binary files /dev/null and b/docs/assets/brand/blue-logo.png differ diff --git a/docs/assets/brand/f-watercolor-waves-dark.png b/docs/assets/brand/f-watercolor-waves-dark.png new file mode 100644 index 000000000..a50956cc8 Binary files /dev/null and b/docs/assets/brand/f-watercolor-waves-dark.png differ diff --git a/docs/assets/brand/f-watercolor-waves.png b/docs/assets/brand/f-watercolor-waves.png new file mode 100644 index 000000000..93ccf6efb Binary files /dev/null and b/docs/assets/brand/f-watercolor-waves.png differ diff --git a/docs/assets/brand/favicon.svg b/docs/assets/brand/favicon.svg new file mode 100644 index 000000000..b9a0a6c9a --- /dev/null +++ b/docs/assets/brand/favicon.svg @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/docs/assets/brand/thumbnail-background.png b/docs/assets/brand/thumbnail-background.png new file mode 100644 index 000000000..2f93bc0ca Binary files /dev/null and b/docs/assets/brand/thumbnail-background.png differ diff --git a/docs/assets/brand/wordmark-padded.png b/docs/assets/brand/wordmark-padded.png new file mode 100644 index 000000000..5dc7932e4 Binary files /dev/null and b/docs/assets/brand/wordmark-padded.png differ diff --git a/docs/assets/brand/wordmark-watercolor-rainbow-dark.png b/docs/assets/brand/wordmark-watercolor-rainbow-dark.png new file mode 100644 index 000000000..5c2d673d4 Binary files /dev/null and b/docs/assets/brand/wordmark-watercolor-rainbow-dark.png differ diff --git a/docs/assets/brand/wordmark-watercolor-rainbow.png b/docs/assets/brand/wordmark-watercolor-rainbow.png new file mode 100644 index 000000000..f16370447 Binary files /dev/null and b/docs/assets/brand/wordmark-watercolor-rainbow.png differ diff --git a/docs/assets/brand/wordmark-watercolor-waves-dark.png b/docs/assets/brand/wordmark-watercolor-waves-dark.png new file mode 100644 index 000000000..02d936762 Binary files /dev/null and b/docs/assets/brand/wordmark-watercolor-waves-dark.png differ diff --git a/docs/assets/brand/wordmark-watercolor-waves.png b/docs/assets/brand/wordmark-watercolor-waves.png new file mode 100644 index 000000000..84950f9ec Binary files /dev/null and b/docs/assets/brand/wordmark-watercolor-waves.png differ diff --git a/docs/assets/brand/wordmark-white-padded.png b/docs/assets/brand/wordmark-white-padded.png new file mode 100644 index 000000000..71eb80777 Binary files /dev/null and b/docs/assets/brand/wordmark-white-padded.png differ diff --git a/docs/assets/brand/wordmark-white.png b/docs/assets/brand/wordmark-white.png new file mode 100644 index 000000000..81377c5be Binary files /dev/null and b/docs/assets/brand/wordmark-white.png differ diff --git a/docs/assets/brand/wordmark.png b/docs/assets/brand/wordmark.png new file mode 100644 index 000000000..41b5717b2 Binary files /dev/null and b/docs/assets/brand/wordmark.png differ diff --git a/docs/assets/images/fastmcp_cloud/connect.png b/docs/assets/images/fastmcp_cloud/connect.png new file mode 100644 index 000000000..583202f34 Binary files /dev/null and b/docs/assets/images/fastmcp_cloud/connect.png differ diff --git a/docs/assets/images/fastmcp_cloud/create_project.png b/docs/assets/images/fastmcp_cloud/create_project.png new file mode 100644 index 000000000..e34c8a1f4 Binary files /dev/null and b/docs/assets/images/fastmcp_cloud/create_project.png differ diff --git a/docs/assets/images/fastmcp_cloud/deployment.png b/docs/assets/images/fastmcp_cloud/deployment.png new file mode 100644 index 000000000..44011d900 Binary files /dev/null and b/docs/assets/images/fastmcp_cloud/deployment.png differ diff --git a/docs/assets/images/fastmcp_cloud/quickstart.png b/docs/assets/images/fastmcp_cloud/quickstart.png new file mode 100644 index 000000000..31c44472e Binary files /dev/null and b/docs/assets/images/fastmcp_cloud/quickstart.png differ diff --git a/docs/assets/images/tutorial-rest-api-result.png b/docs/assets/images/tutorial-rest-api-result.png new file mode 100644 index 000000000..1ff5fc071 Binary files /dev/null and b/docs/assets/images/tutorial-rest-api-result.png differ diff --git a/docs/assets/schemas/mcp_server_config/latest.json b/docs/assets/schemas/mcp_server_config/latest.json new file mode 100644 index 000000000..81d0ad754 --- /dev/null +++ b/docs/assets/schemas/mcp_server_config/latest.json @@ -0,0 +1,348 @@ +{ + "$defs": { + "Deployment": { + "description": "Configuration for server deployment and runtime settings.", + "properties": { + "transport": { + "anyOf": [ + { + "enum": [ + "stdio", + "http", + "sse" + ], + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Transport protocol to use", + "title": "Transport" + }, + "host": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Host to bind to when using HTTP transport", + "examples": [ + "127.0.0.1", + "0.0.0.0", + "localhost" + ], + "title": "Host" + }, + "port": { + "anyOf": [ + { + "type": "integer" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Port to bind to when using HTTP transport", + "examples": [ + 8000, + 3000, + 5000 + ], + "title": "Port" + }, + "path": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "URL path for the server endpoint", + "examples": [ + "/mcp/", + "/api/mcp/", + "/sse/" + ], + "title": "Path" + }, + "log_level": { + "anyOf": [ + { + "enum": [ + "DEBUG", + "INFO", + "WARNING", + "ERROR", + "CRITICAL" + ], + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Log level for the server", + "title": "Log Level" + }, + "cwd": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Working directory for the server process", + "examples": [ + ".", + "./src", + "/app" + ], + "title": "Cwd" + }, + "env": { + "anyOf": [ + { + "additionalProperties": { + "type": "string" + }, + "type": "object" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Environment variables to set when running the server", + "examples": [ + { + "API_KEY": "secret", + "DEBUG": "true" + } + ], + "title": "Env" + }, + "args": { + "anyOf": [ + { + "items": { + "type": "string" + }, + "type": "array" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Arguments to pass to the server (after --)", + "examples": [ + [ + "--config", + "config.json", + "--debug" + ] + ], + "title": "Args" + } + }, + "title": "Deployment", + "type": "object" + }, + "Environment": { + "description": "Configuration for Python environment setup.", + "properties": { + "python": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Python version constraint", + "examples": [ + "3.10", + "3.11", + "3.12" + ], + "title": "Python" + }, + "dependencies": { + "anyOf": [ + { + "items": { + "type": "string" + }, + "type": "array" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Python packages to install with PEP 508 specifiers", + "examples": [ + [ + "fastmcp>=2.0,<3", + "httpx", + "pandas>=2.0" + ] + ], + "title": "Dependencies" + }, + "requirements": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Path to requirements.txt file", + "examples": [ + "requirements.txt", + "../requirements/prod.txt" + ], + "title": "Requirements" + }, + "project": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Path to project directory containing pyproject.toml", + "examples": [ + ".", + "../my-project" + ], + "title": "Project" + }, + "editable": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Directory to install in editable mode", + "examples": [ + ".", + "../my-package" + ], + "title": "Editable" + } + }, + "title": "Environment", + "type": "object" + }, + "FileSystemSource": { + "description": "Source for local Python files.", + "properties": { + "type": { + "const": "filesystem", + "default": "filesystem", + "description": "Source type", + "title": "Type", + "type": "string" + }, + "path": { + "description": "Path to Python file containing the server", + "title": "Path", + "type": "string" + }, + "entrypoint": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Name of server instance or factory function (a no-arg function that returns a FastMCP server)", + "title": "Entrypoint" + } + }, + "required": [ + "path" + ], + "title": "FileSystemSource", + "type": "object" + } + }, + "description": "Configuration file for FastMCP servers", + "properties": { + "$schema": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": "https://gofastmcp.com/public/schemas/fastmcp.json/v1.json", + "description": "JSON schema for IDE support and validation", + "title": "$Schema" + }, + "source": { + "$ref": "#/$defs/FileSystemSource", + "description": "Source configuration for the server", + "examples": [ + { + "path": "server.py" + }, + { + "entrypoint": "app", + "path": "server.py" + }, + { + "entrypoint": "mcp", + "path": "src/server.py", + "type": "filesystem" + } + ] + }, + "environment": { + "$ref": "#/$defs/Environment", + "description": "Python environment setup configuration" + }, + "deployment": { + "$ref": "#/$defs/Deployment", + "description": "Server deployment and runtime settings" + } + }, + "required": [ + "source" + ], + "title": "FastMCP Configuration", + "type": "object", + "$id": "https://gofastmcp.com/public/schemas/fastmcp.json/v1.json" +} diff --git a/docs/assets/schemas/mcp_server_config/v1.json b/docs/assets/schemas/mcp_server_config/v1.json new file mode 100644 index 000000000..81d0ad754 --- /dev/null +++ b/docs/assets/schemas/mcp_server_config/v1.json @@ -0,0 +1,348 @@ +{ + "$defs": { + "Deployment": { + "description": "Configuration for server deployment and runtime settings.", + "properties": { + "transport": { + "anyOf": [ + { + "enum": [ + "stdio", + "http", + "sse" + ], + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Transport protocol to use", + "title": "Transport" + }, + "host": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Host to bind to when using HTTP transport", + "examples": [ + "127.0.0.1", + "0.0.0.0", + "localhost" + ], + "title": "Host" + }, + "port": { + "anyOf": [ + { + "type": "integer" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Port to bind to when using HTTP transport", + "examples": [ + 8000, + 3000, + 5000 + ], + "title": "Port" + }, + "path": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "URL path for the server endpoint", + "examples": [ + "/mcp/", + "/api/mcp/", + "/sse/" + ], + "title": "Path" + }, + "log_level": { + "anyOf": [ + { + "enum": [ + "DEBUG", + "INFO", + "WARNING", + "ERROR", + "CRITICAL" + ], + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Log level for the server", + "title": "Log Level" + }, + "cwd": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Working directory for the server process", + "examples": [ + ".", + "./src", + "/app" + ], + "title": "Cwd" + }, + "env": { + "anyOf": [ + { + "additionalProperties": { + "type": "string" + }, + "type": "object" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Environment variables to set when running the server", + "examples": [ + { + "API_KEY": "secret", + "DEBUG": "true" + } + ], + "title": "Env" + }, + "args": { + "anyOf": [ + { + "items": { + "type": "string" + }, + "type": "array" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Arguments to pass to the server (after --)", + "examples": [ + [ + "--config", + "config.json", + "--debug" + ] + ], + "title": "Args" + } + }, + "title": "Deployment", + "type": "object" + }, + "Environment": { + "description": "Configuration for Python environment setup.", + "properties": { + "python": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Python version constraint", + "examples": [ + "3.10", + "3.11", + "3.12" + ], + "title": "Python" + }, + "dependencies": { + "anyOf": [ + { + "items": { + "type": "string" + }, + "type": "array" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Python packages to install with PEP 508 specifiers", + "examples": [ + [ + "fastmcp>=2.0,<3", + "httpx", + "pandas>=2.0" + ] + ], + "title": "Dependencies" + }, + "requirements": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Path to requirements.txt file", + "examples": [ + "requirements.txt", + "../requirements/prod.txt" + ], + "title": "Requirements" + }, + "project": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Path to project directory containing pyproject.toml", + "examples": [ + ".", + "../my-project" + ], + "title": "Project" + }, + "editable": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Directory to install in editable mode", + "examples": [ + ".", + "../my-package" + ], + "title": "Editable" + } + }, + "title": "Environment", + "type": "object" + }, + "FileSystemSource": { + "description": "Source for local Python files.", + "properties": { + "type": { + "const": "filesystem", + "default": "filesystem", + "description": "Source type", + "title": "Type", + "type": "string" + }, + "path": { + "description": "Path to Python file containing the server", + "title": "Path", + "type": "string" + }, + "entrypoint": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Name of server instance or factory function (a no-arg function that returns a FastMCP server)", + "title": "Entrypoint" + } + }, + "required": [ + "path" + ], + "title": "FileSystemSource", + "type": "object" + } + }, + "description": "Configuration file for FastMCP servers", + "properties": { + "$schema": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": "https://gofastmcp.com/public/schemas/fastmcp.json/v1.json", + "description": "JSON schema for IDE support and validation", + "title": "$Schema" + }, + "source": { + "$ref": "#/$defs/FileSystemSource", + "description": "Source configuration for the server", + "examples": [ + { + "path": "server.py" + }, + { + "entrypoint": "app", + "path": "server.py" + }, + { + "entrypoint": "mcp", + "path": "src/server.py", + "type": "filesystem" + } + ] + }, + "environment": { + "$ref": "#/$defs/Environment", + "description": "Python environment setup configuration" + }, + "deployment": { + "$ref": "#/$defs/Deployment", + "description": "Server deployment and runtime settings" + } + }, + "required": [ + "source" + ], + "title": "FastMCP Configuration", + "type": "object", + "$id": "https://gofastmcp.com/public/schemas/fastmcp.json/v1.json" +} diff --git a/docs/assets/updates/release-2-7.png b/docs/assets/updates/release-2-7.png new file mode 100644 index 000000000..5716e1f73 Binary files /dev/null and b/docs/assets/updates/release-2-7.png differ diff --git a/docs/changelog.mdx b/docs/changelog.mdx new file mode 100644 index 000000000..fabf26c6e --- /dev/null +++ b/docs/changelog.mdx @@ -0,0 +1,2013 @@ +--- +title: "Changelog" +icon: "list-check" +rss: true +--- + + + +**[v2.12.4: OIDC What You Did There](https://github.com/jlowin/fastmcp/releases/tag/v2.12.4)** + +FastMCP 2.12.4 adds comprehensive OIDC support and expands authentication options with AWS Cognito and Descope providers. The release also includes improvements to logging middleware, URL handling for nested resources, persistent OAuth client registration storage, and various fixes to the experimental OpenAPI parser. + +## What's Changed +### New Features ๐ŸŽ‰ +* feat: Add support for OIDC configuration by [@ruhulio](https://github.com/ruhulio) in [#1817](https://github.com/jlowin/fastmcp/pull/1817) +### Enhancements ๐Ÿ”ง +* feat: Move the Starlette context middleware to the front by [@akkuman](https://github.com/akkuman) in [#1812](https://github.com/jlowin/fastmcp/pull/1812) +* Refactor Logging and Structured Logging Middleware by [@strawgate](https://github.com/strawgate) in [#1805](https://github.com/jlowin/fastmcp/pull/1805) +* Update pull_request_template.md by [@jlowin](https://github.com/jlowin) in [#1824](https://github.com/jlowin/fastmcp/pull/1824) +* chore: Set redirect_path default in function by [@ruhulio](https://github.com/ruhulio) in [#1833](https://github.com/jlowin/fastmcp/pull/1833) +* feat: Set instructions in code by [@attiks](https://github.com/attiks) in [#1838](https://github.com/jlowin/fastmcp/pull/1838) +* Automatically Create inline Snapshots by [@strawgate](https://github.com/strawgate) in [#1779](https://github.com/jlowin/fastmcp/pull/1779) +* chore: Cleanup Auth0 redirect_path initialization by [@ruhulio](https://github.com/ruhulio) in [#1842](https://github.com/jlowin/fastmcp/pull/1842) +* feat: Add support for Descope Authentication by [@anvibanga](https://github.com/anvibanga) in [#1853](https://github.com/jlowin/fastmcp/pull/1853) +* Update descope version badges by [@jlowin](https://github.com/jlowin) in [#1870](https://github.com/jlowin/fastmcp/pull/1870) +* Update welcome images by [@jlowin](https://github.com/jlowin) in [#1884](https://github.com/jlowin/fastmcp/pull/1884) +* Fix rounded edges of image by [@jlowin](https://github.com/jlowin) in [#1886](https://github.com/jlowin/fastmcp/pull/1886) +* optimize test suite by [@zzstoatzz](https://github.com/zzstoatzz) in [#1893](https://github.com/jlowin/fastmcp/pull/1893) +* Enhancement: client completions support context_arguments by [@isijoe](https://github.com/isijoe) in [#1906](https://github.com/jlowin/fastmcp/pull/1906) +* Update Descope icon by [@anvibanga](https://github.com/anvibanga) in [#1912](https://github.com/jlowin/fastmcp/pull/1912) +* Add AWS Cognito OAuth Provider for Enterprise Authentication by [@stephaneberle9](https://github.com/stephaneberle9) in [#1873](https://github.com/jlowin/fastmcp/pull/1873) +* Fix typos discovered by codespell by [@cclauss](https://github.com/cclauss) in [#1922](https://github.com/jlowin/fastmcp/pull/1922) +* Use lowercase namespace for fastmcp logger by [@jlowin](https://github.com/jlowin) in [#1791](https://github.com/jlowin/fastmcp/pull/1791) +### Fixes ๐Ÿž +* Update quickstart.mdx by [@radi-dev](https://github.com/radi-dev) in [#1821](https://github.com/jlowin/fastmcp/pull/1821) +* Remove extraneous union import by [@jlowin](https://github.com/jlowin) in [#1823](https://github.com/jlowin/fastmcp/pull/1823) +* Delay import of Provider classes until FastMCP Server Creation by [@strawgate](https://github.com/strawgate) in [#1820](https://github.com/jlowin/fastmcp/pull/1820) +* fix: correct documentation link in deprecation warning by [@strawgate](https://github.com/strawgate) in [#1828](https://github.com/jlowin/fastmcp/pull/1828) +* fix: Increase default 3s timeout on Pytest by [@dacamposol](https://github.com/dacamposol) in [#1866](https://github.com/jlowin/fastmcp/pull/1866) +* fix: Improve URL handling in OIDCConfiguration by [@ruhulio](https://github.com/ruhulio) in [#1850](https://github.com/jlowin/fastmcp/pull/1850) +* fix: correct typing for on_read_resource middleware method by [@strawgate](https://github.com/strawgate) in [#1858](https://github.com/jlowin/fastmcp/pull/1858) +* feat(experimental/openapi): replace $ref in additionalProperties; add tests by [@jlowin](https://github.com/jlowin) in [#1735](https://github.com/jlowin/fastmcp/pull/1735) +* Honor client supplied scopes during registration by [@dmikusa](https://github.com/dmikusa) in [#1860](https://github.com/jlowin/fastmcp/pull/1860) +* Fix: FastAPI list parameter parsing in experimental OpenAPI parser by [@jlowin](https://github.com/jlowin) in [#1834](https://github.com/jlowin/fastmcp/pull/1834) +* Add log level support for stdio and HTTP transports by [@jlowin](https://github.com/jlowin) in [#1840](https://github.com/jlowin/fastmcp/pull/1840) +* Fix OAuth pre-flight check to accept HTTP 200 responses by [@jlowin](https://github.com/jlowin) in [#1874](https://github.com/jlowin/fastmcp/pull/1874) +* Fix: Preserve OpenAPI parameter descriptions in experimental parser by [@shlomo666](https://github.com/shlomo666) in [#1877](https://github.com/jlowin/fastmcp/pull/1877) +* Add persistent storage for OAuth client registrations by [@jlowin](https://github.com/jlowin) in [#1879](https://github.com/jlowin/fastmcp/pull/1879) +* docs: update release dates based on github releases by [@lodu](https://github.com/lodu) in [#1890](https://github.com/jlowin/fastmcp/pull/1890) +* Small updates to Sampling types by [@strawgate](https://github.com/strawgate) in [#1882](https://github.com/jlowin/fastmcp/pull/1882) +* remove lockfile smart_home example by [@zzstoatzz](https://github.com/zzstoatzz) in [#1892](https://github.com/jlowin/fastmcp/pull/1892) +* Fix: Remove JSON schema title metadata while preserving parameters named 'title' by [@jlowin](https://github.com/jlowin) in [#1872](https://github.com/jlowin/fastmcp/pull/1872) +* Fix: get_resource_url nested URL handling by [@raphael-linx](https://github.com/raphael-linx) in [#1914](https://github.com/jlowin/fastmcp/pull/1914) +* Clean up code for creating the resource url by [@jlowin](https://github.com/jlowin) in [#1916](https://github.com/jlowin/fastmcp/pull/1916) +* Fix route count logging in OpenAPI server by [@zzstoatzz](https://github.com/zzstoatzz) in [#1928](https://github.com/jlowin/fastmcp/pull/1928) +### Docs ๐Ÿ“š +* docs: make Gemini CLI integration discoverable by [@jackwotherspoon](https://github.com/jackwotherspoon) in [#1827](https://github.com/jlowin/fastmcp/pull/1827) +* docs: update NEW tags for AI assistant integrations by [@jackwotherspoon](https://github.com/jackwotherspoon) in [#1829](https://github.com/jlowin/fastmcp/pull/1829) +* Update wordmark by [@jlowin](https://github.com/jlowin) in [#1832](https://github.com/jlowin/fastmcp/pull/1832) +* docs: improve OAuth and OIDC Proxy documentation by [@jlowin](https://github.com/jlowin) in [#1880](https://github.com/jlowin/fastmcp/pull/1880) +* Update readme + welcome docs by [@jlowin](https://github.com/jlowin) in [#1883](https://github.com/jlowin/fastmcp/pull/1883) +* Update dark mode image in README by [@jlowin](https://github.com/jlowin) in [#1885](https://github.com/jlowin/fastmcp/pull/1885) + +## New Contributors +* [@radi-dev](https://github.com/radi-dev) made their first contribution in [#1821](https://github.com/jlowin/fastmcp/pull/1821) +* [@akkuman](https://github.com/akkuman) made their first contribution in [#1812](https://github.com/jlowin/fastmcp/pull/1812) +* [@ruhulio](https://github.com/ruhulio) made their first contribution in [#1817](https://github.com/jlowin/fastmcp/pull/1817) +* [@attiks](https://github.com/attiks) made their first contribution in [#1838](https://github.com/jlowin/fastmcp/pull/1838) +* [@anvibanga](https://github.com/anvibanga) made their first contribution in [#1853](https://github.com/jlowin/fastmcp/pull/1853) +* [@shlomo666](https://github.com/shlomo666) made their first contribution in [#1877](https://github.com/jlowin/fastmcp/pull/1877) +* [@lodu](https://github.com/lodu) made their first contribution in [#1890](https://github.com/jlowin/fastmcp/pull/1890) +* [@isijoe](https://github.com/isijoe) made their first contribution in [#1906](https://github.com/jlowin/fastmcp/pull/1906) +* [@raphael-linx](https://github.com/raphael-linx) made their first contribution in [#1914](https://github.com/jlowin/fastmcp/pull/1914) +* [@stephaneberle9](https://github.com/stephaneberle9) made their first contribution in [#1873](https://github.com/jlowin/fastmcp/pull/1873) +* [@cclauss](https://github.com/cclauss) made their first contribution in [#1922](https://github.com/jlowin/fastmcp/pull/1922) + +**Full Changelog**: [v2.12.3...v2.12.4](https://github.com/jlowin/fastmcp/compare/v2.12.3...v2.12.4) + + + + + +**[v2.12.3: Double Time](https://github.com/jlowin/fastmcp/releases/tag/v2.12.3)** + +FastMCP 2.12.3 focuses on performance and developer experience improvements based on community feedback. This release includes optimized auth provider imports that reduce server startup time, enhanced OIDC authentication flows with proper token management, and several reliability fixes for OAuth proxy configurations. The addition of automatic inline snapshot creation significantly improves the testing experience for contributors. + +## What's Changed +### New Features ๐ŸŽ‰ +* feat: Support setting MCP log level via transport configuration by [@jlowin](https://github.com/jlowin) in [#1756](https://github.com/jlowin/fastmcp/pull/1756) +### Enhancements ๐Ÿ”ง +* Add client-side auth support for mcp install cursor command by [@jlowin](https://github.com/jlowin) in [#1747](https://github.com/jlowin/fastmcp/pull/1747) +* Automatically Create inline Snapshots by [@strawgate](https://github.com/strawgate) in [#1779](https://github.com/jlowin/fastmcp/pull/1779) +* Use lowercase namespace for fastmcp logger by [@jlowin](https://github.com/jlowin) in [#1791](https://github.com/jlowin/fastmcp/pull/1791) +### Fixes ๐Ÿž +* fix: correct merge mistake during auth0 refactor by [@strawgate](https://github.com/strawgate) in [#1742](https://github.com/jlowin/fastmcp/pull/1742) +* Remove extraneous union import by [@jlowin](https://github.com/jlowin) in [#1823](https://github.com/jlowin/fastmcp/pull/1823) +* Delay import of Provider classes until FastMCP Server Creation by [@strawgate](https://github.com/strawgate) in [#1820](https://github.com/jlowin/fastmcp/pull/1820) +* fix: refactor OIDC configuration provider for proper token management by [@strawgate](https://github.com/strawgate) in [#1751](https://github.com/jlowin/fastmcp/pull/1751) +* Fix smart_home example imports by [@strawgate](https://github.com/strawgate) in [#1753](https://github.com/jlowin/fastmcp/pull/1753) +* fix: correct oauth proxy initialization of client by [@strawgate](https://github.com/strawgate) in [#1759](https://github.com/jlowin/fastmcp/pull/1759) +* Fix: return empty string when prompts have no arguments by [@jlowin](https://github.com/jlowin) in [#1766](https://github.com/jlowin/fastmcp/pull/1766) +* Fix async server callbacks by [@strawgate](https://github.com/strawgate) in [#1774](https://github.com/jlowin/fastmcp/pull/1774) +* Fix error when retrieving Completion API errors by [@strawgate](https://github.com/strawgate) in [#1785](https://github.com/jlowin/fastmcp/pull/1785) +* fix: correct documentation link in deprecation warning by [@strawgate](https://github.com/strawgate) in [#1828](https://github.com/jlowin/fastmcp/pull/1828) +### Docs ๐Ÿ“š +* Add migration docs for 2.12 by [@jlowin](https://github.com/jlowin) in [#1745](https://github.com/jlowin/fastmcp/pull/1745) +* Update docs for default sampling implementation to mention OpenAI API Key by [@strawgate](https://github.com/strawgate) in [#1763](https://github.com/jlowin/fastmcp/pull/1763) +* Add tip about sampling prompts and user_context to sampling documentation by [@jlowin](https://github.com/jlowin) in [#1764](https://github.com/jlowin/fastmcp/pull/1764) +* Update quickstart.mdx by [@radi-dev](https://github.com/radi-dev) in [#1821](https://github.com/jlowin/fastmcp/pull/1821) +### Other Changes ๐Ÿฆพ +* Replace Marvin with Claude Code in CI by [@jlowin](https://github.com/jlowin) in [#1800](https://github.com/jlowin/fastmcp/pull/1800) +* Refactor logging and structured logging middleware by [@strawgate](https://github.com/strawgate) in [#1805](https://github.com/jlowin/fastmcp/pull/1805) +* feat: Move the Starlette context middleware to the front by [@akkuman](https://github.com/akkuman) in [#1812](https://github.com/jlowin/fastmcp/pull/1812) +* feat: Add support for OIDC configuration by [@ruhulio](https://github.com/ruhulio) in [#1817](https://github.com/jlowin/fastmcp/pull/1817) + +## New Contributors +* [@radi-dev](https://github.com/radi-dev) made their first contribution in [#1821](https://github.com/jlowin/fastmcp/pull/1821) +* [@akkuman](https://github.com/akkuman) made their first contribution in [#1812](https://github.com/jlowin/fastmcp/pull/1812) +* [@ruhulio](https://github.com/ruhulio) made their first contribution in [#1817](https://github.com/jlowin/fastmcp/pull/1817) + +**Full Changelog**: [v2.12.2...v2.12.3](https://github.com/jlowin/fastmcp/compare/v2.12.2...v2.12.3) + + + + + +**[v2.12.2: Perchance to Stream](https://github.com/jlowin/fastmcp/releases/tag/v2.12.2)** + +This is a hotfix for a bug where the `streamable-http` transport was not recognized as a valid option in `fastmcp.json` configuration files, despite being supported by the CLI. This resulted in a parsing error when the CLI arguments were merged against the configuration spec. + +## What's Changed +### Fixes ๐Ÿž +* Fix streamable-http transport validation in fastmcp.json config by [@jlowin](https://github.com/jlowin) in [#1739](https://github.com/jlowin/fastmcp/pull/1739) + +**Full Changelog**: [v2.12.1...v2.12.2](https://github.com/jlowin/fastmcp/compare/v2.12.1...v2.12.2) + + + + + +**[v2.12.1: OAuth to Joy](https://github.com/jlowin/fastmcp/releases/tag/v2.12.1)** + +FastMCP 2.12.1 strengthens the OAuth proxy implementation based on extensive community testing and feedback. This release improves client storage reliability, adds PKCE forwarding for enhanced security, introduces configurable token endpoint authentication methods, and expands scope handlingโ€”all addressing real-world integration challenges discovered since 2.12.0. The enhanced test suite with mock providers ensures these improvements are robust and maintainable. + +## Breaking Changes +- **OAuth Proxy**: Users of built-in IDP integrations should note that `resource_server_url` has been renamed to `base_url` for clarity and consistency + +## What's Changed +### Enhancements ๐Ÿ”ง +* Make openai dependency optional by [@jlowin](https://github.com/jlowin) in [#1701](https://github.com/jlowin/fastmcp/pull/1701) +* Remove orphaned OAuth proxy code by [@jlowin](https://github.com/jlowin) in [#1722](https://github.com/jlowin/fastmcp/pull/1722) +* Expose valid scopes from OAuthProxy metadata by [@dmikusa](https://github.com/dmikusa) in [#1717](https://github.com/jlowin/fastmcp/pull/1717) +* OAuth proxy PKCE forwarding by [@jlowin](https://github.com/jlowin) in [#1733](https://github.com/jlowin/fastmcp/pull/1733) +* Add token_endpoint_auth_method parameter to OAuthProxy by [@jlowin](https://github.com/jlowin) in [#1736](https://github.com/jlowin/fastmcp/pull/1736) +* Clean up and enhance OAuth proxy tests with mock provider by [@jlowin](https://github.com/jlowin) in [#1738](https://github.com/jlowin/fastmcp/pull/1738) +### Fixes ๐Ÿž +* refactor: replace auth provider registry with ImportString by [@jlowin](https://github.com/jlowin) in [#1710](https://github.com/jlowin/fastmcp/pull/1710) +* Fix OAuth resource URL handling and WWW-Authenticate header by [@jlowin](https://github.com/jlowin) in [#1706](https://github.com/jlowin/fastmcp/pull/1706) +* Fix OAuth proxy client storage and add retry logic by [@jlowin](https://github.com/jlowin) in [#1732](https://github.com/jlowin/fastmcp/pull/1732) +### Docs ๐Ÿ“š +* Fix documentation: use StreamableHttpTransport for headers in testing by [@jlowin](https://github.com/jlowin) in [#1702](https://github.com/jlowin/fastmcp/pull/1702) +* docs: add performance warnings for mounted servers and proxies by [@strawgate](https://github.com/strawgate) in [#1669](https://github.com/jlowin/fastmcp/pull/1669) +* Update documentation around scopes for google by [@jlowin](https://github.com/jlowin) in [#1703](https://github.com/jlowin/fastmcp/pull/1703) +* Add deployment information to quickstart by [@seanpwlms](https://github.com/seanpwlms) in [#1433](https://github.com/jlowin/fastmcp/pull/1433) +* Update quickstart by [@jlowin](https://github.com/jlowin) in [#1728](https://github.com/jlowin/fastmcp/pull/1728) +* Add development docs for FastMCP by [@jlowin](https://github.com/jlowin) in [#1719](https://github.com/jlowin/fastmcp/pull/1719) +### Other Changes ๐Ÿฆพ +* Set generics without bounds to default=Any by [@strawgate](https://github.com/strawgate) in [#1648](https://github.com/jlowin/fastmcp/pull/1648) + +## New Contributors +* [@dmikusa](https://github.com/dmikusa) made their first contribution in [#1717](https://github.com/jlowin/fastmcp/pull/1717) +* [@seanpwlms](https://github.com/seanpwlms) made their first contribution in [#1433](https://github.com/jlowin/fastmcp/pull/1433) + +**Full Changelog**: [v2.12.0...v2.12.1](https://github.com/jlowin/fastmcp/compare/v2.12.0...v2.12.1) + + + + + +**[v2.12.0: Auth to the Races](https://github.com/jlowin/fastmcp/releases/tag/v2.12.0)** + +FastMCP 2.12 represents one of our most significant releases to date, both in scope and community involvement. After extensive testing and iteration with the community, we're shipping major improvements to authentication, configuration, and MCP feature adoption. + +๐Ÿ” **OAuth Proxy for Broader Provider Support** addresses a fundamental challenge: while MCP requires Dynamic Client Registration (DCR), many popular OAuth providers don't support it. The new OAuth proxy bridges this gap, enabling FastMCP servers to authenticate with providers like GitHub, Google, WorkOS, and Azure through minimal configuration. These native integrations ship today, with more providers planned based on community needs. + +๐Ÿ“‹ **Declarative JSON Configuration** introduces a standardized, portable way to describe and deploy MCP servers. The `fastmcp.json` configuration file becomes the single source of truth for dependencies, transport settings, entrypoints, and server metadata. This foundation sets the stage for future capabilities like transformations and remote sources, moving toward a world where MCP servers are as portable and shareable as container images. + +๐Ÿง  **Sampling API Fallback** tackles the chicken-and-egg problem limiting adoption of advanced MCP features. Samplingโ€”where servers request LLM completions from clientsโ€”is powerful but underutilized due to limited client support. FastMCP now lets server authors define fallback handlers that generate sampling completions server-side when clients don't support the feature, encouraging adoption while maintaining compatibility. + +This release took longer than usual to ship, and for good reason: the community's aggressive testing and feedback on the authentication system helped us reach a level of stability we're confident in. There's certainly more work ahead, but these foundations position FastMCP to handle increasingly complex use cases while remaining approachable for developers. + +Thank you to our new contributors and everyone who tested preview builds. Your feedback directly shaped these features. + +## What's Changed +### New Features ๐ŸŽ‰ +* Add OAuth proxy that allows authentication with social IDPs without DCR support by [@jlowin](https://github.com/jlowin) in [#1434](https://github.com/jlowin/fastmcp/pull/1434) +* feat: introduce declarative JSON configuration system by [@jlowin](https://github.com/jlowin) in [#1517](https://github.com/jlowin/fastmcp/pull/1517) +* โœจ Fallback to a Completions API when Sampling is not available by [@strawgate](https://github.com/strawgate) in [#1145](https://github.com/jlowin/fastmcp/pull/1145) +* Implement typed source system for FastMCP declarative configuration by [@jlowin](https://github.com/jlowin) in [#1607](https://github.com/jlowin/fastmcp/pull/1607) +### Enhancements ๐Ÿ”ง +* Support importing custom_route endpoints when mounting servers by [@jlowin](https://github.com/jlowin) in [#1470](https://github.com/jlowin/fastmcp/pull/1470) +* Remove unnecessary asserts by [@jlowin](https://github.com/jlowin) in [#1484](https://github.com/jlowin/fastmcp/pull/1484) +* Add Claude issue triage by [@jlowin](https://github.com/jlowin) in [#1510](https://github.com/jlowin/fastmcp/pull/1510) +* Inline dedupe prompt by [@jlowin](https://github.com/jlowin) in [#1512](https://github.com/jlowin/fastmcp/pull/1512) +* Improve stdio and mcp_config clean-up by [@strawgate](https://github.com/strawgate) in [#1444](https://github.com/jlowin/fastmcp/pull/1444) +* involve kwargs to pass parameters on creating RichHandler for logging customization. by [@itaru2622](https://github.com/itaru2622) in [#1504](https://github.com/jlowin/fastmcp/pull/1504) +* Move SDK docs generation to post-merge workflow by [@jlowin](https://github.com/jlowin) in [#1513](https://github.com/jlowin/fastmcp/pull/1513) +* Improve label triage guidance by [@jlowin](https://github.com/jlowin) in [#1516](https://github.com/jlowin/fastmcp/pull/1516) +* Add code review guidelines for agents by [@jlowin](https://github.com/jlowin) in [#1520](https://github.com/jlowin/fastmcp/pull/1520) +* Remove trailing slash in unit tests by [@jlowin](https://github.com/jlowin) in [#1535](https://github.com/jlowin/fastmcp/pull/1535) +* Update OAuth callback UI branding by [@jlowin](https://github.com/jlowin) in [#1536](https://github.com/jlowin/fastmcp/pull/1536) +* Fix Marvin workflow to support development tools by [@jlowin](https://github.com/jlowin) in [#1537](https://github.com/jlowin/fastmcp/pull/1537) +* Add mounted_components_raise_on_load_error setting for debugging by [@jlowin](https://github.com/jlowin) in [#1534](https://github.com/jlowin/fastmcp/pull/1534) +* feat: Add --workspace flag to fastmcp install cursor by [@jlowin](https://github.com/jlowin) in [#1522](https://github.com/jlowin/fastmcp/pull/1522) +* switch from `pyright` to `ty` by [@zzstoatzz](https://github.com/zzstoatzz) in [#1545](https://github.com/jlowin/fastmcp/pull/1545) +* feat: trigger Marvin workflow on PR body content by [@jlowin](https://github.com/jlowin) in [#1549](https://github.com/jlowin/fastmcp/pull/1549) +* Add WorkOS and Azure OAuth providers by [@jlowin](https://github.com/jlowin) in [#1550](https://github.com/jlowin/fastmcp/pull/1550) +* Adjust timeout for slow MCP Server shutdown test by [@strawgate](https://github.com/strawgate) in [#1561](https://github.com/jlowin/fastmcp/pull/1561) +* Update banner by [@jlowin](https://github.com/jlowin) in [#1567](https://github.com/jlowin/fastmcp/pull/1567) +* Added import of AuthProxy to auth __init__ by [@KaliszS](https://github.com/KaliszS) in [#1568](https://github.com/jlowin/fastmcp/pull/1568) +* Add configurable redirect URI validation for OAuth providers by [@jlowin](https://github.com/jlowin) in [#1582](https://github.com/jlowin/fastmcp/pull/1582) +* Remove invalid-argument-type ignore and fix type errors by [@jlowin](https://github.com/jlowin) in [#1588](https://github.com/jlowin/fastmcp/pull/1588) +* Remove generate-schema from public CLI by [@jlowin](https://github.com/jlowin) in [#1591](https://github.com/jlowin/fastmcp/pull/1591) +* Skip flaky windows test / mulit-client garbage collection by [@jlowin](https://github.com/jlowin) in [#1592](https://github.com/jlowin/fastmcp/pull/1592) +* Add setting to disable logging configuration by [@isra17](https://github.com/isra17) in [#1575](https://github.com/jlowin/fastmcp/pull/1575) +* Improve debug logging for nested Servers / Clients by [@strawgate](https://github.com/strawgate) in [#1604](https://github.com/jlowin/fastmcp/pull/1604) +* Add GitHub pull request template by [@strawgate](https://github.com/strawgate) in [#1581](https://github.com/jlowin/fastmcp/pull/1581) +* chore: Automate docs and schema updates via PRs by [@jlowin](https://github.com/jlowin) in [#1611](https://github.com/jlowin/fastmcp/pull/1611) +* Experiment with haiku for limited workflows by [@jlowin](https://github.com/jlowin) in [#1613](https://github.com/jlowin/fastmcp/pull/1613) +* feat: Improve GitHub workflow automation for schema and SDK docs by [@jlowin](https://github.com/jlowin) in [#1615](https://github.com/jlowin/fastmcp/pull/1615) +* Consolidate server loading logic into FileSystemSource by [@jlowin](https://github.com/jlowin) in [#1614](https://github.com/jlowin/fastmcp/pull/1614) +* Prevent Haiku Marvin from commenting when there are no duplicates by [@jlowin](https://github.com/jlowin) in [#1622](https://github.com/jlowin/fastmcp/pull/1622) +* chore: Add clarifying note to automated PR bodies by [@jlowin](https://github.com/jlowin) in [#1623](https://github.com/jlowin/fastmcp/pull/1623) +* feat: introduce inline snapshots by [@strawgate](https://github.com/strawgate) in [#1605](https://github.com/jlowin/fastmcp/pull/1605) +* Improve fastmcp.json environment configuration and project-based deployments by [@jlowin](https://github.com/jlowin) in [#1631](https://github.com/jlowin/fastmcp/pull/1631) +* fix: allow passing query params in OAuthProxy upstream authorization url by [@danb27](https://github.com/danb27) in [#1630](https://github.com/jlowin/fastmcp/pull/1630) +* Support multiple --with-editable flags in CLI commands by [@jlowin](https://github.com/jlowin) in [#1634](https://github.com/jlowin/fastmcp/pull/1634) +* feat: support comma separated oauth scopes by [@jlowin](https://github.com/jlowin) in [#1642](https://github.com/jlowin/fastmcp/pull/1642) +* Add allowed_client_redirect_uris to OAuth provider subclasses by [@jlowin](https://github.com/jlowin) in [#1662](https://github.com/jlowin/fastmcp/pull/1662) +* Consolidate CLI config parsing and prevent infinite loops by [@jlowin](https://github.com/jlowin) in [#1660](https://github.com/jlowin/fastmcp/pull/1660) +* Internal refactor: mcp server config by [@jlowin](https://github.com/jlowin) in [#1672](https://github.com/jlowin/fastmcp/pull/1672) +* Refactor Environment to support multiple runtime types by [@jlowin](https://github.com/jlowin) in [#1673](https://github.com/jlowin/fastmcp/pull/1673) +* Add type field to Environment base class by [@jlowin](https://github.com/jlowin) in [#1676](https://github.com/jlowin/fastmcp/pull/1676) +### Fixes ๐Ÿž +* Fix breaking change: restore output_schema=False compatibility by [@jlowin](https://github.com/jlowin) in [#1482](https://github.com/jlowin/fastmcp/pull/1482) +* Fix #1506: Update tool filtering documentation from _meta to meta by [@maybenotconnor](https://github.com/maybenotconnor) in [#1511](https://github.com/jlowin/fastmcp/pull/1511) +* Fix pytest warnings by [@jlowin](https://github.com/jlowin) in [#1559](https://github.com/jlowin/fastmcp/pull/1559) +* nest schemas under assets by [@jlowin](https://github.com/jlowin) in [#1593](https://github.com/jlowin/fastmcp/pull/1593) +* Skip flaky windows test by [@jlowin](https://github.com/jlowin) in [#1596](https://github.com/jlowin/fastmcp/pull/1596) +* ACTUALLY move schemas to fastmcp.json by [@jlowin](https://github.com/jlowin) in [#1597](https://github.com/jlowin/fastmcp/pull/1597) +* Fix and centralize CLI path resolution by [@jlowin](https://github.com/jlowin) in [#1590](https://github.com/jlowin/fastmcp/pull/1590) +* Remove client info modifications by [@jlowin](https://github.com/jlowin) in [#1620](https://github.com/jlowin/fastmcp/pull/1620) +* Fix $defs being discarded in input schema of transformed tool by [@pldesch-chift](https://github.com/pldesch-chift) in [#1578](https://github.com/jlowin/fastmcp/pull/1578) +* Fix enum elicitation to use inline schemas for MCP compatibility by [@jlowin](https://github.com/jlowin) in [#1632](https://github.com/jlowin/fastmcp/pull/1632) +* Reuse session for `StdioTransport` in `Client.new` by [@strawgate](https://github.com/strawgate) in [#1635](https://github.com/jlowin/fastmcp/pull/1635) +* Feat: Configurable LoggingMiddleware payload serialization by [@vl-kp](https://github.com/vl-kp) in [#1636](https://github.com/jlowin/fastmcp/pull/1636) +* Fix OAuth redirect URI validation for DCR compatibility by [@jlowin](https://github.com/jlowin) in [#1661](https://github.com/jlowin/fastmcp/pull/1661) +* Add default scope handling in OAuth proxy by [@romanusyk](https://github.com/romanusyk) in [#1667](https://github.com/jlowin/fastmcp/pull/1667) +* Fix OAuth token expiry handling by [@jlowin](https://github.com/jlowin) in [#1671](https://github.com/jlowin/fastmcp/pull/1671) +* Add resource_server_url parameter to OAuth proxy providers by [@jlowin](https://github.com/jlowin) in [#1682](https://github.com/jlowin/fastmcp/pull/1682) +### Breaking Changes ๐Ÿ›ซ +* Enhance inspect command with structured output and format options by [@jlowin](https://github.com/jlowin) in [#1481](https://github.com/jlowin/fastmcp/pull/1481) +### Docs ๐Ÿ“š +* Update changelog by [@jlowin](https://github.com/jlowin) in [#1453](https://github.com/jlowin/fastmcp/pull/1453) +* Update banner by [@jlowin](https://github.com/jlowin) in [#1472](https://github.com/jlowin/fastmcp/pull/1472) +* Update logo files by [@jlowin](https://github.com/jlowin) in [#1473](https://github.com/jlowin/fastmcp/pull/1473) +* Update deployment docs by [@jlowin](https://github.com/jlowin) in [#1486](https://github.com/jlowin/fastmcp/pull/1486) +* Update FastMCP Cloud screenshot by [@jlowin](https://github.com/jlowin) in [#1487](https://github.com/jlowin/fastmcp/pull/1487) +* Update authentication note in docs by [@jlowin](https://github.com/jlowin) in [#1488](https://github.com/jlowin/fastmcp/pull/1488) +* chore: Update installation.mdx version snippet by [@thomas-te](https://github.com/thomas-te) in [#1496](https://github.com/jlowin/fastmcp/pull/1496) +* Update fastmcp cloud server requirements by [@jlowin](https://github.com/jlowin) in [#1497](https://github.com/jlowin/fastmcp/pull/1497) +* Fix oauth pyright type checking by [@strawgate](https://github.com/strawgate) in [#1498](https://github.com/jlowin/fastmcp/pull/1498) +* docs: Fix type annotation in return value documentation by [@MaikelVeen](https://github.com/MaikelVeen) in [#1499](https://github.com/jlowin/fastmcp/pull/1499) +* Fix PromptMessage usage in docs example by [@jlowin](https://github.com/jlowin) in [#1515](https://github.com/jlowin/fastmcp/pull/1515) +* Create CODE_OF_CONDUCT.md by [@jlowin](https://github.com/jlowin) in [#1523](https://github.com/jlowin/fastmcp/pull/1523) +* Fixed wrong import path in new docs page by [@KaliszS](https://github.com/KaliszS) in [#1538](https://github.com/jlowin/fastmcp/pull/1538) +* Document symmetric key JWT verification support by [@jlowin](https://github.com/jlowin) in [#1586](https://github.com/jlowin/fastmcp/pull/1586) +* Update fastmcp.json schema path by [@jlowin](https://github.com/jlowin) in [#1595](https://github.com/jlowin/fastmcp/pull/1595) +### Dependencies ๐Ÿ“ฆ +* Bump actions/create-github-app-token from 1 to 2 by [@dependabot](https://github.com/dependabot)[bot] in [#1436](https://github.com/jlowin/fastmcp/pull/1436) +* Bump astral-sh/setup-uv from 4 to 6 by [@dependabot](https://github.com/dependabot)[bot] in [#1532](https://github.com/jlowin/fastmcp/pull/1532) +* Bump actions/checkout from 4 to 5 by [@dependabot](https://github.com/dependabot)[bot] in [#1533](https://github.com/jlowin/fastmcp/pull/1533) +### Other Changes ๐Ÿฆพ +* Add dedupe workflow by [@jlowin](https://github.com/jlowin) in [#1454](https://github.com/jlowin/fastmcp/pull/1454) +* Update AGENTS.md by [@jlowin](https://github.com/jlowin) in [#1471](https://github.com/jlowin/fastmcp/pull/1471) +* Give Marvin the power of the Internet by [@strawgate](https://github.com/strawgate) in [#1475](https://github.com/jlowin/fastmcp/pull/1475) +* Update `just` error message for static checks by [@jlowin](https://github.com/jlowin) in [#1483](https://github.com/jlowin/fastmcp/pull/1483) +* Remove labeler by [@jlowin](https://github.com/jlowin) in [#1509](https://github.com/jlowin/fastmcp/pull/1509) +* update aproto server to handle rich links by [@zzstoatzz](https://github.com/zzstoatzz) in [#1556](https://github.com/jlowin/fastmcp/pull/1556) +* fix: enable triage bot for fork PRs using pull_request_target by [@jlowin](https://github.com/jlowin) in [#1557](https://github.com/jlowin/fastmcp/pull/1557) + +## New Contributors +* [@thomas-te](https://github.com/thomas-te) made their first contribution in [#1496](https://github.com/jlowin/fastmcp/pull/1496) +* [@maybenotconnor](https://github.com/maybenotconnor) made their first contribution in [#1511](https://github.com/jlowin/fastmcp/pull/1511) +* [@MaikelVeen](https://github.com/MaikelVeen) made their first contribution in [#1499](https://github.com/jlowin/fastmcp/pull/1499) +* [@KaliszS](https://github.com/KaliszS) made their first contribution in [#1538](https://github.com/jlowin/fastmcp/pull/1538) +* [@isra17](https://github.com/isra17) made their first contribution in [#1575](https://github.com/jlowin/fastmcp/pull/1575) +* [@marvin-context-protocol](https://github.com/marvin-context-protocol)[bot] made their first contribution in [#1616](https://github.com/jlowin/fastmcp/pull/1616) +* [@pldesch-chift](https://github.com/pldesch-chift) made their first contribution in [#1578](https://github.com/jlowin/fastmcp/pull/1578) +* [@vl-kp](https://github.com/vl-kp) made their first contribution in [#1636](https://github.com/jlowin/fastmcp/pull/1636) +* [@romanusyk](https://github.com/romanusyk) made their first contribution in [#1667](https://github.com/jlowin/fastmcp/pull/1667) + +**Full Changelog**: [v2.11.3...v2.12.0](https://github.com/jlowin/fastmcp/compare/v2.11.3...v2.12.0) + + + + + +**[v2.11.3: API-tite for Change](https://github.com/jlowin/fastmcp/releases/tag/v2.11.3)** + +This release includes significant enhancements to the experimental OpenAPI parser and fixes a significant bug that led schemas not to be included in input/output schemas if they were transitive dependencies (e.g. A โ†’ B โ†’ C implies A depends on C). For users naively transforming large OpenAPI specs into MCP servers, this may result in ballooning payload sizes and necessitate curation. + +## What's Changed +### Enhancements ๐Ÿ”ง +* Improve redirect handling to address 307's by [@jlowin](https://github.com/jlowin) in [#1387](https://github.com/jlowin/fastmcp/pull/1387) +* Ensure resource + template names are properly prefixed when importing/mounting by [@jlowin](https://github.com/jlowin) in [#1423](https://github.com/jlowin/fastmcp/pull/1423) +* fixes #1398: Add JWT claims to AccessToken by [@panargirakis](https://github.com/panargirakis) in [#1399](https://github.com/jlowin/fastmcp/pull/1399) +* Enable Protected Resource Metadata to provide resource_name and resouโ€ฆ by [@yannj-fr](https://github.com/yannj-fr) in [#1371](https://github.com/jlowin/fastmcp/pull/1371) +* Pin mcp SDK under 2.0 to avoid breaking changes by [@jlowin](https://github.com/jlowin) in [#1428](https://github.com/jlowin/fastmcp/pull/1428) +* Clean up complexity from PR #1426 by [@jlowin](https://github.com/jlowin) in [#1435](https://github.com/jlowin/fastmcp/pull/1435) +* Optimize OpenAPI payload size by 46% by [@jlowin](https://github.com/jlowin) in [#1452](https://github.com/jlowin/fastmcp/pull/1452) +* Update static checks by [@jlowin](https://github.com/jlowin) in [#1448](https://github.com/jlowin/fastmcp/pull/1448) +### Fixes ๐Ÿž +* Fix client-side logging bug #1394 by [@chi2liu](https://github.com/chi2liu) in [#1397](https://github.com/jlowin/fastmcp/pull/1397) +* fix: Fix httpx_client_factory type annotation to match MCP SDK (#1402) by [@chi2liu](https://github.com/chi2liu) in [#1405](https://github.com/jlowin/fastmcp/pull/1405) +* Fix OpenAPI allOf handling at requestBody top level (#1378) by [@chi2liu](https://github.com/chi2liu) in [#1425](https://github.com/jlowin/fastmcp/pull/1425) +* Fix OpenAPI transitive references and performance (#1372) by [@jlowin](https://github.com/jlowin) in [#1426](https://github.com/jlowin/fastmcp/pull/1426) +* fix(type): lifespan is partially unknown by [@ykun9](https://github.com/ykun9) in [#1389](https://github.com/jlowin/fastmcp/pull/1389) +* Ensure transformed tools generate structured content by [@jlowin](https://github.com/jlowin) in [#1443](https://github.com/jlowin/fastmcp/pull/1443) +### Docs ๐Ÿ“š +* docs(client/logging): reflect corrected default log level mapping by [@jlowin](https://github.com/jlowin) in [#1403](https://github.com/jlowin/fastmcp/pull/1403) +* Add documentation for get_access_token() dependency function by [@jlowin](https://github.com/jlowin) in [#1446](https://github.com/jlowin/fastmcp/pull/1446) +### Other Changes ๐Ÿฆพ +* Add comprehensive tests for utilities.components module by [@chi2liu](https://github.com/chi2liu) in [#1395](https://github.com/jlowin/fastmcp/pull/1395) +* Consolidate agent instructions into AGENTS.md by [@jlowin](https://github.com/jlowin) in [#1404](https://github.com/jlowin/fastmcp/pull/1404) +* Fix performance test threshold to prevent flaky failures by [@jlowin](https://github.com/jlowin) in [#1406](https://github.com/jlowin/fastmcp/pull/1406) +* Update agents.md; add github instructions by [@jlowin](https://github.com/jlowin) in [#1410](https://github.com/jlowin/fastmcp/pull/1410) +* Add Marvin assistant by [@jlowin](https://github.com/jlowin) in [#1412](https://github.com/jlowin/fastmcp/pull/1412) +* Marvin: fix deprecated variable names by [@jlowin](https://github.com/jlowin) in [#1417](https://github.com/jlowin/fastmcp/pull/1417) +* Simplify action setup and add github tools for Marvin by [@jlowin](https://github.com/jlowin) in [#1419](https://github.com/jlowin/fastmcp/pull/1419) +* Update marvin workflow name by [@jlowin](https://github.com/jlowin) in [#1421](https://github.com/jlowin/fastmcp/pull/1421) +* Improve GitHub templates by [@jlowin](https://github.com/jlowin) in [#1422](https://github.com/jlowin/fastmcp/pull/1422) + +## New Contributors +* [@panargirakis](https://github.com/panargirakis) made their first contribution in [#1399](https://github.com/jlowin/fastmcp/pull/1399) +* [@ykun9](https://github.com/ykun9) made their first contribution in [#1389](https://github.com/jlowin/fastmcp/pull/1389) +* [@yannj-fr](https://github.com/yannj-fr) made their first contribution in [#1371](https://github.com/jlowin/fastmcp/pull/1371) + +**Full Changelog**: [v2.11.2...v2.11.3](https://github.com/jlowin/fastmcp/compare/v2.11.2...v2.11.3) + + + + + +## [v2.11.2: Satis-factory](https://github.com/jlowin/fastmcp/releases/tag/v2.11.2) + +## What's Changed +### Enhancements ๐Ÿ”ง +* Support factory functions in fastmcp run by [@jlowin](https://github.com/jlowin) in [#1384](https://github.com/jlowin/fastmcp/pull/1384) +* Add async support to client_factory in FastMCPProxy (#1286) by [@bianning](https://github.com/bianning) in [#1375](https://github.com/jlowin/fastmcp/pull/1375) +### Fixes ๐Ÿž +* Fix server_version field in inspect manifest by [@jlowin](https://github.com/jlowin) in [#1383](https://github.com/jlowin/fastmcp/pull/1383) +* Fix Settings field with both default and default_factory by [@jlowin](https://github.com/jlowin) in [#1380](https://github.com/jlowin/fastmcp/pull/1380) +### Other Changes ๐Ÿฆพ +* Remove unused arg by [@jlowin](https://github.com/jlowin) in [#1382](https://github.com/jlowin/fastmcp/pull/1382) +* Add remote auth provider tests by [@jlowin](https://github.com/jlowin) in [#1351](https://github.com/jlowin/fastmcp/pull/1351) + +## New Contributors +* [@bianning](https://github.com/bianning) made their first contribution in [#1375](https://github.com/jlowin/fastmcp/pull/1375) + +**Full Changelog**: [v2.11.1...v2.11.2](https://github.com/jlowin/fastmcp/compare/v2.11.1...v2.11.2) + + + + + +## [v2.11.1: You're Better Auth Now](https://github.com/jlowin/fastmcp/releases/tag/v2.11.1) + +## What's Changed +### New Features ๐ŸŽ‰ +* Introduce `RemoteAuthProvider` for cleaner external identity provider integration, update docs by [@jlowin](https://github.com/jlowin) in [#1346](https://github.com/jlowin/fastmcp/pull/1346) +### Enhancements ๐Ÿ”ง +* perf: optimize string operations in OpenAPI parameter processing by [@chi2liu](https://github.com/chi2liu) in [#1342](https://github.com/jlowin/fastmcp/pull/1342) +### Fixes ๐Ÿž +* Fix method-bound FunctionTool schemas by [@strawgate](https://github.com/strawgate) in [#1360](https://github.com/jlowin/fastmcp/pull/1360) +* Manually set `_key` after `model_copy()` to enable prefixing Transformed Tools by [@strawgate](https://github.com/strawgate) in [#1357](https://github.com/jlowin/fastmcp/pull/1357) +### Docs ๐Ÿ“š +* Docs updates by [@jlowin](https://github.com/jlowin) in [#1336](https://github.com/jlowin/fastmcp/pull/1336) +* Add 2.11 to changelog by [@jlowin](https://github.com/jlowin) in [#1337](https://github.com/jlowin/fastmcp/pull/1337) +* Update AuthKit vocab by [@jlowin](https://github.com/jlowin) in [#1338](https://github.com/jlowin/fastmcp/pull/1338) +* Fix typo in decorating-methods.mdx by [@Ozzuke](https://github.com/Ozzuke) in [#1344](https://github.com/jlowin/fastmcp/pull/1344) + +## New Contributors +* [@Ozzuke](https://github.com/Ozzuke) made their first contribution in [#1344](https://github.com/jlowin/fastmcp/pull/1344) + +**Full Changelog**: [v2.11.0...v2.11.1](https://github.com/jlowin/fastmcp/compare/v2.11.0...v2.11.1) + + + + + +## [v2.11.0: Auth to a Good Start](https://github.com/jlowin/fastmcp/releases/tag/v2.11.0) + +FastMCP 2.11 doubles down on what developers need most: speed and simplicity. This massive release delivers significant performance improvements and a dramatically better developer experience. + +๐Ÿ” **Enterprise-Ready Authentication** brings comprehensive OAuth 2.1 support with WorkOS's AuthKit integration. The new AuthProvider interface leverages MCP's support for separate resource and authorization servers, handling API keys and remote authentication with Dynamic Client Registration. AuthKit integration means you can plug into existing enterprise identity systems without rebuilding your auth stack, setting the stage for plug-and-play auth that doesn't require users to become security experts overnight. + +โšก The **Experimental OpenAPI Parser** delivers dramatic performance improvements through single-pass schema processing and optimized memory usage. OpenAPI integrations are now significantly faster, with cleaner, more maintainable code. _(Note: the experimental parser is disabled by default, set `FASTMCPEXPERIMENTALENABLENEWOPENAPIPARSER=1` to enable it. A message will be shown to all users on the legacy parser encouraging them to try the new one before it becomes the default.)_ + +๐Ÿง  **Context State Management** finally gives you persistent state across tool calls with a simple dict interface, while enhanced meta support lets you expose rich component metadata to clients. Combined with improved type annotations, string-based argument descriptions, and UV transport support, this release makes FastMCP feel more intuitive than ever. + +This release represents a TON of community contributions and sets the foundation for even more ambitious features ahead. + +## What's Changed +### New Features ๐ŸŽ‰ +* Introduce experimental OpenAPI parser with improved performance and maintainability by [@jlowin](https://github.com/jlowin) in [#1209](https://github.com/jlowin/fastmcp/pull/1209) +* Add state dict to Context (#1118) by [@mukulmurthy](https://github.com/mukulmurthy) in [#1160](https://github.com/jlowin/fastmcp/pull/1160) +* Expose FastMCP tags to clients via component `meta` dict by [@jlowin](https://github.com/jlowin) in [#1281](https://github.com/jlowin/fastmcp/pull/1281) +* Add _fastmcp meta namespace by [@jlowin](https://github.com/jlowin) in [#1290](https://github.com/jlowin/fastmcp/pull/1290) +* Add TokenVerifier protocol support alongside existing OAuthProvider authentication by [@jlowin](https://github.com/jlowin) in [#1297](https://github.com/jlowin/fastmcp/pull/1297) +* Add comprehensive OAuth 2.1 authentication system with WorkOS integration by [@jlowin](https://github.com/jlowin) in [#1327](https://github.com/jlowin/fastmcp/pull/1327) +### Enhancements ๐Ÿ”ง +* [๐Ÿถ] Transform MCP Server Tools by [@strawgate](https://github.com/strawgate) in [#1132](https://github.com/jlowin/fastmcp/pull/1132) +* Add --python, --project, and --with-requirements options to CLI commands by [@jlowin](https://github.com/jlowin) in [#1190](https://github.com/jlowin/fastmcp/pull/1190) +* Support `fastmcp run mcp.json` by [@strawgate](https://github.com/strawgate) in [#1138](https://github.com/jlowin/fastmcp/pull/1138) +* Support from __future__ import annotations by [@jlowin](https://github.com/jlowin) in [#1199](https://github.com/jlowin/fastmcp/pull/1199) +* Optimize OpenAPI parser performance with single-pass schema processing by [@jlowin](https://github.com/jlowin) in [#1214](https://github.com/jlowin/fastmcp/pull/1214) +* Log tool name on transform validation error by [@strawgate](https://github.com/strawgate) in [#1238](https://github.com/jlowin/fastmcp/pull/1238) +* Refactor `get_http_request` and `context.session_id` by [@hopeful0](https://github.com/hopeful0) in [#1242](https://github.com/jlowin/fastmcp/pull/1242) +* Support creating tool argument descriptions from string annotations by [@jlowin](https://github.com/jlowin) in [#1255](https://github.com/jlowin/fastmcp/pull/1255) +* feat: Add Annotations support for resources and resource templates by [@chughtapan](https://github.com/chughtapan) in [#1260](https://github.com/jlowin/fastmcp/pull/1260) +* Add UV Transport by [@strawgate](https://github.com/strawgate) in [#1270](https://github.com/jlowin/fastmcp/pull/1270) +* Improve OpenAPI-to-JSONSchema conversion utilities by [@jlowin](https://github.com/jlowin) in [#1283](https://github.com/jlowin/fastmcp/pull/1283) +* Ensure proxy components forward meta dicts by [@jlowin](https://github.com/jlowin) in [#1282](https://github.com/jlowin/fastmcp/pull/1282) +* fix: server argument passing in CLI run command by [@chughtapan](https://github.com/chughtapan) in [#1293](https://github.com/jlowin/fastmcp/pull/1293) +* Add meta support to tool transformation utilities by [@jlowin](https://github.com/jlowin) in [#1295](https://github.com/jlowin/fastmcp/pull/1295) +* feat: Allow Resource Metadata URL as field in OAuthProvider by [@dacamposol](https://github.com/dacamposol) in [#1287](https://github.com/jlowin/fastmcp/pull/1287) +* Use a simple overwrite instead of a merge for meta by [@jlowin](https://github.com/jlowin) in [#1296](https://github.com/jlowin/fastmcp/pull/1296) +* Remove unused TimedCache by [@strawgate](https://github.com/strawgate) in [#1303](https://github.com/jlowin/fastmcp/pull/1303) +* refactor: standardize logging usage across OpenAPI utilities by [@chi2liu](https://github.com/chi2liu) in [#1322](https://github.com/jlowin/fastmcp/pull/1322) +* perf: optimize OpenAPI parsing by reducing dict copy operations by [@chi2liu](https://github.com/chi2liu) in [#1321](https://github.com/jlowin/fastmcp/pull/1321) +* Structured client-side logging by [@cjermain](https://github.com/cjermain) in [#1326](https://github.com/jlowin/fastmcp/pull/1326) +### Fixes ๐Ÿž +* fix: preserve def reference when referenced in allOf / oneOf / anyOf by [@algirdasci](https://github.com/algirdasci) in [#1208](https://github.com/jlowin/fastmcp/pull/1208) +* fix: add type hint to custom_route decorator by [@zzstoatzz](https://github.com/zzstoatzz) in [#1210](https://github.com/jlowin/fastmcp/pull/1210) +* chore: typo by [@richardkmichael](https://github.com/richardkmichael) in [#1216](https://github.com/jlowin/fastmcp/pull/1216) +* fix: handle non-string $ref values in experimental OpenAPI parser by [@jlowin](https://github.com/jlowin) in [#1217](https://github.com/jlowin/fastmcp/pull/1217) +* Skip repeated type conversion and validation in proxy client elicitation handler by [@chughtapan](https://github.com/chughtapan) in [#1222](https://github.com/jlowin/fastmcp/pull/1222) +* Ensure default fields are not marked nullable by [@jlowin](https://github.com/jlowin) in [#1224](https://github.com/jlowin/fastmcp/pull/1224) +* Fix stateful proxy client mixing in multi-proxies sessions by [@hopeful0](https://github.com/hopeful0) in [#1245](https://github.com/jlowin/fastmcp/pull/1245) +* Fix invalid async context manager usage in proxy documentation by [@zzstoatzz](https://github.com/zzstoatzz) in [#1246](https://github.com/jlowin/fastmcp/pull/1246) +* fix: experimental FastMCPOpenAPI server lost headers in request when __init__(client with headers) by [@itaru2622](https://github.com/itaru2622) in [#1254](https://github.com/jlowin/fastmcp/pull/1254) +* Fix typing, add tests for tool call middleware by [@jlowin](https://github.com/jlowin) in [#1269](https://github.com/jlowin/fastmcp/pull/1269) +* Fix: prune hidden parameter defs by [@muhammadkhalid-03](https://github.com/muhammadkhalid-03) in [#1257](https://github.com/jlowin/fastmcp/pull/1257) +* Fix nullable field handling in OpenAPI to JSON Schema conversion by [@jlowin](https://github.com/jlowin) in [#1279](https://github.com/jlowin/fastmcp/pull/1279) +* Ensure fastmcp run supports v1 servers by [@jlowin](https://github.com/jlowin) in [#1332](https://github.com/jlowin/fastmcp/pull/1332) +### Breaking Changes ๐Ÿ›ซ +* Change server flag to --name by [@jlowin](https://github.com/jlowin) in [#1248](https://github.com/jlowin/fastmcp/pull/1248) +### Docs ๐Ÿ“š +* Remove unused import from FastAPI integration documentation by [@mariotaddeucci](https://github.com/mariotaddeucci) in [#1194](https://github.com/jlowin/fastmcp/pull/1194) +* Update fastapi docs by [@jlowin](https://github.com/jlowin) in [#1198](https://github.com/jlowin/fastmcp/pull/1198) +* Add docs for context state management by [@jlowin](https://github.com/jlowin) in [#1227](https://github.com/jlowin/fastmcp/pull/1227) +* Permit.io integration docs by [@orweis](https://github.com/orweis) in [#1226](https://github.com/jlowin/fastmcp/pull/1226) +* Update docs to reflect sync tools by [@jlowin](https://github.com/jlowin) in [#1234](https://github.com/jlowin/fastmcp/pull/1234) +* Update changelog.mdx by [@jlowin](https://github.com/jlowin) in [#1235](https://github.com/jlowin/fastmcp/pull/1235) +* Update SDK docs by [@jlowin](https://github.com/jlowin) in [#1236](https://github.com/jlowin/fastmcp/pull/1236) +* Update --name flag documentation for Cursor/Claude by [@adam-conway](https://github.com/adam-conway) in [#1239](https://github.com/jlowin/fastmcp/pull/1239) +* Add annotations docs by [@jlowin](https://github.com/jlowin) in [#1268](https://github.com/jlowin/fastmcp/pull/1268) +* Update openapi/fastapi URLs README.md by [@jbn](https://github.com/jbn) in [#1278](https://github.com/jlowin/fastmcp/pull/1278) +* Add 2.11 version badge for state management by [@jlowin](https://github.com/jlowin) in [#1289](https://github.com/jlowin/fastmcp/pull/1289) +* Add meta parameter support to tools, resources, templates, and prompts decorators by [@jlowin](https://github.com/jlowin) in [#1294](https://github.com/jlowin/fastmcp/pull/1294) +* docs: update get_state and set_state references by [@Maxi91f](https://github.com/Maxi91f) in [#1306](https://github.com/jlowin/fastmcp/pull/1306) +* Add unit tests and docs for denying tool calls with middleware by [@jlowin](https://github.com/jlowin) in [#1333](https://github.com/jlowin/fastmcp/pull/1333) +* Remove reference to stacked decorators by [@jlowin](https://github.com/jlowin) in [#1334](https://github.com/jlowin/fastmcp/pull/1334) +* Eunomia authorization server can run embedded within the MCP server by [@tommitt](https://github.com/tommitt) in [#1317](https://github.com/jlowin/fastmcp/pull/1317) +### Other Changes ๐Ÿฆพ +* Update README.md by [@jlowin](https://github.com/jlowin) in [#1230](https://github.com/jlowin/fastmcp/pull/1230) +* Logcapture addition to test_server file by [@Sourav-Tripathy](https://github.com/Sourav-Tripathy) in [#1229](https://github.com/jlowin/fastmcp/pull/1229) +* Add tests for headers with both legacy and experimental openapi parser by [@jlowin](https://github.com/jlowin) in [#1259](https://github.com/jlowin/fastmcp/pull/1259) +* Small clean-up from MCP Tool Transform PR by [@strawgate](https://github.com/strawgate) in [#1267](https://github.com/jlowin/fastmcp/pull/1267) +* Add test for proxy tags visibility by [@jlowin](https://github.com/jlowin) in [#1302](https://github.com/jlowin/fastmcp/pull/1302) +* Add unit test for sampling with image messages by [@jlowin](https://github.com/jlowin) in [#1329](https://github.com/jlowin/fastmcp/pull/1329) +* Remove redundant resource_metadata_url assignment by [@jlowin](https://github.com/jlowin) in [#1328](https://github.com/jlowin/fastmcp/pull/1328) +* Update bug.yml by [@jlowin](https://github.com/jlowin) in [#1331](https://github.com/jlowin/fastmcp/pull/1331) +* Ensure validation errors are raised when masked by [@jlowin](https://github.com/jlowin) in [#1330](https://github.com/jlowin/fastmcp/pull/1330) + +## New Contributors +* [@mariotaddeucci](https://github.com/mariotaddeucci) made their first contribution in [#1194](https://github.com/jlowin/fastmcp/pull/1194) +* [@algirdasci](https://github.com/algirdasci) made their first contribution in [#1208](https://github.com/jlowin/fastmcp/pull/1208) +* [@chughtapan](https://github.com/chughtapan) made their first contribution in [#1222](https://github.com/jlowin/fastmcp/pull/1222) +* [@mukulmurthy](https://github.com/mukulmurthy) made their first contribution in [#1160](https://github.com/jlowin/fastmcp/pull/1160) +* [@orweis](https://github.com/orweis) made their first contribution in [#1226](https://github.com/jlowin/fastmcp/pull/1226) +* [@Sourav-Tripathy](https://github.com/Sourav-Tripathy) made their first contribution in [#1229](https://github.com/jlowin/fastmcp/pull/1229) +* [@adam-conway](https://github.com/adam-conway) made their first contribution in [#1239](https://github.com/jlowin/fastmcp/pull/1239) +* [@muhammadkhalid-03](https://github.com/muhammadkhalid-03) made their first contribution in [#1257](https://github.com/jlowin/fastmcp/pull/1257) +* [@jbn](https://github.com/jbn) made their first contribution in [#1278](https://github.com/jlowin/fastmcp/pull/1278) +* [@dacamposol](https://github.com/dacamposol) made their first contribution in [#1287](https://github.com/jlowin/fastmcp/pull/1287) +* [@chi2liu](https://github.com/chi2liu) made their first contribution in [#1322](https://github.com/jlowin/fastmcp/pull/1322) +* [@cjermain](https://github.com/cjermain) made their first contribution in [#1326](https://github.com/jlowin/fastmcp/pull/1326) + +**Full Changelog**: [v2.10.6...v2.11.0](https://github.com/jlowin/fastmcp/compare/v2.10.6...v2.11.0) + + + + + +## [v2.10.6: Hymn for the Weekend](https://github.com/jlowin/fastmcp/releases/tag/v2.10.6) + +A special Saturday release with many fixes. + +## What's Changed +### Enhancements ๐Ÿ”ง +* Resolve #1139 -- Implement include_context argument in Context.sample by [@codingjoe](https://github.com/codingjoe) in [#1141](https://github.com/jlowin/fastmcp/pull/1141) +* feat(settings): add log level normalization by [@ka2048](https://github.com/ka2048) in [#1171](https://github.com/jlowin/fastmcp/pull/1171) +* add server name to mounted server warnings by [@artificial-aidan](https://github.com/artificial-aidan) in [#1147](https://github.com/jlowin/fastmcp/pull/1147) +* Add StatefulProxyClient by [@hopeful0](https://github.com/hopeful0) in [#1109](https://github.com/jlowin/fastmcp/pull/1109) +### Fixes ๐Ÿž +* Fix OpenAPI empty parameters by [@FabrizioSandri](https://github.com/FabrizioSandri) in [#1128](https://github.com/jlowin/fastmcp/pull/1128) +* Fix title field preservation in tool transformations by [@jlowin](https://github.com/jlowin) in [#1131](https://github.com/jlowin/fastmcp/pull/1131) +* Fix optional parameter validation in OpenAPI integration by [@jlowin](https://github.com/jlowin) in [#1135](https://github.com/jlowin/fastmcp/pull/1135) +* Do not silently exclude the "context" key from JSON body by [@melkamar](https://github.com/melkamar) in [#1153](https://github.com/jlowin/fastmcp/pull/1153) +* Fix tool output schema generation to respect Pydantic serialization aliases by [@zzstoatzz](https://github.com/zzstoatzz) in [#1148](https://github.com/jlowin/fastmcp/pull/1148) +* fix: _replace_ref_with_defs; ensure ref_path is string by [@itaru2622](https://github.com/itaru2622) in [#1164](https://github.com/jlowin/fastmcp/pull/1164) +* Fix nesting when making OpenAPI arrays and objects optional by [@melkamar](https://github.com/melkamar) in [#1178](https://github.com/jlowin/fastmcp/pull/1178) +* Fix `mcp-json` output format to include server name by [@jlowin](https://github.com/jlowin) in [#1185](https://github.com/jlowin/fastmcp/pull/1185) +* Only configure logging one time by [@jlowin](https://github.com/jlowin) in [#1187](https://github.com/jlowin/fastmcp/pull/1187) +### Docs ๐Ÿ“š +* Update changelog.mdx by [@jlowin](https://github.com/jlowin) in [#1127](https://github.com/jlowin/fastmcp/pull/1127) +* Eunomia Authorization with native FastMCP's Middleware by [@tommitt](https://github.com/tommitt) in [#1144](https://github.com/jlowin/fastmcp/pull/1144) +* update api ref for new `mdxify` version by [@zzstoatzz](https://github.com/zzstoatzz) in [#1182](https://github.com/jlowin/fastmcp/pull/1182) +### Other Changes ๐Ÿฆพ +* Expand empty parameter filtering and add comprehensive tests by [@jlowin](https://github.com/jlowin) in [#1129](https://github.com/jlowin/fastmcp/pull/1129) +* Add no-commit-to-branch hook by [@zzstoatzz](https://github.com/zzstoatzz) in [#1149](https://github.com/jlowin/fastmcp/pull/1149) +* Update README.md by [@jlowin](https://github.com/jlowin) in [#1165](https://github.com/jlowin/fastmcp/pull/1165) +* skip on rate limit by [@zzstoatzz](https://github.com/zzstoatzz) in [#1183](https://github.com/jlowin/fastmcp/pull/1183) +* Remove deprecated proxy creation by [@jlowin](https://github.com/jlowin) in [#1186](https://github.com/jlowin/fastmcp/pull/1186) +* Separate integration tests from unit tests in CI by [@jlowin](https://github.com/jlowin) in [#1188](https://github.com/jlowin/fastmcp/pull/1188) + +## New Contributors +* [@FabrizioSandri](https://github.com/FabrizioSandri) made their first contribution in [#1128](https://github.com/jlowin/fastmcp/pull/1128) +* [@melkamar](https://github.com/melkamar) made their first contribution in [#1153](https://github.com/jlowin/fastmcp/pull/1153) +* [@codingjoe](https://github.com/codingjoe) made their first contribution in [#1141](https://github.com/jlowin/fastmcp/pull/1141) +* [@itaru2622](https://github.com/itaru2622) made their first contribution in [#1164](https://github.com/jlowin/fastmcp/pull/1164) +* [@ka2048](https://github.com/ka2048) made their first contribution in [#1171](https://github.com/jlowin/fastmcp/pull/1171) +* [@artificial-aidan](https://github.com/artificial-aidan) made their first contribution in [#1147](https://github.com/jlowin/fastmcp/pull/1147) + +**Full Changelog**: [v2.10.5...v2.10.6](https://github.com/jlowin/fastmcp/compare/v2.10.5...v2.10.6) + + + + + +## [v2.10.5: Middle Management](https://github.com/jlowin/fastmcp/releases/tag/v2.10.5) + +A maintenance release focused on OpenAPI refinements and middleware fixes, plus console improvements. + +## What's Changed +### Enhancements ๐Ÿ”ง +* Fix Claude Code CLI detection for npm global installations by [@jlowin](https://github.com/jlowin) in [#1106](https://github.com/jlowin/fastmcp/pull/1106) +* Fix OpenAPI parameter name collisions with location suffixing by [@jlowin](https://github.com/jlowin) in [#1107](https://github.com/jlowin/fastmcp/pull/1107) +* Add mirrored component support for proxy servers by [@jlowin](https://github.com/jlowin) in [#1105](https://github.com/jlowin/fastmcp/pull/1105) +### Fixes ๐Ÿž +* Fix OpenAPI deepObject style parameter encoding by [@jlowin](https://github.com/jlowin) in [#1122](https://github.com/jlowin/fastmcp/pull/1122) +* xfail when github token is not set ('' or None) by [@jlowin](https://github.com/jlowin) in [#1123](https://github.com/jlowin/fastmcp/pull/1123) +* fix: replace oneOf with anyOf in OpenAPI output schemas by [@MagnusS0](https://github.com/MagnusS0) in [#1119](https://github.com/jlowin/fastmcp/pull/1119) +* Fix middleware list result types by [@jlowin](https://github.com/jlowin) in [#1125](https://github.com/jlowin/fastmcp/pull/1125) +* Improve console width for logo by [@jlowin](https://github.com/jlowin) in [#1126](https://github.com/jlowin/fastmcp/pull/1126) +### Docs ๐Ÿ“š +* Improve transport + integration docs by [@jlowin](https://github.com/jlowin) in [#1103](https://github.com/jlowin/fastmcp/pull/1103) +* Update proxy.mdx by [@coldfire-x](https://github.com/coldfire-x) in [#1108](https://github.com/jlowin/fastmcp/pull/1108) +### Other Changes ๐Ÿฆพ +* Update github remote server tests with secret by [@jlowin](https://github.com/jlowin) in [#1112](https://github.com/jlowin/fastmcp/pull/1112) + +## New Contributors +* [@coldfire-x](https://github.com/coldfire-x) made their first contribution in [#1108](https://github.com/jlowin/fastmcp/pull/1108) +* [@MagnusS0](https://github.com/MagnusS0) made their first contribution in [#1119](https://github.com/jlowin/fastmcp/pull/1119) + +**Full Changelog**: [v2.10.4...v2.10.5](https://github.com/jlowin/fastmcp/compare/v2.10.4...v2.10.5) + + + + + +## [v2.10.4: Transport-ation](https://github.com/jlowin/fastmcp/releases/tag/v2.10.4) + +A quick fix to ensure the CLI accepts "streamable-http" as a valid transport option. + +## What's Changed +### Fixes ๐Ÿž +* Ensure the CLI accepts "streamable-http" as a valid transport by [@jlowin](https://github.com/jlowin) in [#1099](https://github.com/jlowin/fastmcp/pull/1099) + +**Full Changelog**: [v2.10.3...v2.10.4](https://github.com/jlowin/fastmcp/compare/v2.10.3...v2.10.4) + + + + + +## [v2.10.3: CLI Me a River](https://github.com/jlowin/fastmcp/releases/tag/v2.10.3) + +A major CLI overhaul featuring a complete refactor from typer to cyclopts, new IDE integrations, and comprehensive OpenAPI improvements. + +## What's Changed +### New Features ๐ŸŽ‰ +* Refactor CLI from typer to cyclopts and add comprehensive tests by [@jlowin](https://github.com/jlowin) in [#1062](https://github.com/jlowin/fastmcp/pull/1062) +* Add output schema support for OpenAPI tools by [@jlowin](https://github.com/jlowin) in [#1073](https://github.com/jlowin/fastmcp/pull/1073) +### Enhancements ๐Ÿ”ง +* Add Cursor support via CLI integration by [@jlowin](https://github.com/jlowin) in [#1052](https://github.com/jlowin/fastmcp/pull/1052) +* Add Claude Code install integration by [@jlowin](https://github.com/jlowin) in [#1053](https://github.com/jlowin/fastmcp/pull/1053) +* Generate MCP JSON config output from CLI as new `fastmcp install` command by [@jlowin](https://github.com/jlowin) in [#1056](https://github.com/jlowin/fastmcp/pull/1056) +* Use isawaitable instead of iscoroutine by [@jlowin](https://github.com/jlowin) in [#1059](https://github.com/jlowin/fastmcp/pull/1059) +* feat: Add `--path` Option to CLI for HTTP/SSE Route by [@davidbk-legit](https://github.com/davidbk-legit) in [#1087](https://github.com/jlowin/fastmcp/pull/1087) +* Fix concurrent proxy client operations with session isolation by [@jlowin](https://github.com/jlowin) in [#1083](https://github.com/jlowin/fastmcp/pull/1083) +### Fixes ๐Ÿž +* Refactor Client context management to avoid concurrency issue by [@hopeful0](https://github.com/hopeful0) in [#1054](https://github.com/jlowin/fastmcp/pull/1054) +* Keep json schema $defs on transform by [@strawgate](https://github.com/strawgate) in [#1066](https://github.com/jlowin/fastmcp/pull/1066) +* Ensure fastmcp version copy is plaintext by [@jlowin](https://github.com/jlowin) in [#1071](https://github.com/jlowin/fastmcp/pull/1071) +* Fix single-element list unwrapping in tool content by [@jlowin](https://github.com/jlowin) in [#1074](https://github.com/jlowin/fastmcp/pull/1074) +* Fix max recursion error when pruning OpenAPI definitions by [@dimitribarbot](https://github.com/dimitribarbot) in [#1092](https://github.com/jlowin/fastmcp/pull/1092) +* Fix OpenAPI tool name registration when modified by mcp_component_fn by [@jlowin](https://github.com/jlowin) in [#1096](https://github.com/jlowin/fastmcp/pull/1096) +### Docs ๐Ÿ“š +* Docs: add example of more concise way to use bearer auth by [@neilconway](https://github.com/neilconway) in [#1055](https://github.com/jlowin/fastmcp/pull/1055) +* Update favicon by [@jlowin](https://github.com/jlowin) in [#1058](https://github.com/jlowin/fastmcp/pull/1058) +* Update environment note by [@jlowin](https://github.com/jlowin) in [#1075](https://github.com/jlowin/fastmcp/pull/1075) +* Add fastmcp version --copy documentation by [@jlowin](https://github.com/jlowin) in [#1076](https://github.com/jlowin/fastmcp/pull/1076) +### Other Changes ๐Ÿฆพ +* Remove asserts and add documentation following #1054 by [@jlowin](https://github.com/jlowin) in [#1057](https://github.com/jlowin/fastmcp/pull/1057) +* Add --copy flag for fastmcp version by [@jlowin](https://github.com/jlowin) in [#1063](https://github.com/jlowin/fastmcp/pull/1063) +* Fix docstring format for fastmcp.client.Client by [@neilconway](https://github.com/neilconway) in [#1094](https://github.com/jlowin/fastmcp/pull/1094) + +## New Contributors +* [@neilconway](https://github.com/neilconway) made their first contribution in [#1055](https://github.com/jlowin/fastmcp/pull/1055) +* [@davidbk-legit](https://github.com/davidbk-legit) made their first contribution in [#1087](https://github.com/jlowin/fastmcp/pull/1087) +* [@dimitribarbot](https://github.com/dimitribarbot) made their first contribution in [#1092](https://github.com/jlowin/fastmcp/pull/1092) + +**Full Changelog**: [v2.10.2...v2.10.3](https://github.com/jlowin/fastmcp/compare/v2.10.2...v2.10.3) + + + + + +## [v2.10.2: Forward March](https://github.com/jlowin/fastmcp/releases/tag/v2.10.2) + +The headline feature of this release is the ability to "forward" advanced MCP interactions like logging, progress, and elicitation through proxy servers. If the remote server requests an elicitation, the proxy client will pass that request to the new, "ultimate" client. + +## What's Changed +### New Features ๐ŸŽ‰ +* Proxy support advanced MCP features by [@hopeful0](https://github.com/hopeful0) in [#1022](https://github.com/jlowin/fastmcp/pull/1022) +### Enhancements ๐Ÿ”ง +* Re-add splash screen by [@jlowin](https://github.com/jlowin) in [#1027](https://github.com/jlowin/fastmcp/pull/1027) +* Reduce banner padding by [@jlowin](https://github.com/jlowin) in [#1030](https://github.com/jlowin/fastmcp/pull/1030) +* Allow per-server timeouts in MCPConfig by [@cegersdoerfer](https://github.com/cegersdoerfer) in [#1031](https://github.com/jlowin/fastmcp/pull/1031) +* Support 'scp' claim for OAuth scopes in BearerAuthProvider by [@jlowin](https://github.com/jlowin) in [#1033](https://github.com/jlowin/fastmcp/pull/1033) +* Add path expansion to image/audio/file by [@jlowin](https://github.com/jlowin) in [#1038](https://github.com/jlowin/fastmcp/pull/1038) +* Ensure multi-client configurations use new ProxyClient by [@jlowin](https://github.com/jlowin) in [#1045](https://github.com/jlowin/fastmcp/pull/1045) +### Fixes ๐Ÿž +* Expose stateless_http kwarg for mcp.run() by [@jlowin](https://github.com/jlowin) in [#1018](https://github.com/jlowin/fastmcp/pull/1018) +* Avoid propagating logs by [@jlowin](https://github.com/jlowin) in [#1042](https://github.com/jlowin/fastmcp/pull/1042) +### Docs ๐Ÿ“š +* Clean up docs by [@jlowin](https://github.com/jlowin) in [#1028](https://github.com/jlowin/fastmcp/pull/1028) +* Docs: clarify server URL paths for ChatGPT integration by [@thap2331](https://github.com/thap2331) in [#1017](https://github.com/jlowin/fastmcp/pull/1017) +### Other Changes ๐Ÿฆพ +* Split giant openapi test file into smaller files by [@jlowin](https://github.com/jlowin) in [#1034](https://github.com/jlowin/fastmcp/pull/1034) +* Add comprehensive OpenAPI 3.0 vs 3.1 compatibility tests by [@jlowin](https://github.com/jlowin) in [#1035](https://github.com/jlowin/fastmcp/pull/1035) +* Update banner and use console.log by [@jlowin](https://github.com/jlowin) in [#1041](https://github.com/jlowin/fastmcp/pull/1041) + +## New Contributors +* [@cegersdoerfer](https://github.com/cegersdoerfer) made their first contribution in [#1031](https://github.com/jlowin/fastmcp/pull/1031) +* [@hopeful0](https://github.com/hopeful0) made their first contribution in [#1022](https://github.com/jlowin/fastmcp/pull/1022) +* [@thap2331](https://github.com/thap2331) made their first contribution in [#1017](https://github.com/jlowin/fastmcp/pull/1017) + +**Full Changelog**: [v2.10.1...v2.10.2](https://github.com/jlowin/fastmcp/compare/v2.10.1...v2.10.2) + + + + + +## [v2.10.1: Revert to Sender](https://github.com/jlowin/fastmcp/releases/tag/v2.10.1) + +A quick patch to revert the CLI banner that was added in v2.10.0. + +## What's Changed +### Docs ๐Ÿ“š +* Update changelog.mdx by [@jlowin](https://github.com/jlowin) in [#1009](https://github.com/jlowin/fastmcp/pull/1009) +* Revert "Add CLI banner" by [@jlowin](https://github.com/jlowin) in [#1011](https://github.com/jlowin/fastmcp/pull/1011) + +**Full Changelog**: [v2.10.0...v2.10.1](https://github.com/jlowin/fastmcp/compare/v2.10.0...v2.10.1) + + + + + +## [v2.10.0: Great Spec-tations](https://github.com/jlowin/fastmcp/releases/tag/v2.10.0) + +FastMCP 2.10 brings full compliance with the 6/18/2025 MCP spec update, introducing elicitation support for dynamic server-client communication and output schemas for structured tool responses. Please note that due to these changes, this release also includes a breaking change to the return signature of `client.call_tool()`. + +### Elicitation Support +Elicitation allows MCP servers to request additional information from clients during tool execution, enabling more interactive and dynamic server behavior. This opens up new possibilities for tools that need user input or confirmation during execution. + +### Output Schemas +Tools can now define structured output schemas, ensuring that responses conform to expected formats and making tool integration more predictable and type-safe. + +## What's Changed +### New Features ๐ŸŽ‰ +* MCP 6/18/25: Add output schema to tools by [@jlowin](https://github.com/jlowin) in [#901](https://github.com/jlowin/fastmcp/pull/901) +* MCP 6/18/25: Elicitation support by [@jlowin](https://github.com/jlowin) in [#889](https://github.com/jlowin/fastmcp/pull/889) +### Enhancements ๐Ÿ”ง +* Update types + tests for SDK changes by [@jlowin](https://github.com/jlowin) in [#888](https://github.com/jlowin/fastmcp/pull/888) +* MCP 6/18/25: Update auth primitives by [@jlowin](https://github.com/jlowin) in [#966](https://github.com/jlowin/fastmcp/pull/966) +* Add OpenAPI extensions support to HTTPRoute by [@maddymanu](https://github.com/maddymanu) in [#977](https://github.com/jlowin/fastmcp/pull/977) +* Add title field support to FastMCP components by [@jlowin](https://github.com/jlowin) in [#982](https://github.com/jlowin/fastmcp/pull/982) +* Support implicit Elicitation acceptance by [@jlowin](https://github.com/jlowin) in [#983](https://github.com/jlowin/fastmcp/pull/983) +* Support 'no response' elicitation requests by [@jlowin](https://github.com/jlowin) in [#992](https://github.com/jlowin/fastmcp/pull/992) +* Add Support for Configurable Algorithms by [@sstene1](https://github.com/sstene1) in [#997](https://github.com/jlowin/fastmcp/pull/997) +### Fixes ๐Ÿž +* Improve stdio error handling to raise connection failures immediately by [@jlowin](https://github.com/jlowin) in [#984](https://github.com/jlowin/fastmcp/pull/984) +* Fix type hints for FunctionResource:fn by [@CfirTsabari](https://github.com/CfirTsabari) in [#986](https://github.com/jlowin/fastmcp/pull/986) +* Update link to OpenAI MCP example by [@mossbanay](https://github.com/mossbanay) in [#985](https://github.com/jlowin/fastmcp/pull/985) +* Fix output schema generation edge case by [@jlowin](https://github.com/jlowin) in [#995](https://github.com/jlowin/fastmcp/pull/995) +* Refactor array parameter formatting to reduce code duplication by [@jlowin](https://github.com/jlowin) in [#1007](https://github.com/jlowin/fastmcp/pull/1007) +* Fix OpenAPI array parameter explode handling by [@jlowin](https://github.com/jlowin) in [#1008](https://github.com/jlowin/fastmcp/pull/1008) +### Breaking Changes ๐Ÿ›ซ +* MCP 6/18/25: Upgrade to mcp 1.10 by [@jlowin](https://github.com/jlowin) in [#887](https://github.com/jlowin/fastmcp/pull/887) +### Docs ๐Ÿ“š +* Update middleware imports and documentation by [@jlowin](https://github.com/jlowin) in [#999](https://github.com/jlowin/fastmcp/pull/999) +* Update OpenAI docs by [@jlowin](https://github.com/jlowin) in [#1001](https://github.com/jlowin/fastmcp/pull/1001) +* Add CLI banner by [@jlowin](https://github.com/jlowin) in [#1005](https://github.com/jlowin/fastmcp/pull/1005) +### Examples & Contrib ๐Ÿ’ก +* Component Manager by [@gorocode](https://github.com/gorocode) in [#976](https://github.com/jlowin/fastmcp/pull/976) +### Other Changes ๐Ÿฆพ +* Minor auth improvements by [@jlowin](https://github.com/jlowin) in [#967](https://github.com/jlowin/fastmcp/pull/967) +* Add .ccignore for copychat by [@jlowin](https://github.com/jlowin) in [#1000](https://github.com/jlowin/fastmcp/pull/1000) + +## New Contributors +* [@maddymanu](https://github.com/maddymanu) made their first contribution in [#977](https://github.com/jlowin/fastmcp/pull/977) +* [@github0hello](https://github.com/github0hello) made their first contribution in [#979](https://github.com/jlowin/fastmcp/pull/979) +* [@tommitt](https://github.com/tommitt) made their first contribution in [#975](https://github.com/jlowin/fastmcp/pull/975) +* [@CfirTsabari](https://github.com/CfirTsabari) made their first contribution in [#986](https://github.com/jlowin/fastmcp/pull/986) +* [@mossbanay](https://github.com/mossbanay) made their first contribution in [#985](https://github.com/jlowin/fastmcp/pull/985) +* [@sstene1](https://github.com/sstene1) made their first contribution in [#997](https://github.com/jlowin/fastmcp/pull/997) + +**Full Changelog**: [v2.9.2...v2.10.0](https://github.com/jlowin/fastmcp/compare/v2.9.2...v2.10.0) + + + + + +## [v2.9.2: Safety Pin](https://github.com/jlowin/fastmcp/releases/tag/v2.9.2) + +This is a patch release to pin `mcp` below 1.10, which includes changes related to the 6/18/2025 MCP spec update and could potentially break functionality for some FastMCP users. + +## What's Changed +### Docs ๐Ÿ“š +* Fix version badge for messages by [@jlowin](https://github.com/jlowin) in [#960](https://github.com/jlowin/fastmcp/pull/960) +### Dependencies ๐Ÿ“ฆ +* Pin mcp dependency by [@jlowin](https://github.com/jlowin) in [#962](https://github.com/jlowin/fastmcp/pull/962) + +**Full Changelog**: [v2.9.1...v2.9.2](https://github.com/jlowin/fastmcp/compare/v2.9.1...v2.9.2) + + + + + +## [v2.9.1: Call Me Maybe](https://github.com/jlowin/fastmcp/releases/tag/v2.9.1) + +FastMCP 2.9.1 introduces automatic MCP list change notifications, allowing servers to notify clients when tools, resources, or prompts are dynamically updated. This enables more responsive and adaptive MCP integrations. + +## What's Changed +### New Features ๐ŸŽ‰ +* Add automatic MCP list change notifications and client message handling by [@jlowin](https://github.com/jlowin) in [#939](https://github.com/jlowin/fastmcp/pull/939) +### Enhancements ๐Ÿ”ง +* Add debug logging to bearer token authentication by [@jlowin](https://github.com/jlowin) in [#952](https://github.com/jlowin/fastmcp/pull/952) +### Fixes ๐Ÿž +* Fix duplicate error logging in exception handlers by [@jlowin](https://github.com/jlowin) in [#938](https://github.com/jlowin/fastmcp/pull/938) +* Fix parameter location enum handling in OpenAPI parser by [@jlowin](https://github.com/jlowin) in [#953](https://github.com/jlowin/fastmcp/pull/953) +* Fix external schema reference handling in OpenAPI parser by [@jlowin](https://github.com/jlowin) in [#954](https://github.com/jlowin/fastmcp/pull/954) +### Docs ๐Ÿ“š +* Update changelog for 2.9 release by [@jlowin](https://github.com/jlowin) in [#929](https://github.com/jlowin/fastmcp/pull/929) +* Regenerate API references by [@zzstoatzz](https://github.com/zzstoatzz) in [#935](https://github.com/jlowin/fastmcp/pull/935) +* Regenerate API references by [@zzstoatzz](https://github.com/zzstoatzz) in [#947](https://github.com/jlowin/fastmcp/pull/947) +* Regenerate API references by [@zzstoatzz](https://github.com/zzstoatzz) in [#949](https://github.com/jlowin/fastmcp/pull/949) +### Examples & Contrib ๐Ÿ’ก +* Add `create_thread` tool to bsky MCP server by [@zzstoatzz](https://github.com/zzstoatzz) in [#927](https://github.com/jlowin/fastmcp/pull/927) +* Update `mount_example.py` to work with current fastmcp API by [@rajephon](https://github.com/rajephon) in [#957](https://github.com/jlowin/fastmcp/pull/957) + +## New Contributors +* [@rajephon](https://github.com/rajephon) made their first contribution in [#957](https://github.com/jlowin/fastmcp/pull/957) + +**Full Changelog**: [v2.9.0...v2.9.1](https://github.com/jlowin/fastmcp/compare/v2.9.0...v2.9.1) + + + + + +## [v2.9.0: Stuck in the Middleware With You](https://github.com/jlowin/fastmcp/releases/tag/v2.9.0) + +FastMCP 2.9 introduces two important features that push beyond the basic MCP protocol: MCP Middleware and server-side type conversion. + +### MCP Middleware +MCP middleware lets you intercept and modify requests and responses at the protocol level, giving you powerful capabilities for logging, authentication, validation, and more. This is particularly useful for building production-ready MCP servers that need sophisticated request handling. + +### Server-side Type Conversion +This release also introduces server-side type conversion for prompt arguments, ensuring that data is properly formatted before being passed to your functions. This reduces the burden on individual tools and prompts to handle type validation and conversion. + +## What's Changed +### New Features ๐ŸŽ‰ +* Add File utility for binary data by [@gorocode](https://github.com/gorocode) in [#843](https://github.com/jlowin/fastmcp/pull/843) +* Consolidate prefix logic into FastMCP methods by [@jlowin](https://github.com/jlowin) in [#861](https://github.com/jlowin/fastmcp/pull/861) +* Add MCP Middleware by [@jlowin](https://github.com/jlowin) in [#870](https://github.com/jlowin/fastmcp/pull/870) +* Implement server-side type conversion for prompt arguments by [@jlowin](https://github.com/jlowin) in [#908](https://github.com/jlowin/fastmcp/pull/908) +### Enhancements ๐Ÿ”ง +* Fix tool description indentation issue by [@zfflxx](https://github.com/zfflxx) in [#845](https://github.com/jlowin/fastmcp/pull/845) +* Add version parameter to FastMCP constructor by [@mkyutani](https://github.com/mkyutani) in [#842](https://github.com/jlowin/fastmcp/pull/842) +* Update version to not be positional by [@jlowin](https://github.com/jlowin) in [#848](https://github.com/jlowin/fastmcp/pull/848) +* Add key to component by [@jlowin](https://github.com/jlowin) in [#869](https://github.com/jlowin/fastmcp/pull/869) +* Add session_id property to Context for data sharing by [@jlowin](https://github.com/jlowin) in [#881](https://github.com/jlowin/fastmcp/pull/881) +* Fix CORS documentation example by [@jlowin](https://github.com/jlowin) in [#895](https://github.com/jlowin/fastmcp/pull/895) +### Fixes ๐Ÿž +* "report_progress missing passing related_request_id causes notifications not working" by [@alexsee](https://github.com/alexsee) in [#838](https://github.com/jlowin/fastmcp/pull/838) +* Fix JWT issuer validation to support string values per RFC 7519 by [@jlowin](https://github.com/jlowin) in [#892](https://github.com/jlowin/fastmcp/pull/892) +* Fix BearerAuthProvider audience type annotations by [@jlowin](https://github.com/jlowin) in [#894](https://github.com/jlowin/fastmcp/pull/894) +### Docs ๐Ÿ“š +* Add CLAUDE.md development guidelines by [@jlowin](https://github.com/jlowin) in [#880](https://github.com/jlowin/fastmcp/pull/880) +* Update context docs for session_id property by [@jlowin](https://github.com/jlowin) in [#882](https://github.com/jlowin/fastmcp/pull/882) +* Add API reference by [@zzstoatzz](https://github.com/zzstoatzz) in [#893](https://github.com/jlowin/fastmcp/pull/893) +* Fix API ref rendering by [@zzstoatzz](https://github.com/zzstoatzz) in [#900](https://github.com/jlowin/fastmcp/pull/900) +* Simplify docs nav by [@jlowin](https://github.com/jlowin) in [#902](https://github.com/jlowin/fastmcp/pull/902) +* Add fastmcp inspect command by [@jlowin](https://github.com/jlowin) in [#904](https://github.com/jlowin/fastmcp/pull/904) +* Update client docs by [@jlowin](https://github.com/jlowin) in [#912](https://github.com/jlowin/fastmcp/pull/912) +* Update docs nav by [@jlowin](https://github.com/jlowin) in [#913](https://github.com/jlowin/fastmcp/pull/913) +* Update integration documentation for Claude Desktop, ChatGPT, and Claude Code by [@jlowin](https://github.com/jlowin) in [#915](https://github.com/jlowin/fastmcp/pull/915) +* Add http as an alias for streamable http by [@jlowin](https://github.com/jlowin) in [#917](https://github.com/jlowin/fastmcp/pull/917) +* Clean up parameter documentation by [@jlowin](https://github.com/jlowin) in [#918](https://github.com/jlowin/fastmcp/pull/918) +* Add middleware examples for timing, logging, rate limiting, and error handling by [@jlowin](https://github.com/jlowin) in [#919](https://github.com/jlowin/fastmcp/pull/919) +* ControlFlow โ†’ FastMCP rename by [@jlowin](https://github.com/jlowin) in [#922](https://github.com/jlowin/fastmcp/pull/922) +### Examples & Contrib ๐Ÿ’ก +* Add contrib.mcp_mixin support for annotations by [@rsp2k](https://github.com/rsp2k) in [#860](https://github.com/jlowin/fastmcp/pull/860) +* Add ATProto (Bluesky) MCP Server Example by [@zzstoatzz](https://github.com/zzstoatzz) in [#916](https://github.com/jlowin/fastmcp/pull/916) +* Fix path in atproto example pyproject by [@zzstoatzz](https://github.com/zzstoatzz) in [#920](https://github.com/jlowin/fastmcp/pull/920) +* Remove uv source in example by [@zzstoatzz](https://github.com/zzstoatzz) in [#921](https://github.com/jlowin/fastmcp/pull/921) + +## New Contributors +* [@alexsee](https://github.com/alexsee) made their first contribution in [#838](https://github.com/jlowin/fastmcp/pull/838) +* [@zfflxx](https://github.com/zfflxx) made their first contribution in [#845](https://github.com/jlowin/fastmcp/pull/845) +* [@mkyutani](https://github.com/mkyutani) made their first contribution in [#842](https://github.com/jlowin/fastmcp/pull/842) +* [@gorocode](https://github.com/gorocode) made their first contribution in [#843](https://github.com/jlowin/fastmcp/pull/843) +* [@rsp2k](https://github.com/rsp2k) made their first contribution in [#860](https://github.com/jlowin/fastmcp/pull/860) +* [@owtaylor](https://github.com/owtaylor) made their first contribution in [#897](https://github.com/jlowin/fastmcp/pull/897) +* [@Jason-CKY](https://github.com/Jason-CKY) made their first contribution in [#906](https://github.com/jlowin/fastmcp/pull/906) + +**Full Changelog**: [v2.8.1...v2.9.0](https://github.com/jlowin/fastmcp/compare/v2.8.1...v2.9.0) + + + + + +## [v2.8.1: Sound Judgement](https://github.com/jlowin/fastmcp/releases/tag/v2.8.1) + +2.8.1 introduces audio support, as well as minor fixes and updates for deprecated features. + +### Audio Support +This release adds support for audio content in MCP tools and resources, expanding FastMCP's multimedia capabilities beyond text and images. + +## What's Changed +### New Features ๐ŸŽ‰ +* Add audio support by [@jlowin](https://github.com/jlowin) in [#833](https://github.com/jlowin/fastmcp/pull/833) +### Enhancements ๐Ÿ”ง +* Add flag for disabling deprecation warnings by [@jlowin](https://github.com/jlowin) in [#802](https://github.com/jlowin/fastmcp/pull/802) +* Add examples to Tool Arg Param transformation by [@strawgate](https://github.com/strawgate) in [#806](https://github.com/jlowin/fastmcp/pull/806) +### Fixes ๐Ÿž +* Restore .settings access as deprecated by [@jlowin](https://github.com/jlowin) in [#800](https://github.com/jlowin/fastmcp/pull/800) +* Ensure handling of false http kwargs correctly; removed unused kwarg by [@jlowin](https://github.com/jlowin) in [#804](https://github.com/jlowin/fastmcp/pull/804) +* Bump mcp 1.9.4 by [@jlowin](https://github.com/jlowin) in [#835](https://github.com/jlowin/fastmcp/pull/835) +### Docs ๐Ÿ“š +* Update changelog for 2.8.0 by [@jlowin](https://github.com/jlowin) in [#794](https://github.com/jlowin/fastmcp/pull/794) +* Update welcome docs by [@jlowin](https://github.com/jlowin) in [#808](https://github.com/jlowin/fastmcp/pull/808) +* Update headers in docs by [@jlowin](https://github.com/jlowin) in [#809](https://github.com/jlowin/fastmcp/pull/809) +* Add MCP group to tutorials by [@jlowin](https://github.com/jlowin) in [#810](https://github.com/jlowin/fastmcp/pull/810) +* Add Community section to documentation by [@zzstoatzz](https://github.com/zzstoatzz) in [#819](https://github.com/jlowin/fastmcp/pull/819) +* Add 2.8 update by [@jlowin](https://github.com/jlowin) in [#821](https://github.com/jlowin/fastmcp/pull/821) +* Embed YouTube videos in community showcase by [@zzstoatzz](https://github.com/zzstoatzz) in [#820](https://github.com/jlowin/fastmcp/pull/820) +### Other Changes ๐Ÿฆพ +* Ensure http args are passed through by [@jlowin](https://github.com/jlowin) in [#803](https://github.com/jlowin/fastmcp/pull/803) +* Fix install link in readme by [@jlowin](https://github.com/jlowin) in [#836](https://github.com/jlowin/fastmcp/pull/836) + +**Full Changelog**: [v2.8.0...v2.8.1](https://github.com/jlowin/fastmcp/compare/v2.8.0...v2.8.1) + + + + + +## [v2.8.0: Transform and Roll Out](https://github.com/jlowin/fastmcp/releases/tag/v2.8.0) + +FastMCP 2.8.0 introduces powerful new ways to customize and control your MCP servers! + +### Tool Transformation + +The highlight of this release is first-class [**Tool Transformation**](/patterns/tool-transformation), a new feature that lets you create enhanced variations of existing tools. You can now easily rename arguments, hide parameters, modify descriptions, and even wrap tools with custom validation or post-processing logicโ€”all without rewriting the original code. This makes it easier than ever to adapt generic tools for specific LLM use cases or to simplify complex APIs. Huge thanks to [@strawgate](https://github.com/strawgate) for partnering on this, starting with [#591](https://github.com/jlowin/fastmcp/discussions/591) and [#599](https://github.com/jlowin/fastmcp/pull/599) and continuing offline. + +### Component Control +This release also gives you more granular control over which components are exposed to clients. With new [**tag-based filtering**](/servers/server#tag-based-filtering), you can selectively enable or disable tools, resources, and prompts based on tags, perfect for managing different environments or user permissions. Complementing this, every component now supports being [programmatically enabled or disabled](/servers/tools#disabling-tools), offering dynamic control over your server's capabilities. + +### Tools-by-Default +Finally, to improve compatibility with a wider range of LLM clients, this release changes the default behavior for OpenAPI integration: all API endpoints are now converted to `Tools` by default. This is a **breaking change** but pragmatically necessitated by the fact that the majority of MCP clients available today are, sadly, only compatible with MCP tools. Therefore, this change significantly simplifies the out-of-the-box experience and ensures your entire API is immediately accessible to any tool-using agent. + +## What's Changed +### New Features ๐ŸŽ‰ +* First-class tool transformation by [@jlowin](https://github.com/jlowin) in [#745](https://github.com/jlowin/fastmcp/pull/745) +* Support enable/disable for all FastMCP components (tools, prompts, resources, templates) by [@jlowin](https://github.com/jlowin) in [#781](https://github.com/jlowin/fastmcp/pull/781) +* Add support for tag-based component filtering by [@jlowin](https://github.com/jlowin) in [#748](https://github.com/jlowin/fastmcp/pull/748) +* Allow tag assignments for OpenAPI by [@jlowin](https://github.com/jlowin) in [#791](https://github.com/jlowin/fastmcp/pull/791) +### Enhancements ๐Ÿ”ง +* Create common base class for components by [@jlowin](https://github.com/jlowin) in [#776](https://github.com/jlowin/fastmcp/pull/776) +* Move components to own file; add resource by [@jlowin](https://github.com/jlowin) in [#777](https://github.com/jlowin/fastmcp/pull/777) +* Update FastMCP component with __eq__ and __repr__ by [@jlowin](https://github.com/jlowin) in [#779](https://github.com/jlowin/fastmcp/pull/779) +* Remove open-ended and server-specific settings by [@jlowin](https://github.com/jlowin) in [#750](https://github.com/jlowin/fastmcp/pull/750) +### Fixes ๐Ÿž +* Ensure client is only initialized once by [@jlowin](https://github.com/jlowin) in [#758](https://github.com/jlowin/fastmcp/pull/758) +* Fix field validator for resource by [@jlowin](https://github.com/jlowin) in [#778](https://github.com/jlowin/fastmcp/pull/778) +* Ensure proxies can overwrite remote tools without falling back to the remote by [@jlowin](https://github.com/jlowin) in [#782](https://github.com/jlowin/fastmcp/pull/782) +### Breaking Changes ๐Ÿ›ซ +* Treat all openapi routes as tools by [@jlowin](https://github.com/jlowin) in [#788](https://github.com/jlowin/fastmcp/pull/788) +* Fix issue with global OpenAPI tags by [@jlowin](https://github.com/jlowin) in [#792](https://github.com/jlowin/fastmcp/pull/792) +### Docs ๐Ÿ“š +* Minor docs updates by [@jlowin](https://github.com/jlowin) in [#755](https://github.com/jlowin/fastmcp/pull/755) +* Add 2.7 update by [@jlowin](https://github.com/jlowin) in [#756](https://github.com/jlowin/fastmcp/pull/756) +* Reduce 2.7 image size by [@jlowin](https://github.com/jlowin) in [#757](https://github.com/jlowin/fastmcp/pull/757) +* Update updates.mdx by [@jlowin](https://github.com/jlowin) in [#765](https://github.com/jlowin/fastmcp/pull/765) +* Hide docs sidebar scrollbar by default by [@jlowin](https://github.com/jlowin) in [#766](https://github.com/jlowin/fastmcp/pull/766) +* Add "stop vibe testing" to tutorials by [@jlowin](https://github.com/jlowin) in [#767](https://github.com/jlowin/fastmcp/pull/767) +* Add docs links by [@jlowin](https://github.com/jlowin) in [#768](https://github.com/jlowin/fastmcp/pull/768) +* Fix: updated variable name under Gemini remote client by [@yrangana](https://github.com/yrangana) in [#769](https://github.com/jlowin/fastmcp/pull/769) +* Revert "Hide docs sidebar scrollbar by default" by [@jlowin](https://github.com/jlowin) in [#770](https://github.com/jlowin/fastmcp/pull/770) +* Add updates by [@jlowin](https://github.com/jlowin) in [#773](https://github.com/jlowin/fastmcp/pull/773) +* Add tutorials by [@jlowin](https://github.com/jlowin) in [#783](https://github.com/jlowin/fastmcp/pull/783) +* Update LLM-friendly docs by [@jlowin](https://github.com/jlowin) in [#784](https://github.com/jlowin/fastmcp/pull/784) +* Update oauth.mdx by [@JeremyCraigMartinez](https://github.com/JeremyCraigMartinez) in [#787](https://github.com/jlowin/fastmcp/pull/787) +* Add changelog by [@jlowin](https://github.com/jlowin) in [#789](https://github.com/jlowin/fastmcp/pull/789) +* Add tutorials by [@jlowin](https://github.com/jlowin) in [#790](https://github.com/jlowin/fastmcp/pull/790) +* Add docs for tag-based filtering by [@jlowin](https://github.com/jlowin) in [#793](https://github.com/jlowin/fastmcp/pull/793) +### Other Changes ๐Ÿฆพ +* Create dependabot.yml by [@jlowin](https://github.com/jlowin) in [#759](https://github.com/jlowin/fastmcp/pull/759) +* Bump astral-sh/setup-uv from 3 to 6 by [@dependabot](https://github.com/dependabot) in [#760](https://github.com/jlowin/fastmcp/pull/760) +* Add dependencies section to release by [@jlowin](https://github.com/jlowin) in [#761](https://github.com/jlowin/fastmcp/pull/761) +* Remove extra imports for MCPConfig by [@Maanas-Verma](https://github.com/Maanas-Verma) in [#763](https://github.com/jlowin/fastmcp/pull/763) +* Split out enhancements in release notes by [@jlowin](https://github.com/jlowin) in [#764](https://github.com/jlowin/fastmcp/pull/764) + +## New Contributors +* [@dependabot](https://github.com/dependabot) made their first contribution in [#760](https://github.com/jlowin/fastmcp/pull/760) +* [@Maanas-Verma](https://github.com/Maanas-Verma) made their first contribution in [#763](https://github.com/jlowin/fastmcp/pull/763) +* [@JeremyCraigMartinez](https://github.com/JeremyCraigMartinez) made their first contribution in [#787](https://github.com/jlowin/fastmcp/pull/787) + +**Full Changelog**: [v2.7.1...v2.8.0](https://github.com/jlowin/fastmcp/compare/v2.7.1...v2.8.0) + + + + + +## [v2.7.1: The Bearer Necessities](https://github.com/jlowin/fastmcp/releases/tag/v2.7.1) + +This release primarily contains a fix for parsing string tokens that are provided to FastMCP clients. + +### New Features ๐ŸŽ‰ + +* Respect cache setting, set default to 1 second by [@jlowin](https://github.com/jlowin) in [#747](https://github.com/jlowin/fastmcp/pull/747) + +### Fixes ๐Ÿž + +* Ensure event store is properly typed by [@jlowin](https://github.com/jlowin) in [#753](https://github.com/jlowin/fastmcp/pull/753) +* Fix passing token string to client auth & add auth to MCPConfig clients by [@jlowin](https://github.com/jlowin) in [#754](https://github.com/jlowin/fastmcp/pull/754) + +### Docs ๐Ÿ“š + +* Docs : fix client to mcp\_client in Gemini example by [@yrangana](https://github.com/yrangana) in [#734](https://github.com/jlowin/fastmcp/pull/734) +* update add tool docstring by [@strawgate](https://github.com/strawgate) in [#739](https://github.com/jlowin/fastmcp/pull/739) +* Fix contrib link by [@richardkmichael](https://github.com/richardkmichael) in [#749](https://github.com/jlowin/fastmcp/pull/749) + +### Other Changes ๐Ÿฆพ + +* Switch Pydantic defaults to kwargs by [@strawgate](https://github.com/strawgate) in [#731](https://github.com/jlowin/fastmcp/pull/731) +* Fix Typo in CLI module by [@wfclark5](https://github.com/wfclark5) in [#737](https://github.com/jlowin/fastmcp/pull/737) +* chore: fix prompt docstring by [@danb27](https://github.com/danb27) in [#752](https://github.com/jlowin/fastmcp/pull/752) +* Add accept to excluded headers by [@jlowin](https://github.com/jlowin) in [#751](https://github.com/jlowin/fastmcp/pull/751) + +### New Contributors + +* [@wfclark5](https://github.com/wfclark5) made their first contribution in [#737](https://github.com/jlowin/fastmcp/pull/737) +* [@richardkmichael](https://github.com/richardkmichael) made their first contribution in [#749](https://github.com/jlowin/fastmcp/pull/749) +* [@danb27](https://github.com/danb27) made their first contribution in [#752](https://github.com/jlowin/fastmcp/pull/752) + +**Full Changelog**: [v2.7.0...v2.7.1](https://github.com/jlowin/fastmcp/compare/v2.7.0...v2.7.1) + + + + +## [v2.7.0: Pare Programming](https://github.com/jlowin/fastmcp/releases/tag/v2.7.0) + +This is primarily a housekeeping release to remove or deprecate cruft that's accumulated since v1. Primarily, this release refactors FastMCP's internals in preparation for features planned in the next few major releases. However please note that as a result, this release has some minor breaking changes (which is why it's 2.7, not 2.6.2, in accordance with repo guidelines) though not to the core user-facing APIs. + +### Breaking Changes ๐Ÿ›ซ + +* decorators return the objects they create, not the decorated function +* websockets is an optional dependency +* methods on the server for automatically converting functions into tools/resources/prompts have been deprecated in favor of using the decorators directly + +### New Features ๐ŸŽ‰ + +* allow passing flags to servers by [@zzstoatzz](https://github.com/zzstoatzz) in [#690](https://github.com/jlowin/fastmcp/pull/690) +* replace $ref pointing to `#/components/schemas/` with `#/$defs/` by [@phateffect](https://github.com/phateffect) in [#697](https://github.com/jlowin/fastmcp/pull/697) +* Split Tool into Tool and FunctionTool by [@jlowin](https://github.com/jlowin) in [#700](https://github.com/jlowin/fastmcp/pull/700) +* Use strict basemodel for Prompt; relax from\_function deprecation by [@jlowin](https://github.com/jlowin) in [#701](https://github.com/jlowin/fastmcp/pull/701) +* Formalize resource/functionresource replationship by [@jlowin](https://github.com/jlowin) in [#702](https://github.com/jlowin/fastmcp/pull/702) +* Formalize template/functiontemplate split by [@jlowin](https://github.com/jlowin) in [#703](https://github.com/jlowin/fastmcp/pull/703) +* Support flexible @tool decorator call patterns by [@jlowin](https://github.com/jlowin) in [#706](https://github.com/jlowin/fastmcp/pull/706) +* Ensure deprecation warnings have stacklevel=2 by [@jlowin](https://github.com/jlowin) in [#710](https://github.com/jlowin/fastmcp/pull/710) +* Allow naked prompt decorator by [@jlowin](https://github.com/jlowin) in [#711](https://github.com/jlowin/fastmcp/pull/711) + +### Fixes ๐Ÿž + +* Updates / Fixes for Tool Content Conversion by [@strawgate](https://github.com/strawgate) in [#642](https://github.com/jlowin/fastmcp/pull/642) +* Fix pr labeler permissions by [@jlowin](https://github.com/jlowin) in [#708](https://github.com/jlowin/fastmcp/pull/708) +* remove -n auto by [@jlowin](https://github.com/jlowin) in [#709](https://github.com/jlowin/fastmcp/pull/709) +* Fix links in README.md by [@alainivars](https://github.com/alainivars) in [#723](https://github.com/jlowin/fastmcp/pull/723) + +Happily, this release DOES permit the use of "naked" decorators to align with Pythonic practice: + +```python +@mcp.tool +def my_tool(): + ... +``` + +**Full Changelog**: [v2.6.2...v2.7.0](https://github.com/jlowin/fastmcp/compare/v2.6.2...v2.7.0) + + + + +## [v2.6.1: Blast Auth (second ignition)](https://github.com/jlowin/fastmcp/releases/tag/v2.6.1) + +This is a patch release to restore py.typed in #686. + +### Docs ๐Ÿ“š + +* Update readme by [@jlowin](https://github.com/jlowin) in [#679](https://github.com/jlowin/fastmcp/pull/679) +* Add gemini tutorial by [@jlowin](https://github.com/jlowin) in [#680](https://github.com/jlowin/fastmcp/pull/680) +* Fix : fix path error to CLI Documentation by [@yrangana](https://github.com/yrangana) in [#684](https://github.com/jlowin/fastmcp/pull/684) +* Update auth docs by [@jlowin](https://github.com/jlowin) in [#687](https://github.com/jlowin/fastmcp/pull/687) + +### Other Changes ๐Ÿฆพ + +* Remove deprecation notice by [@jlowin](https://github.com/jlowin) in [#677](https://github.com/jlowin/fastmcp/pull/677) +* Delete server.py by [@jlowin](https://github.com/jlowin) in [#681](https://github.com/jlowin/fastmcp/pull/681) +* Restore py.typed by [@jlowin](https://github.com/jlowin) in [#686](https://github.com/jlowin/fastmcp/pull/686) + +### New Contributors + +* [@yrangana](https://github.com/yrangana) made their first contribution in [#684](https://github.com/jlowin/fastmcp/pull/684) + +**Full Changelog**: [v2.6.0...v2.6.1](https://github.com/jlowin/fastmcp/compare/v2.6.0...v2.6.1) + + + + +## [v2.6.0: Blast Auth](https://github.com/jlowin/fastmcp/releases/tag/v2.6.0) + +### New Features ๐ŸŽ‰ + +* Introduce MCP client oauth flow by [@jlowin](https://github.com/jlowin) in [#478](https://github.com/jlowin/fastmcp/pull/478) +* Support providing tools at init by [@jlowin](https://github.com/jlowin) in [#647](https://github.com/jlowin/fastmcp/pull/647) +* Simplify code for running servers in processes during tests by [@jlowin](https://github.com/jlowin) in [#649](https://github.com/jlowin/fastmcp/pull/649) +* Add basic bearer auth for server and client by [@jlowin](https://github.com/jlowin) in [#650](https://github.com/jlowin/fastmcp/pull/650) +* Support configuring bearer auth from env vars by [@jlowin](https://github.com/jlowin) in [#652](https://github.com/jlowin/fastmcp/pull/652) +* feat(tool): add support for excluding arguments from tool definition by [@deepak-stratforge](https://github.com/deepak-stratforge) in [#626](https://github.com/jlowin/fastmcp/pull/626) +* Add docs for server + client auth by [@jlowin](https://github.com/jlowin) in [#655](https://github.com/jlowin/fastmcp/pull/655) + +### Fixes ๐Ÿž + +* fix: Support concurrency in FastMcpProxy (and Client) by [@Sillocan](https://github.com/Sillocan) in [#635](https://github.com/jlowin/fastmcp/pull/635) +* Ensure Client.close() cleans up client context appropriately by [@jlowin](https://github.com/jlowin) in [#643](https://github.com/jlowin/fastmcp/pull/643) +* Update client.mdx: ClientError namespace by [@mjkaye](https://github.com/mjkaye) in [#657](https://github.com/jlowin/fastmcp/pull/657) + +### Docs ๐Ÿ“š + +* Make FastMCPTransport support simulated Streamable HTTP Transport (didn't work) by [@jlowin](https://github.com/jlowin) in [#645](https://github.com/jlowin/fastmcp/pull/645) +* Document exclude\_args by [@jlowin](https://github.com/jlowin) in [#653](https://github.com/jlowin/fastmcp/pull/653) +* Update welcome by [@jlowin](https://github.com/jlowin) in [#673](https://github.com/jlowin/fastmcp/pull/673) +* Add Anthropic + Claude desktop integration guides by [@jlowin](https://github.com/jlowin) in [#674](https://github.com/jlowin/fastmcp/pull/674) +* Minor docs design updates by [@jlowin](https://github.com/jlowin) in [#676](https://github.com/jlowin/fastmcp/pull/676) + +### Other Changes ๐Ÿฆพ + +* Update test typing by [@jlowin](https://github.com/jlowin) in [#646](https://github.com/jlowin/fastmcp/pull/646) +* Add OpenAI integration docs by [@jlowin](https://github.com/jlowin) in [#660](https://github.com/jlowin/fastmcp/pull/660) + +### New Contributors + +* [@Sillocan](https://github.com/Sillocan) made their first contribution in [#635](https://github.com/jlowin/fastmcp/pull/635) +* [@deepak-stratforge](https://github.com/deepak-stratforge) made their first contribution in [#626](https://github.com/jlowin/fastmcp/pull/626) +* [@mjkaye](https://github.com/mjkaye) made their first contribution in [#657](https://github.com/jlowin/fastmcp/pull/657) + +**Full Changelog**: [v2.5.2...v2.6.0](https://github.com/jlowin/fastmcp/compare/v2.5.2...v2.6.0) + + + + +## [v2.5.2: Stayin' Alive](https://github.com/jlowin/fastmcp/releases/tag/v2.5.2) + +### New Features ๐ŸŽ‰ + +* Add graceful error handling for unreachable mounted servers by [@davenpi](https://github.com/davenpi) in [#605](https://github.com/jlowin/fastmcp/pull/605) +* Improve type inference from client transport by [@jlowin](https://github.com/jlowin) in [#623](https://github.com/jlowin/fastmcp/pull/623) +* Add keep\_alive param to reuse subprocess by [@jlowin](https://github.com/jlowin) in [#624](https://github.com/jlowin/fastmcp/pull/624) + +### Fixes ๐Ÿž + +* Fix handling tools without descriptions by [@jlowin](https://github.com/jlowin) in [#610](https://github.com/jlowin/fastmcp/pull/610) +* Don't print env vars to console when format is wrong by [@jlowin](https://github.com/jlowin) in [#615](https://github.com/jlowin/fastmcp/pull/615) +* Ensure behavior-affecting headers are excluded when forwarding proxies/openapi by [@jlowin](https://github.com/jlowin) in [#620](https://github.com/jlowin/fastmcp/pull/620) + +### Docs ๐Ÿ“š + +* Add notes about uv and claude desktop by [@jlowin](https://github.com/jlowin) in [#597](https://github.com/jlowin/fastmcp/pull/597) + +### Other Changes ๐Ÿฆพ + +* add init\_timeout for mcp client by [@jfouret](https://github.com/jfouret) in [#607](https://github.com/jlowin/fastmcp/pull/607) +* Add init\_timeout for mcp client (incl settings) by [@jlowin](https://github.com/jlowin) in [#609](https://github.com/jlowin/fastmcp/pull/609) +* Support for uppercase letters at the log level by [@ksawaray](https://github.com/ksawaray) in [#625](https://github.com/jlowin/fastmcp/pull/625) + +### New Contributors + +* [@jfouret](https://github.com/jfouret) made their first contribution in [#607](https://github.com/jlowin/fastmcp/pull/607) +* [@ksawaray](https://github.com/ksawaray) made their first contribution in [#625](https://github.com/jlowin/fastmcp/pull/625) + +**Full Changelog**: [v2.5.1...v2.5.2](https://github.com/jlowin/fastmcp/compare/v2.5.1...v2.5.2) + + + + +## [v2.5.1: Route Awakening (Part 2)](https://github.com/jlowin/fastmcp/releases/tag/v2.5.1) + +### Fixes ๐Ÿž + +* Ensure content-length is always stripped from client headers by [@jlowin](https://github.com/jlowin) in [#589](https://github.com/jlowin/fastmcp/pull/589) + +### Docs ๐Ÿ“š + +* Fix redundant section of docs by [@jlowin](https://github.com/jlowin) in [#583](https://github.com/jlowin/fastmcp/pull/583) + +**Full Changelog**: [v2.5.0...v2.5.1](https://github.com/jlowin/fastmcp/compare/v2.5.0...v2.5.1) + + + + +## [v2.5.0: Route Awakening](https://github.com/jlowin/fastmcp/releases/tag/v2.5.0) + +This release introduces completely new tools for generating and customizing MCP servers from OpenAPI specs and FastAPI apps, including popular requests like mechanisms for determining what routes map to what MCP components; renaming routes; and customizing the generated MCP components. + +### New Features ๐ŸŽ‰ + +* Add FastMCP 1.0 server support for in-memory Client / Testing by [@jlowin](https://github.com/jlowin) in [#539](https://github.com/jlowin/fastmcp/pull/539) +* Minor addition: add transport to stdio server in mcpconfig, with default by [@jlowin](https://github.com/jlowin) in [#555](https://github.com/jlowin/fastmcp/pull/555) +* Raise an error if a Client is created with no servers in config by [@jlowin](https://github.com/jlowin) in [#554](https://github.com/jlowin/fastmcp/pull/554) +* Expose model preferences in `Context.sample` for flexible model selection. by [@davenpi](https://github.com/davenpi) in [#542](https://github.com/jlowin/fastmcp/pull/542) +* Ensure custom routes are respected by [@jlowin](https://github.com/jlowin) in [#558](https://github.com/jlowin/fastmcp/pull/558) +* Add client method to send cancellation notifications by [@davenpi](https://github.com/davenpi) in [#563](https://github.com/jlowin/fastmcp/pull/563) +* Enhance route map logic for include/exclude OpenAPI routes by [@jlowin](https://github.com/jlowin) in [#564](https://github.com/jlowin/fastmcp/pull/564) +* Add tag-based route maps by [@jlowin](https://github.com/jlowin) in [#565](https://github.com/jlowin/fastmcp/pull/565) +* Add advanced control of openAPI route creation by [@jlowin](https://github.com/jlowin) in [#566](https://github.com/jlowin/fastmcp/pull/566) +* Make error masking configurable by [@jlowin](https://github.com/jlowin) in [#550](https://github.com/jlowin/fastmcp/pull/550) +* Ensure client headers are passed through to remote servers by [@jlowin](https://github.com/jlowin) in [#575](https://github.com/jlowin/fastmcp/pull/575) +* Use lowercase name for headers when comparing by [@jlowin](https://github.com/jlowin) in [#576](https://github.com/jlowin/fastmcp/pull/576) +* Permit more flexible name generation for OpenAPI servers by [@jlowin](https://github.com/jlowin) in [#578](https://github.com/jlowin/fastmcp/pull/578) +* Ensure that tools/templates/prompts are compatible with callable objects by [@jlowin](https://github.com/jlowin) in [#579](https://github.com/jlowin/fastmcp/pull/579) + +### Docs ๐Ÿ“š + +* Add version badge for prefix formats by [@jlowin](https://github.com/jlowin) in [#537](https://github.com/jlowin/fastmcp/pull/537) +* Add versioning note to docs by [@jlowin](https://github.com/jlowin) in [#551](https://github.com/jlowin/fastmcp/pull/551) +* Bump 2.3.6 references to 2.4.0 by [@jlowin](https://github.com/jlowin) in [#567](https://github.com/jlowin/fastmcp/pull/567) + +**Full Changelog**: [v2.4.0...v2.5.0](https://github.com/jlowin/fastmcp/compare/v2.4.0...v2.5.0) + + + + +## [v2.4.0: Config and Conquer](https://github.com/jlowin/fastmcp/releases/tag/v2.4.0) + +**Note**: this release includes a backwards-incompatible change to how resources are prefixed when mounted in composed servers. However, it is only backwards-incompatible if users were running tests or manually loading resources by prefixed key; LLMs should not have any issue discovering the new route. See [Resource Prefix Formats](https://gofastmcp.com/servers/composition#resource-prefix-formats) for more. + +### New Features ๐ŸŽ‰ + +* Allow \* Methods and all routes as tools shortcuts by [@jlowin](https://github.com/jlowin) in [#520](https://github.com/jlowin/fastmcp/pull/520) +* Improved support for config dicts by [@jlowin](https://github.com/jlowin) in [#522](https://github.com/jlowin/fastmcp/pull/522) +* Support creating clients from MCP config dicts, including multi-server clients by [@jlowin](https://github.com/jlowin) in [#527](https://github.com/jlowin/fastmcp/pull/527) +* Make resource prefix format configurable by [@jlowin](https://github.com/jlowin) in [#534](https://github.com/jlowin/fastmcp/pull/534) + +### Fixes ๐Ÿž + +* Avoid hanging on initializing server session by [@jlowin](https://github.com/jlowin) in [#523](https://github.com/jlowin/fastmcp/pull/523) + +### Breaking Changes ๐Ÿ›ซ + +* Remove customizable separators; improve resource separator by [@jlowin](https://github.com/jlowin) in [#526](https://github.com/jlowin/fastmcp/pull/526) + +### Docs ๐Ÿ“š + +* Improve client documentation by [@jlowin](https://github.com/jlowin) in [#517](https://github.com/jlowin/fastmcp/pull/517) + +### Other Changes ๐Ÿฆพ + +* Ensure openapi path params are handled properly by [@jlowin](https://github.com/jlowin) in [#519](https://github.com/jlowin/fastmcp/pull/519) +* better error when missing lifespan by [@zzstoatzz](https://github.com/zzstoatzz) in [#521](https://github.com/jlowin/fastmcp/pull/521) + +**Full Changelog**: [v2.3.5...v2.4.0](https://github.com/jlowin/fastmcp/compare/v2.3.5...v2.4.0) + + + + +## [v2.3.5: Making Progress](https://github.com/jlowin/fastmcp/releases/tag/v2.3.5) + +### New Features ๐ŸŽ‰ + +* support messages in progress notifications by [@rickygenhealth](https://github.com/rickygenhealth) in [#471](https://github.com/jlowin/fastmcp/pull/471) +* feat: Add middleware option in server.run by [@Maxi91f](https://github.com/Maxi91f) in [#475](https://github.com/jlowin/fastmcp/pull/475) +* Add lifespan property to app by [@jlowin](https://github.com/jlowin) in [#483](https://github.com/jlowin/fastmcp/pull/483) +* Update `fastmcp run` to work with remote servers by [@jlowin](https://github.com/jlowin) in [#491](https://github.com/jlowin/fastmcp/pull/491) +* Add FastMCP.as\_proxy() by [@jlowin](https://github.com/jlowin) in [#490](https://github.com/jlowin/fastmcp/pull/490) +* Infer sse transport from urls containing /sse by [@jlowin](https://github.com/jlowin) in [#512](https://github.com/jlowin/fastmcp/pull/512) +* Add progress handler to client by [@jlowin](https://github.com/jlowin) in [#513](https://github.com/jlowin/fastmcp/pull/513) +* Store the initialize result on the client by [@jlowin](https://github.com/jlowin) in [#509](https://github.com/jlowin/fastmcp/pull/509) + +### Fixes ๐Ÿž + +* Remove patch and use upstream SSEServerTransport by [@jlowin](https://github.com/jlowin) in [#425](https://github.com/jlowin/fastmcp/pull/425) + +### Docs ๐Ÿ“š + +* Update transport docs by [@jlowin](https://github.com/jlowin) in [#458](https://github.com/jlowin/fastmcp/pull/458) +* update proxy docs + example by [@zzstoatzz](https://github.com/zzstoatzz) in [#460](https://github.com/jlowin/fastmcp/pull/460) +* doc(asgi): Change custom route example to PlainTextResponse by [@mcw0933](https://github.com/mcw0933) in [#477](https://github.com/jlowin/fastmcp/pull/477) +* Store FastMCP instance on app.state.fastmcp\_server by [@jlowin](https://github.com/jlowin) in [#489](https://github.com/jlowin/fastmcp/pull/489) +* Improve AGENTS.md overview by [@jlowin](https://github.com/jlowin) in [#492](https://github.com/jlowin/fastmcp/pull/492) +* Update release numbers for anticipated version by [@jlowin](https://github.com/jlowin) in [#516](https://github.com/jlowin/fastmcp/pull/516) + +### Other Changes ๐Ÿฆพ + +* run tests on all PRs by [@jlowin](https://github.com/jlowin) in [#468](https://github.com/jlowin/fastmcp/pull/468) +* add null check by [@zzstoatzz](https://github.com/zzstoatzz) in [#473](https://github.com/jlowin/fastmcp/pull/473) +* strict typing for `server.py` by [@zzstoatzz](https://github.com/zzstoatzz) in [#476](https://github.com/jlowin/fastmcp/pull/476) +* Doc(quickstart): Fix import statements by [@mai-nakagawa](https://github.com/mai-nakagawa) in [#479](https://github.com/jlowin/fastmcp/pull/479) +* Add labeler by [@jlowin](https://github.com/jlowin) in [#484](https://github.com/jlowin/fastmcp/pull/484) +* Fix flaky timeout test by increasing timeout (#474) by [@davenpi](https://github.com/davenpi) in [#486](https://github.com/jlowin/fastmcp/pull/486) +* Skipping `test_permission_error` if runner is root. by [@ZiadAmerr](https://github.com/ZiadAmerr) in [#502](https://github.com/jlowin/fastmcp/pull/502) +* allow passing full uvicorn config by [@zzstoatzz](https://github.com/zzstoatzz) in [#504](https://github.com/jlowin/fastmcp/pull/504) +* Skip timeout tests on windows by [@jlowin](https://github.com/jlowin) in [#514](https://github.com/jlowin/fastmcp/pull/514) + +### New Contributors + +* [@rickygenhealth](https://github.com/rickygenhealth) made their first contribution in [#471](https://github.com/jlowin/fastmcp/pull/471) +* [@Maxi91f](https://github.com/Maxi91f) made their first contribution in [#475](https://github.com/jlowin/fastmcp/pull/475) +* [@mcw0933](https://github.com/mcw0933) made their first contribution in [#477](https://github.com/jlowin/fastmcp/pull/477) +* [@mai-nakagawa](https://github.com/mai-nakagawa) made their first contribution in [#479](https://github.com/jlowin/fastmcp/pull/479) +* [@ZiadAmerr](https://github.com/ZiadAmerr) made their first contribution in [#502](https://github.com/jlowin/fastmcp/pull/502) + +**Full Changelog**: [v2.3.4...v2.3.5](https://github.com/jlowin/fastmcp/compare/v2.3.4...v2.3.5) + + + + +## [v2.3.4: Error Today, Gone Tomorrow](https://github.com/jlowin/fastmcp/releases/tag/v2.3.4) + +### New Features ๐ŸŽ‰ + +* logging stack trace for easier debugging by [@jbkoh](https://github.com/jbkoh) in [#413](https://github.com/jlowin/fastmcp/pull/413) +* add missing StreamableHttpTransport in client exports by [@yihuang](https://github.com/yihuang) in [#408](https://github.com/jlowin/fastmcp/pull/408) +* Improve error handling for tools and resources by [@jlowin](https://github.com/jlowin) in [#434](https://github.com/jlowin/fastmcp/pull/434) +* feat: add support for removing tools from server by [@davenpi](https://github.com/davenpi) in [#437](https://github.com/jlowin/fastmcp/pull/437) +* Prune titles from JSONSchemas by [@jlowin](https://github.com/jlowin) in [#449](https://github.com/jlowin/fastmcp/pull/449) +* Declare toolsChanged capability for stdio server. by [@davenpi](https://github.com/davenpi) in [#450](https://github.com/jlowin/fastmcp/pull/450) +* Improve handling of exceptiongroups when raised in clients by [@jlowin](https://github.com/jlowin) in [#452](https://github.com/jlowin/fastmcp/pull/452) +* Add timeout support to client by [@jlowin](https://github.com/jlowin) in [#455](https://github.com/jlowin/fastmcp/pull/455) + +### Fixes ๐Ÿž + +* Pin to mcp 1.8.1 to resolve callback deadlocks with SHTTP by [@jlowin](https://github.com/jlowin) in [#427](https://github.com/jlowin/fastmcp/pull/427) +* Add reprs for OpenAPI objects by [@jlowin](https://github.com/jlowin) in [#447](https://github.com/jlowin/fastmcp/pull/447) +* Ensure openapi defs for structured objects are loaded properly by [@jlowin](https://github.com/jlowin) in [#448](https://github.com/jlowin/fastmcp/pull/448) +* Ensure tests run against correct python version by [@jlowin](https://github.com/jlowin) in [#454](https://github.com/jlowin/fastmcp/pull/454) +* Ensure result is only returned if a new key was found by [@jlowin](https://github.com/jlowin) in [#456](https://github.com/jlowin/fastmcp/pull/456) + +### Docs ๐Ÿ“š + +* Add documentation for tool removal by [@jlowin](https://github.com/jlowin) in [#440](https://github.com/jlowin/fastmcp/pull/440) + +### Other Changes ๐Ÿฆพ + +* Deprecate passing settings to the FastMCP instance by [@jlowin](https://github.com/jlowin) in [#424](https://github.com/jlowin/fastmcp/pull/424) +* Add path prefix to test by [@jlowin](https://github.com/jlowin) in [#432](https://github.com/jlowin/fastmcp/pull/432) + +### New Contributors + +* [@jbkoh](https://github.com/jbkoh) made their first contribution in [#413](https://github.com/jlowin/fastmcp/pull/413) +* [@davenpi](https://github.com/davenpi) made their first contribution in [#437](https://github.com/jlowin/fastmcp/pull/437) + +**Full Changelog**: [v2.3.3...v2.3.4](https://github.com/jlowin/fastmcp/compare/v2.3.3...v2.3.4) + + + + +## [v2.3.3: SSE you later](https://github.com/jlowin/fastmcp/releases/tag/v2.3.3) + +This is a hotfix for a bug introduced in 2.3.2 that broke SSE servers + +### Fixes ๐Ÿž + +* Fix bug that sets message path and sse path to same value by [@jlowin](https://github.com/jlowin) in [#405](https://github.com/jlowin/fastmcp/pull/405) + +### Docs ๐Ÿ“š + +* Update composition docs by [@jlowin](https://github.com/jlowin) in [#403](https://github.com/jlowin/fastmcp/pull/403) + +### Other Changes ๐Ÿฆพ + +* Add test for no prefix when importing by [@jlowin](https://github.com/jlowin) in [#404](https://github.com/jlowin/fastmcp/pull/404) + +**Full Changelog**: [v2.3.2...v2.3.3](https://github.com/jlowin/fastmcp/compare/v2.3.2...v2.3.3) + + + + +## [v2.3.2: Stuck in the Middleware With You](https://github.com/jlowin/fastmcp/releases/tag/v2.3.2) + +### New Features ๐ŸŽ‰ + +* Allow users to pass middleware to starlette app constructors by [@jlowin](https://github.com/jlowin) in [#398](https://github.com/jlowin/fastmcp/pull/398) +* Deprecate transport-specific methods on FastMCP server by [@jlowin](https://github.com/jlowin) in [#401](https://github.com/jlowin/fastmcp/pull/401) + +### Docs ๐Ÿ“š + +* Update CLI docs by [@jlowin](https://github.com/jlowin) in [#402](https://github.com/jlowin/fastmcp/pull/402) + +### Other Changes ๐Ÿฆพ + +* Adding 23 tests for CLI by [@didier-durand](https://github.com/didier-durand) in [#394](https://github.com/jlowin/fastmcp/pull/394) + +**Full Changelog**: [v2.3.1...v2.3.2](https://github.com/jlowin/fastmcp/compare/v2.3.1...v2.3.2) + + + + +## [v2.3.1: For Good-nests Sake](https://github.com/jlowin/fastmcp/releases/tag/v2.3.1) + +This release primarily patches a long-standing bug with nested ASGI SSE servers. + +### Fixes ๐Ÿž + +* Fix tool result serialization when the tool returns a list by [@strawgate](https://github.com/strawgate) in [#379](https://github.com/jlowin/fastmcp/pull/379) +* Ensure FastMCP handles nested SSE and SHTTP apps properly in ASGI frameworks by [@jlowin](https://github.com/jlowin) in [#390](https://github.com/jlowin/fastmcp/pull/390) + +### Docs ๐Ÿ“š + +* Update transport docs by [@jlowin](https://github.com/jlowin) in [#377](https://github.com/jlowin/fastmcp/pull/377) +* Add llms.txt to docs by [@jlowin](https://github.com/jlowin) in [#384](https://github.com/jlowin/fastmcp/pull/384) +* Fixing various text typos by [@didier-durand](https://github.com/didier-durand) in [#385](https://github.com/jlowin/fastmcp/pull/385) + +### Other Changes ๐Ÿฆพ + +* Adding a few tests to Image type by [@didier-durand](https://github.com/didier-durand) in [#387](https://github.com/jlowin/fastmcp/pull/387) +* Adding tests for TimedCache by [@didier-durand](https://github.com/didier-durand) in [#388](https://github.com/jlowin/fastmcp/pull/388) + +### New Contributors + +* [@didier-durand](https://github.com/didier-durand) made their first contribution in [#385](https://github.com/jlowin/fastmcp/pull/385) + +**Full Changelog**: [v2.3.0...v2.3.1](https://github.com/jlowin/fastmcp/compare/v2.3.0...v2.3.1) + + + + +## [v2.3.0: Stream Me Up, Scotty](https://github.com/jlowin/fastmcp/releases/tag/v2.3.0) + +### New Features ๐ŸŽ‰ + +* Add streaming support for HTTP transport by [@jlowin](https://github.com/jlowin) in [#365](https://github.com/jlowin/fastmcp/pull/365) +* Support streaming HTTP transport in clients by [@jlowin](https://github.com/jlowin) in [#366](https://github.com/jlowin/fastmcp/pull/366) +* Add streaming support to CLI by [@jlowin](https://github.com/jlowin) in [#367](https://github.com/jlowin/fastmcp/pull/367) + +### Fixes ๐Ÿž + +* Fix streaming transport initialization by [@jlowin](https://github.com/jlowin) in [#368](https://github.com/jlowin/fastmcp/pull/368) + +### Docs ๐Ÿ“š + +* Update transport documentation for streaming support by [@jlowin](https://github.com/jlowin) in [#369](https://github.com/jlowin/fastmcp/pull/369) + +**Full Changelog**: [v2.2.10...v2.3.0](https://github.com/jlowin/fastmcp/compare/v2.2.10...v2.3.0) + + + + +## [v2.2.10: That's JSON Bourne](https://github.com/jlowin/fastmcp/releases/tag/v2.2.10) + +### Fixes ๐Ÿž + +* Disable automatic JSON parsing of tool args by [@jlowin](https://github.com/jlowin) in [#341](https://github.com/jlowin/fastmcp/pull/341) +* Fix prompt test by [@jlowin](https://github.com/jlowin) in [#342](https://github.com/jlowin/fastmcp/pull/342) + +### Other Changes ๐Ÿฆพ + +* Update docs.json by [@jlowin](https://github.com/jlowin) in [#338](https://github.com/jlowin/fastmcp/pull/338) +* Add test coverage + tests on 4 examples by [@alainivars](https://github.com/alainivars) in [#306](https://github.com/jlowin/fastmcp/pull/306) + +### New Contributors + +* [@alainivars](https://github.com/alainivars) made their first contribution in [#306](https://github.com/jlowin/fastmcp/pull/306) + +**Full Changelog**: [v2.2.9...v2.2.10](https://github.com/jlowin/fastmcp/compare/v2.2.9...v2.2.10) + + + + +## [v2.2.9: Str-ing the Pot (Hotfix)](https://github.com/jlowin/fastmcp/releases/tag/v2.2.9) + +This release is a hotfix for the issue detailed in #330 + +### Fixes ๐Ÿž + +* Prevent invalid resource URIs by [@jlowin](https://github.com/jlowin) in [#336](https://github.com/jlowin/fastmcp/pull/336) +* Coerce numbers to str by [@jlowin](https://github.com/jlowin) in [#337](https://github.com/jlowin/fastmcp/pull/337) + +### Docs ๐Ÿ“š + +* Add client badge by [@jlowin](https://github.com/jlowin) in [#327](https://github.com/jlowin/fastmcp/pull/327) +* Update bug.yml by [@jlowin](https://github.com/jlowin) in [#328](https://github.com/jlowin/fastmcp/pull/328) + +### Other Changes ๐Ÿฆพ + +* Update quickstart.mdx example to include import by [@discdiver](https://github.com/discdiver) in [#329](https://github.com/jlowin/fastmcp/pull/329) + +### New Contributors + +* [@discdiver](https://github.com/discdiver) made their first contribution in [#329](https://github.com/jlowin/fastmcp/pull/329) + +**Full Changelog**: [v2.2.8...v2.2.9](https://github.com/jlowin/fastmcp/compare/v2.2.8...v2.2.9) + + + + +## [v2.2.8: Parse and Recreation](https://github.com/jlowin/fastmcp/releases/tag/v2.2.8) + +### New Features ๐ŸŽ‰ + +* Replace custom parsing with TypeAdapter by [@jlowin](https://github.com/jlowin) in [#314](https://github.com/jlowin/fastmcp/pull/314) +* Handle \*args/\*\*kwargs appropriately for various components by [@jlowin](https://github.com/jlowin) in [#317](https://github.com/jlowin/fastmcp/pull/317) +* Add timeout-graceful-shutdown as a default config for SSE app by [@jlowin](https://github.com/jlowin) in [#323](https://github.com/jlowin/fastmcp/pull/323) +* Ensure prompts return descriptions by [@jlowin](https://github.com/jlowin) in [#325](https://github.com/jlowin/fastmcp/pull/325) + +### Fixes ๐Ÿž + +* Ensure that tool serialization has a graceful fallback by [@jlowin](https://github.com/jlowin) in [#310](https://github.com/jlowin/fastmcp/pull/310) + +### Docs ๐Ÿ“š + +* Update docs for clarity by [@jlowin](https://github.com/jlowin) in [#312](https://github.com/jlowin/fastmcp/pull/312) + +### Other Changes ๐Ÿฆพ + +* Remove is\_async attribute by [@jlowin](https://github.com/jlowin) in [#315](https://github.com/jlowin/fastmcp/pull/315) +* Dry out retrieving context kwarg by [@jlowin](https://github.com/jlowin) in [#316](https://github.com/jlowin/fastmcp/pull/316) + +**Full Changelog**: [v2.2.7...v2.2.8](https://github.com/jlowin/fastmcp/compare/v2.2.7...v2.2.8) + + + + +## [v2.2.7: You Auth to Know Better](https://github.com/jlowin/fastmcp/releases/tag/v2.2.7) + +### New Features ๐ŸŽ‰ + +* use pydantic\_core.to\_json by [@jlowin](https://github.com/jlowin) in [#290](https://github.com/jlowin/fastmcp/pull/290) +* Ensure openapi descriptions are included in tool details by [@jlowin](https://github.com/jlowin) in [#293](https://github.com/jlowin/fastmcp/pull/293) +* Bump mcp to 1.7.1 by [@jlowin](https://github.com/jlowin) in [#298](https://github.com/jlowin/fastmcp/pull/298) +* Add support for tool annotations by [@jlowin](https://github.com/jlowin) in [#299](https://github.com/jlowin/fastmcp/pull/299) +* Add auth support by [@jlowin](https://github.com/jlowin) in [#300](https://github.com/jlowin/fastmcp/pull/300) +* Add low-level methods to client by [@jlowin](https://github.com/jlowin) in [#301](https://github.com/jlowin/fastmcp/pull/301) +* Add method for retrieving current starlette request to FastMCP context by [@jlowin](https://github.com/jlowin) in [#302](https://github.com/jlowin/fastmcp/pull/302) +* get\_starlette\_request โ†’ get\_http\_request by [@jlowin](https://github.com/jlowin) in [#303](https://github.com/jlowin/fastmcp/pull/303) +* Support custom Serializer for Tools by [@strawgate](https://github.com/strawgate) in [#308](https://github.com/jlowin/fastmcp/pull/308) +* Support proxy mount by [@jlowin](https://github.com/jlowin) in [#309](https://github.com/jlowin/fastmcp/pull/309) + +### Other Changes ๐Ÿฆพ + +* Improve context injection type checks by [@jlowin](https://github.com/jlowin) in [#291](https://github.com/jlowin/fastmcp/pull/291) +* add readme to smarthome example by [@zzstoatzz](https://github.com/zzstoatzz) in [#294](https://github.com/jlowin/fastmcp/pull/294) + +**Full Changelog**: [v2.2.6...v2.2.7](https://github.com/jlowin/fastmcp/compare/v2.2.6...v2.2.7) + + + + +## [v2.2.6: The REST is History](https://github.com/jlowin/fastmcp/releases/tag/v2.2.6) + +### New Features ๐ŸŽ‰ + +* Added feature : Load MCP server using config by [@sandipan1](https://github.com/sandipan1) in [#260](https://github.com/jlowin/fastmcp/pull/260) +* small typing fixes by [@zzstoatzz](https://github.com/zzstoatzz) in [#237](https://github.com/jlowin/fastmcp/pull/237) +* Expose configurable timeout for OpenAPI by [@jlowin](https://github.com/jlowin) in [#279](https://github.com/jlowin/fastmcp/pull/279) +* Lower websockets pin for compatibility by [@jlowin](https://github.com/jlowin) in [#286](https://github.com/jlowin/fastmcp/pull/286) +* Improve OpenAPI param handling by [@jlowin](https://github.com/jlowin) in [#287](https://github.com/jlowin/fastmcp/pull/287) + +### Fixes ๐Ÿž + +* Ensure openapi tool responses are properly converted by [@jlowin](https://github.com/jlowin) in [#283](https://github.com/jlowin/fastmcp/pull/283) +* Fix OpenAPI examples by [@jlowin](https://github.com/jlowin) in [#285](https://github.com/jlowin/fastmcp/pull/285) +* Fix client docs for advanced features, add tests for logging by [@jlowin](https://github.com/jlowin) in [#284](https://github.com/jlowin/fastmcp/pull/284) + +### Other Changes ๐Ÿฆพ + +* add testing doc by [@jlowin](https://github.com/jlowin) in [#264](https://github.com/jlowin/fastmcp/pull/264) +* #267 Fix openapi template resource to support multiple path parameters by [@jeger-at](https://github.com/jeger-at) in [#278](https://github.com/jlowin/fastmcp/pull/278) + +### New Contributors + +* [@sandipan1](https://github.com/sandipan1) made their first contribution in [#260](https://github.com/jlowin/fastmcp/pull/260) +* [@jeger-at](https://github.com/jeger-at) made their first contribution in [#278](https://github.com/jlowin/fastmcp/pull/278) + +**Full Changelog**: [v2.2.5...v2.2.6](https://github.com/jlowin/fastmcp/compare/v2.2.5...v2.2.6) + + + + +## [v2.2.5: Context Switching](https://github.com/jlowin/fastmcp/releases/tag/v2.2.5) + +### New Features ๐ŸŽ‰ + +* Add tests for tool return types; improve serialization behavior by [@jlowin](https://github.com/jlowin) in [#262](https://github.com/jlowin/fastmcp/pull/262) +* Support context injection in resources, templates, and prompts (like tools) by [@jlowin](https://github.com/jlowin) in [#263](https://github.com/jlowin/fastmcp/pull/263) + +### Docs ๐Ÿ“š + +* Update wildcards to 2.2.4 by [@jlowin](https://github.com/jlowin) in [#257](https://github.com/jlowin/fastmcp/pull/257) +* Update note in templates docs by [@jlowin](https://github.com/jlowin) in [#258](https://github.com/jlowin/fastmcp/pull/258) +* Significant documentation and test expansion for tool input types by [@jlowin](https://github.com/jlowin) in [#261](https://github.com/jlowin/fastmcp/pull/261) + +**Full Changelog**: [v2.2.4...v2.2.5](https://github.com/jlowin/fastmcp/compare/v2.2.4...v2.2.5) + + + + +## [v2.2.4: The Wild Side, Actually](https://github.com/jlowin/fastmcp/releases/tag/v2.2.4) + +The wildcard URI templates exposed in v2.2.3 were blocked by a server-level check which is removed in this release. + +### New Features ๐ŸŽ‰ + +* Allow customization of inspector proxy port, ui port, and version by [@jlowin](https://github.com/jlowin) in [#253](https://github.com/jlowin/fastmcp/pull/253) + +### Fixes ๐Ÿž + +* fix: unintended type convert by [@cutekibry](https://github.com/cutekibry) in [#252](https://github.com/jlowin/fastmcp/pull/252) +* Ensure openapi resources return valid responses by [@jlowin](https://github.com/jlowin) in [#254](https://github.com/jlowin/fastmcp/pull/254) +* Ensure servers expose template wildcards by [@jlowin](https://github.com/jlowin) in [#256](https://github.com/jlowin/fastmcp/pull/256) + +### Docs ๐Ÿ“š + +* Update README.md Grammar error by [@TechWithTy](https://github.com/TechWithTy) in [#249](https://github.com/jlowin/fastmcp/pull/249) + +### Other Changes ๐Ÿฆพ + +* Add resource template tests by [@jlowin](https://github.com/jlowin) in [#255](https://github.com/jlowin/fastmcp/pull/255) + +### New Contributors + +* [@TechWithTy](https://github.com/TechWithTy) made their first contribution in [#249](https://github.com/jlowin/fastmcp/pull/249) +* [@cutekibry](https://github.com/cutekibry) made their first contribution in [#252](https://github.com/jlowin/fastmcp/pull/252) + +**Full Changelog**: [v2.2.3...v2.2.4](https://github.com/jlowin/fastmcp/compare/v2.2.3...v2.2.4) + + + + +## [v2.2.3: The Wild Side](https://github.com/jlowin/fastmcp/releases/tag/v2.2.3) + +### New Features ๐ŸŽ‰ + +* Add wildcard params for resource templates by [@jlowin](https://github.com/jlowin) in [#246](https://github.com/jlowin/fastmcp/pull/246) + +### Docs ๐Ÿ“š + +* Indicate that Image class is for returns by [@jlowin](https://github.com/jlowin) in [#242](https://github.com/jlowin/fastmcp/pull/242) +* Update mermaid diagram by [@jlowin](https://github.com/jlowin) in [#243](https://github.com/jlowin/fastmcp/pull/243) + +### Other Changes ๐Ÿฆพ + +* update version badges by [@jlowin](https://github.com/jlowin) in [#248](https://github.com/jlowin/fastmcp/pull/248) + +**Full Changelog**: [v2.2.2...v2.2.3](https://github.com/jlowin/fastmcp/compare/v2.2.2...v2.2.3) + + + + +## [v2.2.2: Prompt and Circumstance](https://github.com/jlowin/fastmcp/releases/tag/v2.2.2) + +### New Features ๐ŸŽ‰ + +* Add prompt support by [@jlowin](https://github.com/jlowin) in [#235](https://github.com/jlowin/fastmcp/pull/235) + +### Fixes ๐Ÿž + +* Ensure that resource templates are properly exposed by [@jlowin](https://github.com/jlowin) in [#238](https://github.com/jlowin/fastmcp/pull/238) + +### Docs ๐Ÿ“š + +* Update docs for prompts by [@jlowin](https://github.com/jlowin) in [#236](https://github.com/jlowin/fastmcp/pull/236) + +### Other Changes ๐Ÿฆพ + +* Add prompt tests by [@jlowin](https://github.com/jlowin) in [#239](https://github.com/jlowin/fastmcp/pull/239) + +**Full Changelog**: [v2.2.1...v2.2.2](https://github.com/jlowin/fastmcp/compare/v2.2.1...v2.2.2) + + + + +## [v2.2.1: Template for Success](https://github.com/jlowin/fastmcp/releases/tag/v2.2.1) + +### New Features ๐ŸŽ‰ + +* Add resource templates by [@jlowin](https://github.com/jlowin) in [#230](https://github.com/jlowin/fastmcp/pull/230) + +### Fixes ๐Ÿž + +* Ensure that resource templates are properly exposed by [@jlowin](https://github.com/jlowin) in [#231](https://github.com/jlowin/fastmcp/pull/231) + +### Docs ๐Ÿ“š + +* Update docs for resource templates by [@jlowin](https://github.com/jlowin) in [#232](https://github.com/jlowin/fastmcp/pull/232) + +### Other Changes ๐Ÿฆพ + +* Add resource template tests by [@jlowin](https://github.com/jlowin) in [#233](https://github.com/jlowin/fastmcp/pull/233) + +**Full Changelog**: [v2.2.0...v2.2.1](https://github.com/jlowin/fastmcp/compare/v2.2.0...v2.2.1) + + + + +## [v2.2.0: Compose Yourself](https://github.com/jlowin/fastmcp/releases/tag/v2.2.0) + +### New Features ๐ŸŽ‰ + +* Add support for mounting FastMCP servers by [@jlowin](https://github.com/jlowin) in [#175](https://github.com/jlowin/fastmcp/pull/175) +* Add support for duplicate behavior == ignore by [@jlowin](https://github.com/jlowin) in [#169](https://github.com/jlowin/fastmcp/pull/169) + +### Breaking Changes ๐Ÿ›ซ + +* Refactor MCP composition by [@jlowin](https://github.com/jlowin) in [#176](https://github.com/jlowin/fastmcp/pull/176) + +### Docs ๐Ÿ“š + +* Improve integration documentation by [@jlowin](https://github.com/jlowin) in [#184](https://github.com/jlowin/fastmcp/pull/184) +* Improve documentation by [@jlowin](https://github.com/jlowin) in [#185](https://github.com/jlowin/fastmcp/pull/185) + +### Other Changes ๐Ÿฆพ + +* Add transport kwargs for mcp.run() and fastmcp run by [@jlowin](https://github.com/jlowin) in [#161](https://github.com/jlowin/fastmcp/pull/161) +* Allow resource templates to have optional / excluded arguments by [@jlowin](https://github.com/jlowin) in [#164](https://github.com/jlowin/fastmcp/pull/164) +* Update resources.mdx by [@jlowin](https://github.com/jlowin) in [#165](https://github.com/jlowin/fastmcp/pull/165) + +### New Contributors + +* [@kongqi404](https://github.com/kongqi404) made their first contribution in [#181](https://github.com/jlowin/fastmcp/pull/181) + +**Full Changelog**: [v2.1.2...v2.2.0](https://github.com/jlowin/fastmcp/compare/v2.1.2...v2.2.0) + + + + +## [v2.1.2: Copy That, Good Buddy](https://github.com/jlowin/fastmcp/releases/tag/v2.1.2) + +The main improvement in this release is a fix that allows FastAPI / OpenAPI-generated servers to be mounted as sub-servers. + +### Fixes ๐Ÿž + +* Ensure objects are copied properly and test mounting fastapi by [@jlowin](https://github.com/jlowin) in [#153](https://github.com/jlowin/fastmcp/pull/153) + +### Docs ๐Ÿ“š + +* Fix broken links in docs by [@jlowin](https://github.com/jlowin) in [#154](https://github.com/jlowin/fastmcp/pull/154) + +### Other Changes ๐Ÿฆพ + +* Update README.md by [@jlowin](https://github.com/jlowin) in [#149](https://github.com/jlowin/fastmcp/pull/149) +* Only apply log config to FastMCP loggers by [@jlowin](https://github.com/jlowin) in [#155](https://github.com/jlowin/fastmcp/pull/155) +* Update pyproject.toml by [@jlowin](https://github.com/jlowin) in [#156](https://github.com/jlowin/fastmcp/pull/156) + +**Full Changelog**: [v2.1.1...v2.1.2](https://github.com/jlowin/fastmcp/compare/v2.1.1...v2.1.2) + + + + +## [v2.1.1: Doc Holiday](https://github.com/jlowin/fastmcp/releases/tag/v2.1.1) + +FastMCP's docs are now available at gofastmcp.com. + +### Docs ๐Ÿ“š + +* Add docs by [@jlowin](https://github.com/jlowin) in [#136](https://github.com/jlowin/fastmcp/pull/136) +* Add docs link to readme by [@jlowin](https://github.com/jlowin) in [#137](https://github.com/jlowin/fastmcp/pull/137) +* Minor docs updates by [@jlowin](https://github.com/jlowin) in [#138](https://github.com/jlowin/fastmcp/pull/138) + +### Fixes ๐Ÿž + +* fix branch name in example by [@zzstoatzz](https://github.com/zzstoatzz) in [#140](https://github.com/jlowin/fastmcp/pull/140) + +### Other Changes ๐Ÿฆพ + +* smart home example by [@zzstoatzz](https://github.com/zzstoatzz) in [#115](https://github.com/jlowin/fastmcp/pull/115) +* Remove mac os tests by [@jlowin](https://github.com/jlowin) in [#142](https://github.com/jlowin/fastmcp/pull/142) +* Expand support for various method interactions by [@jlowin](https://github.com/jlowin) in [#143](https://github.com/jlowin/fastmcp/pull/143) +* Update docs and add\_resource\_fn by [@jlowin](https://github.com/jlowin) in [#144](https://github.com/jlowin/fastmcp/pull/144) +* Update description by [@jlowin](https://github.com/jlowin) in [#145](https://github.com/jlowin/fastmcp/pull/145) +* Support openapi 3.0 and 3.1 by [@jlowin](https://github.com/jlowin) in [#147](https://github.com/jlowin/fastmcp/pull/147) + +**Full Changelog**: [v2.1.0...v2.1.1](https://github.com/jlowin/fastmcp/compare/v2.1.0...v2.1.1) + + + + +## [v2.1.0: Tag, You're It](https://github.com/jlowin/fastmcp/releases/tag/v2.1.0) + +The primary motivation for this release is the fix in #128 for Claude desktop compatibility, but the primary new feature of this release is per-object tags. Currently these are for bookkeeping only but will become useful in future releases. + +### New Features ๐ŸŽ‰ + +* Add tags for all core MCP objects by [@jlowin](https://github.com/jlowin) in [#121](https://github.com/jlowin/fastmcp/pull/121) +* Ensure that openapi tags are transferred to MCP objects by [@jlowin](https://github.com/jlowin) in [#124](https://github.com/jlowin/fastmcp/pull/124) + +### Fixes ๐Ÿž + +* Change default mounted tool separator from / to \_ by [@jlowin](https://github.com/jlowin) in [#128](https://github.com/jlowin/fastmcp/pull/128) +* Enter mounted app lifespans by [@jlowin](https://github.com/jlowin) in [#129](https://github.com/jlowin/fastmcp/pull/129) +* Fix CLI that called mcp instead of fastmcp by [@jlowin](https://github.com/jlowin) in [#128](https://github.com/jlowin/fastmcp/pull/128) + +### Breaking Changes ๐Ÿ›ซ + +* Changed configuration for duplicate resources/tools/prompts by [@jlowin](https://github.com/jlowin) in [#121](https://github.com/jlowin/fastmcp/pull/121) +* Improve client return types by [@jlowin](https://github.com/jlowin) in [#123](https://github.com/jlowin/fastmcp/pull/123) + +### Other Changes ๐Ÿฆพ + +* Add tests for tags in server decorators by [@jlowin](https://github.com/jlowin) in [#122](https://github.com/jlowin/fastmcp/pull/122) +* Clean up server tests by [@jlowin](https://github.com/jlowin) in [#125](https://github.com/jlowin/fastmcp/pull/125) + +**Full Changelog**: [v2.0.0...v2.1.0](https://github.com/jlowin/fastmcp/compare/v2.0.0...v2.1.0) + + + + +## [v2.0.0: Second to None](https://github.com/jlowin/fastmcp/releases/tag/v2.0.0) + +### New Features ๐ŸŽ‰ + +* Support mounting FastMCP instances as sub-MCPs by [@jlowin](https://github.com/jlowin) in [#99](https://github.com/jlowin/fastmcp/pull/99) +* Add in-memory client for calling FastMCP servers (and tests) by [@jlowin](https://github.com/jlowin) in [#100](https://github.com/jlowin/fastmcp/pull/100) +* Add MCP proxy server by [@jlowin](https://github.com/jlowin) in [#105](https://github.com/jlowin/fastmcp/pull/105) +* Update FastMCP for upstream changes by [@jlowin](https://github.com/jlowin) in [#107](https://github.com/jlowin/fastmcp/pull/107) +* Generate FastMCP servers from OpenAPI specs and FastAPI by [@jlowin](https://github.com/jlowin) in [#110](https://github.com/jlowin/fastmcp/pull/110) +* Reorganize all client / transports by [@jlowin](https://github.com/jlowin) in [#111](https://github.com/jlowin/fastmcp/pull/111) +* Add sampling and roots by [@jlowin](https://github.com/jlowin) in [#117](https://github.com/jlowin/fastmcp/pull/117) + +### Fixes ๐Ÿž + +* Fix bug with tools that return lists by [@jlowin](https://github.com/jlowin) in [#116](https://github.com/jlowin/fastmcp/pull/116) + +### Other Changes ๐Ÿฆพ + +* Add back FastMCP CLI by [@jlowin](https://github.com/jlowin) in [#108](https://github.com/jlowin/fastmcp/pull/108) +* Update Readme for v2 by [@jlowin](https://github.com/jlowin) in [#112](https://github.com/jlowin/fastmcp/pull/112) +* fix deprecation warnings by [@zzstoatzz](https://github.com/zzstoatzz) in [#113](https://github.com/jlowin/fastmcp/pull/113) +* Readme by [@jlowin](https://github.com/jlowin) in [#118](https://github.com/jlowin/fastmcp/pull/118) +* FastMCP 2.0 by [@jlowin](https://github.com/jlowin) in [#119](https://github.com/jlowin/fastmcp/pull/119) + +**Full Changelog**: [v1.0...v2.0.0](https://github.com/jlowin/fastmcp/compare/v1.0...v2.0.0) + + + + +## [v1.0: It's Official](https://github.com/jlowin/fastmcp/releases/tag/v1.0) + +This release commemorates FastMCP 1.0, which is included in the official Model Context Protocol SDK: + +```python +from mcp.server.fastmcp import FastMCP +``` + +To the best of my knowledge, v1 is identical to the upstream version included with `mcp`. + +### Docs ๐Ÿ“š + +* Update readme to redirect to the official SDK by [@jlowin](https://github.com/jlowin) in [#79](https://github.com/jlowin/fastmcp/pull/79) + +### Other Changes ๐Ÿฆพ + +* fix: use Mount instead of Route for SSE message handling by [@samihamine](https://github.com/samihamine) in [#77](https://github.com/jlowin/fastmcp/pull/77) + +### New Contributors + +* [@samihamine](https://github.com/samihamine) made their first contribution in [#77](https://github.com/jlowin/fastmcp/pull/77) + +**Full Changelog**: [v0.4.1...v1.0](https://github.com/jlowin/fastmcp/compare/v0.4.1...v1.0) + + + + +## [v0.4.1: String Theory](https://github.com/jlowin/fastmcp/releases/tag/v0.4.1) + +### Fixes ๐Ÿž + +* fix: handle strings containing numbers correctly by [@sd2k](https://github.com/sd2k) in [#63](https://github.com/jlowin/fastmcp/pull/63) + +### Docs ๐Ÿ“š + +* patch: Update pyproject.toml license by [@leonkozlowski](https://github.com/leonkozlowski) in [#67](https://github.com/jlowin/fastmcp/pull/67) + +### Other Changes ๐Ÿฆพ + +* Avoid new try\_eval\_type unavailable with older pydantic by [@jurasofish](https://github.com/jurasofish) in [#57](https://github.com/jlowin/fastmcp/pull/57) +* Decorator typing by [@jurasofish](https://github.com/jurasofish) in [#56](https://github.com/jlowin/fastmcp/pull/56) + +### New Contributors + +* [@leonkozlowski](https://github.com/leonkozlowski) made their first contribution in [#67](https://github.com/jlowin/fastmcp/pull/67) + +**Full Changelog**: [v0.4.0...v0.4.1](https://github.com/jlowin/fastmcp/compare/v0.4.0...v0.4.1) + + + + +## [v0.4.0: Nice to MIT You](https://github.com/jlowin/fastmcp/releases/tag/v0.4.0) + +This is a relatively small release in terms of features, but the version is bumped to 0.4 to reflect that the code is being relicensed from Apache 2.0 to MIT. This is to facilitate FastMCP's inclusion in the official MCP SDK. + +### New Features ๐ŸŽ‰ + +* Add pyright + tests by [@jlowin](https://github.com/jlowin) in [#52](https://github.com/jlowin/fastmcp/pull/52) +* add pgvector memory example by [@zzstoatzz](https://github.com/zzstoatzz) in [#49](https://github.com/jlowin/fastmcp/pull/49) + +### Fixes ๐Ÿž + +* fix: use stderr for logging by [@sd2k](https://github.com/sd2k) in [#51](https://github.com/jlowin/fastmcp/pull/51) + +### Docs ๐Ÿ“š + +* Update ai-labeler.yml by [@jlowin](https://github.com/jlowin) in [#48](https://github.com/jlowin/fastmcp/pull/48) +* Relicense from Apache 2.0 to MIT by [@jlowin](https://github.com/jlowin) in [#54](https://github.com/jlowin/fastmcp/pull/54) + +### Other Changes ๐Ÿฆพ + +* fix warning and flake by [@zzstoatzz](https://github.com/zzstoatzz) in [#47](https://github.com/jlowin/fastmcp/pull/47) + +### New Contributors + +* [@sd2k](https://github.com/sd2k) made their first contribution in [#51](https://github.com/jlowin/fastmcp/pull/51) + +**Full Changelog**: [v0.3.5...v0.4.0](https://github.com/jlowin/fastmcp/compare/v0.3.5...v0.4.0) + + + + +## [v0.3.5: Windows of Opportunity](https://github.com/jlowin/fastmcp/releases/tag/v0.3.5) + +This release is highlighted by the ability to handle complex JSON objects as MCP inputs and improved Windows compatibility. + +### New Features ๐ŸŽ‰ + +* Set up multiple os tests by [@jlowin](https://github.com/jlowin) in [#44](https://github.com/jlowin/fastmcp/pull/44) +* Changes to accommodate windows users. by [@justjoehere](https://github.com/justjoehere) in [#42](https://github.com/jlowin/fastmcp/pull/42) +* Handle complex inputs by [@jurasofish](https://github.com/jurasofish) in [#31](https://github.com/jlowin/fastmcp/pull/31) + +### Docs ๐Ÿ“š + +* Make AI labeler more conservative by [@jlowin](https://github.com/jlowin) in [#46](https://github.com/jlowin/fastmcp/pull/46) + +### Other Changes ๐Ÿฆพ + +* Additional Windows Fixes for Dev running and for importing modules in a server by [@justjoehere](https://github.com/justjoehere) in [#43](https://github.com/jlowin/fastmcp/pull/43) + +### New Contributors + +* [@justjoehere](https://github.com/justjoehere) made their first contribution in [#42](https://github.com/jlowin/fastmcp/pull/42) +* [@jurasofish](https://github.com/jurasofish) made their first contribution in [#31](https://github.com/jlowin/fastmcp/pull/31) + +**Full Changelog**: [v0.3.4...v0.3.5](https://github.com/jlowin/fastmcp/compare/v0.3.4...v0.3.5) + + + + +## [v0.3.4: URL's Well That Ends Well](https://github.com/jlowin/fastmcp/releases/tag/v0.3.4) + +### Fixes ๐Ÿž + +* Handle missing config file when installing by [@jlowin](https://github.com/jlowin) in [#37](https://github.com/jlowin/fastmcp/pull/37) +* Remove BaseURL reference and use AnyURL by [@jlowin](https://github.com/jlowin) in [#40](https://github.com/jlowin/fastmcp/pull/40) + +**Full Changelog**: [v0.3.3...v0.3.4](https://github.com/jlowin/fastmcp/compare/v0.3.3...v0.3.4) + + + + +## [v0.3.3: Dependence Day](https://github.com/jlowin/fastmcp/releases/tag/v0.3.3) + +### New Features ๐ŸŽ‰ + +* Surge example by [@zzstoatzz](https://github.com/zzstoatzz) in [#29](https://github.com/jlowin/fastmcp/pull/29) +* Support Python dependencies in Server by [@jlowin](https://github.com/jlowin) in [#34](https://github.com/jlowin/fastmcp/pull/34) + +### Docs ๐Ÿ“š + +* add `Contributing` section to README by [@zzstoatzz](https://github.com/zzstoatzz) in [#32](https://github.com/jlowin/fastmcp/pull/32) + +**Full Changelog**: [v0.3.2...v0.3.3](https://github.com/jlowin/fastmcp/compare/v0.3.2...v0.3.3) + + + + +## [v0.3.2: Green with ENVy](https://github.com/jlowin/fastmcp/releases/tag/v0.3.2) + +### New Features ๐ŸŽ‰ + +* Support env vars when installing by [@jlowin](https://github.com/jlowin) in [#27](https://github.com/jlowin/fastmcp/pull/27) + +### Docs ๐Ÿ“š + +* Remove top level env var by [@jlowin](https://github.com/jlowin) in [#28](https://github.com/jlowin/fastmcp/pull/28) + +**Full Changelog**: [v0.3.1...v0.3.2](https://github.com/jlowin/fastmcp/compare/v0.3.1...v0.3.2) + + + + +## [v0.3.1](https://github.com/jlowin/fastmcp/releases/tag/v0.3.1) + +### New Features ๐ŸŽ‰ + +* Update README.md by [@jlowin](https://github.com/jlowin) in [#23](https://github.com/jlowin/fastmcp/pull/23) +* add rich handler and dotenv loading for settings by [@zzstoatzz](https://github.com/zzstoatzz) in [#22](https://github.com/jlowin/fastmcp/pull/22) +* print exception when server can't start by [@jlowin](https://github.com/jlowin) in [#25](https://github.com/jlowin/fastmcp/pull/25) + +### Docs ๐Ÿ“š + +* Update README.md by [@jlowin](https://github.com/jlowin) in [#24](https://github.com/jlowin/fastmcp/pull/24) + +### Other Changes ๐Ÿฆพ + +* Remove log by [@jlowin](https://github.com/jlowin) in [#26](https://github.com/jlowin/fastmcp/pull/26) + +**Full Changelog**: [v0.3.0...v0.3.1](https://github.com/jlowin/fastmcp/compare/v0.3.0...v0.3.1) + + + + +## [v0.3.0: Prompt and Circumstance](https://github.com/jlowin/fastmcp/releases/tag/v0.3.0) + +### New Features ๐ŸŽ‰ + +* Update README by [@jlowin](https://github.com/jlowin) in [#3](https://github.com/jlowin/fastmcp/pull/3) +* Make log levels strings by [@jlowin](https://github.com/jlowin) in [#4](https://github.com/jlowin/fastmcp/pull/4) +* Make content method a function by [@jlowin](https://github.com/jlowin) in [#5](https://github.com/jlowin/fastmcp/pull/5) +* Add template support by [@jlowin](https://github.com/jlowin) in [#6](https://github.com/jlowin/fastmcp/pull/6) +* Refactor resources module by [@jlowin](https://github.com/jlowin) in [#7](https://github.com/jlowin/fastmcp/pull/7) +* Clean up cli imports by [@jlowin](https://github.com/jlowin) in [#8](https://github.com/jlowin/fastmcp/pull/8) +* Prepare to list templates by [@jlowin](https://github.com/jlowin) in [#11](https://github.com/jlowin/fastmcp/pull/11) +* Move image to separate module by [@jlowin](https://github.com/jlowin) in [#9](https://github.com/jlowin/fastmcp/pull/9) +* Add support for request context, progress, logging, etc. by [@jlowin](https://github.com/jlowin) in [#12](https://github.com/jlowin/fastmcp/pull/12) +* Add context tests and better runtime loads by [@jlowin](https://github.com/jlowin) in [#13](https://github.com/jlowin/fastmcp/pull/13) +* Refactor tools + resourcemanager by [@jlowin](https://github.com/jlowin) in [#14](https://github.com/jlowin/fastmcp/pull/14) +* func โ†’ fn everywhere by [@jlowin](https://github.com/jlowin) in [#15](https://github.com/jlowin/fastmcp/pull/15) +* Add support for prompts by [@jlowin](https://github.com/jlowin) in [#16](https://github.com/jlowin/fastmcp/pull/16) +* Create LICENSE by [@jlowin](https://github.com/jlowin) in [#18](https://github.com/jlowin/fastmcp/pull/18) +* Update cli file spec by [@jlowin](https://github.com/jlowin) in [#19](https://github.com/jlowin/fastmcp/pull/19) +* Update readmeUpdate README by [@jlowin](https://github.com/jlowin) in [#20](https://github.com/jlowin/fastmcp/pull/20) +* Use hatchling for version by [@jlowin](https://github.com/jlowin) in [#21](https://github.com/jlowin/fastmcp/pull/21) + +### Other Changes ๐Ÿฆพ + +* Add echo server by [@jlowin](https://github.com/jlowin) in [#1](https://github.com/jlowin/fastmcp/pull/1) +* Add github workflows by [@jlowin](https://github.com/jlowin) in [#2](https://github.com/jlowin/fastmcp/pull/2) +* typing updates by [@zzstoatzz](https://github.com/zzstoatzz) in [#17](https://github.com/jlowin/fastmcp/pull/17) + +### New Contributors + +* [@jlowin](https://github.com/jlowin) made their first contribution in [#1](https://github.com/jlowin/fastmcp/pull/1) +* [@zzstoatzz](https://github.com/zzstoatzz) made their first contribution in [#17](https://github.com/jlowin/fastmcp/pull/17) + +**Full Changelog**: [v0.2.0...v0.3.0](https://github.com/jlowin/fastmcp/compare/v0.2.0...v0.3.0) + + + + +## [v0.2.0](https://github.com/jlowin/fastmcp/releases/tag/v0.2.0) + +**Full Changelog**: [v0.1.0...v0.2.0](https://github.com/jlowin/fastmcp/compare/v0.1.0...v0.2.0) + + + + +## [v0.1.0](https://github.com/jlowin/fastmcp/releases/tag/v0.1.0) + +The very first release of FastMCP! ๐ŸŽ‰ + +**Full Changelog**: [Initial commits](https://github.com/jlowin/fastmcp/commits/v0.1.0) + \ No newline at end of file diff --git a/docs/clients/auth/bearer.mdx b/docs/clients/auth/bearer.mdx new file mode 100644 index 000000000..0a5b57902 --- /dev/null +++ b/docs/clients/auth/bearer.mdx @@ -0,0 +1,88 @@ +--- +title: Bearer Token Authentication +sidebarTitle: Bearer Auth +description: Authenticate your FastMCP client with a Bearer token. +icon: key +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + + + + +Bearer Token authentication is only relevant for HTTP-based transports. + + +You can configure your FastMCP client to use **bearer authentication** by supplying a valid access token. This is most appropriate for service accounts, long-lived API keys, CI/CD, applications where authentication is managed separately, or other non-interactive authentication methods. + +A Bearer token is a JSON Web Token (JWT) that is used to authenticate a request. It is most commonly used in the `Authorization` header of an HTTP request, using the `Bearer` scheme: + +```http +Authorization: Bearer +``` + + +## Client Usage + +The most straightforward way to use a pre-existing Bearer token is to provide it as a string to the `auth` parameter of the `fastmcp.Client` or transport instance. FastMCP will automatically format it correctly for the `Authorization` header and bearer scheme. + + +If you're using a string token, do not include the `Bearer` prefix. FastMCP will add it for you. + + +```python {5} +from fastmcp import Client + +async with Client( + "https://fastmcp.cloud/mcp", + auth="", +) as client: + await client.ping() +``` + +You can also supply a Bearer token to a transport instance, such as `StreamableHttpTransport` or `SSETransport`: + +```python {6} +from fastmcp import Client +from fastmcp.client.transports import StreamableHttpTransport + +transport = StreamableHttpTransport( + "http://fastmcp.cloud/mcp", + auth="", +) + +async with Client(transport) as client: + await client.ping() +``` + +## `BearerAuth` Helper + +If you prefer to be more explicit and not rely on FastMCP to transform your string token, you can use the `BearerAuth` class yourself, which implements the `httpx.Auth` interface. + +```python {6} +from fastmcp import Client +from fastmcp.client.auth import BearerAuth + +async with Client( + "https://fastmcp.cloud/mcp", + auth=BearerAuth(token=""), +) as client: + await client.ping() +``` + +## Custom Headers + +If the MCP server expects a custom header or token scheme, you can manually set the client's `headers` instead of using the `auth` parameter by setting them on your transport: + +```python {5} +from fastmcp import Client +from fastmcp.client.transports import StreamableHttpTransport + +async with Client( + transport=StreamableHttpTransport( + "https://fastmcp.cloud/mcp", + headers={"X-API-Key": ""}, + ), +) as client: + await client.ping() +``` diff --git a/docs/clients/auth/oauth.mdx b/docs/clients/auth/oauth.mdx new file mode 100644 index 000000000..56e02e0b4 --- /dev/null +++ b/docs/clients/auth/oauth.mdx @@ -0,0 +1,118 @@ +--- +title: OAuth Authentication +sidebarTitle: OAuth +description: Authenticate your FastMCP client via OAuth 2.1. +icon: window +tag: NEW +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + + + + +OAuth authentication is only relevant for HTTP-based transports and requires user interaction via a web browser. + + +When your FastMCP client needs to access an MCP server protected by OAuth 2.1, and the process requires user interaction (like logging in and granting consent), you should use the Authorization Code Flow. FastMCP provides the `fastmcp.client.auth.OAuth` helper to simplify this entire process. + +This flow is common for user-facing applications where the application acts on behalf of the user. + +## Client Usage + + +### Default Configuration + +The simplest way to use OAuth is to pass the string `"oauth"` to the `auth` parameter of the `Client` or transport instance. FastMCP will automatically configure the client to use OAuth with default settings: + +```python {4} +from fastmcp import Client + +# Uses default OAuth settings +async with Client("https://fastmcp.cloud/mcp", auth="oauth") as client: + await client.ping() +``` + + +### `OAuth` Helper + +To fully configure the OAuth flow, use the `OAuth` helper and pass it to the `auth` parameter of the `Client` or transport instance. `OAuth` manages the complexities of the OAuth 2.1 Authorization Code Grant with PKCE (Proof Key for Code Exchange) for enhanced security, and implements the full `httpx.Auth` interface. + +```python {2, 4, 6} +from fastmcp import Client +from fastmcp.client.auth import OAuth + +oauth = OAuth(mcp_url="https://fastmcp.cloud/mcp") + +async with Client("https://fastmcp.cloud/mcp", auth=oauth) as client: + await client.ping() +``` + +#### `OAuth` Parameters + +- **`mcp_url`** (`str`): The full URL of the target MCP server endpoint. Used to discover OAuth server metadata +- **`scopes`** (`str | list[str]`, optional): OAuth scopes to request. Can be space-separated string or list of strings +- **`client_name`** (`str`, optional): Client name for dynamic registration. Defaults to `"FastMCP Client"` +- **`token_storage_cache_dir`** (`Path`, optional): Token cache directory. Defaults to `~/.fastmcp/oauth-mcp-client-cache/` +- **`additional_client_metadata`** (`dict[str, Any]`, optional): Extra metadata for client registration +- **`callback_port`** (`int`, optional): Fixed port for OAuth callback server. If not specified, uses a random available port + + +## OAuth Flow + +The OAuth flow is triggered when you use a FastMCP `Client` configured to use OAuth. + + + +The client first checks the `token_storage_cache_dir` for existing, valid tokens for the target server. If one is found, it will be used to authenticate the client. + + +If no valid tokens exist, the client attempts to discover the OAuth server's endpoints using a well-known URI (e.g., `/.well-known/oauth-authorization-server`) based on the `mcp_url`. + + +If the OAuth server supports it and the client isn't already registered (or credentials aren't cached), the client performs dynamic client registration according to RFC 7591. + + +A temporary local HTTP server is started on an available port (or the port specified via `callback_port`). This server's address (e.g., `http://127.0.0.1:/callback`) acts as the `redirect_uri` for the OAuth flow. + + +The user's default web browser is automatically opened, directing them to the OAuth server's authorization endpoint. The user logs in and grants (or denies) the requested `scopes`. + + +Upon approval, the OAuth server redirects the user's browser to the local callback server with an `authorization_code`. The client captures this code and exchanges it with the OAuth server's token endpoint for an `access_token` (and often a `refresh_token`) using PKCE for security. + + +The obtained tokens are saved to the `token_storage_cache_dir` for future use, eliminating the need for repeated browser interactions. + + +The access token is automatically included in the `Authorization` header for requests to the MCP server. + + +If the access token expires, the client will automatically use the refresh token to get a new access token. + + + +## Token Management + +### Token Storage + +OAuth access tokens are automatically cached in `~/.fastmcp/oauth-mcp-client-cache/` and persist between application runs. Files are keyed by the OAuth server's base URL. + +### Managing Cache + +To clear the tokens for a specific server, instantiate a `FileTokenStorage` instance and call the `clear` method: + +```python +from fastmcp.client.auth.oauth import FileTokenStorage + +storage = FileTokenStorage(server_url="https://fastmcp.cloud/mcp") +await storage.clear() +``` + +To clear *all* tokens for all servers, call the `clear_all` method on the `FileTokenStorage` class: + +```python +from fastmcp.client.auth.oauth import FileTokenStorage + +FileTokenStorage.clear_all() +``` diff --git a/docs/clients/client.mdx b/docs/clients/client.mdx new file mode 100644 index 000000000..0f5dc0f1e --- /dev/null +++ b/docs/clients/client.mdx @@ -0,0 +1,291 @@ +--- +title: The FastMCP Client +sidebarTitle: Overview +description: Programmatic client for interacting with MCP servers through a well-typed, Pythonic interface. +icon: user-robot +--- + +import { VersionBadge } from '/snippets/version-badge.mdx' + + + +The central piece of MCP client applications is the `fastmcp.Client` class. This class provides a **programmatic interface** for interacting with any Model Context Protocol (MCP) server, handling protocol details and connection management automatically. + +The FastMCP Client is designed for deterministic, controlled interactions rather than autonomous behavior, making it ideal for: + +- **Testing MCP servers** during development +- **Building deterministic applications** that need reliable MCP interactions +- **Creating the foundation for agentic or LLM-based clients** with structured, type-safe operations + +All client operations require using the `async with` context manager for proper connection lifecycle management. + + + +This is not an agentic client - it requires explicit function calls and provides direct control over all MCP operations. Use it as a building block for higher-level systems. + + +## Creating a Client + +Creating a client is straightforward. You provide a server source and the client automatically infers the appropriate transport mechanism. + +```python +import asyncio +from fastmcp import Client, FastMCP + +# In-memory server (ideal for testing) +server = FastMCP("TestServer") +client = Client(server) + +# HTTP server +client = Client("https://example.com/mcp") + +# Local Python script +client = Client("my_mcp_server.py") + +async def main(): + async with client: + # Basic server interaction + await client.ping() + + # List available operations + tools = await client.list_tools() + resources = await client.list_resources() + prompts = await client.list_prompts() + + # Execute operations + result = await client.call_tool("example_tool", {"param": "value"}) + print(result) + +asyncio.run(main()) +``` + +## Client-Transport Architecture + +The FastMCP Client separates concerns between protocol and connection: + +- **`Client`**: Handles MCP protocol operations (tools, resources, prompts) and manages callbacks +- **`Transport`**: Establishes and maintains the connection (WebSockets, HTTP, Stdio, in-memory) + +### Transport Inference + +The client automatically infers the appropriate transport based on the input: + +1. **`FastMCP` instance** โ†’ In-memory transport (perfect for testing) +2. **File path ending in `.py`** โ†’ Python Stdio transport +3. **File path ending in `.js`** โ†’ Node.js Stdio transport +4. **URL starting with `http://` or `https://`** โ†’ HTTP transport +5. **`MCPConfig` dictionary** โ†’ Multi-server client + +```python +from fastmcp import Client, FastMCP + +# Examples of transport inference +client_memory = Client(FastMCP("TestServer")) +client_script = Client("./server.py") +client_http = Client("https://api.example.com/mcp") +``` + + +For testing and development, always prefer the in-memory transport by passing a `FastMCP` server directly to the client. This eliminates network complexity and separate processes. + + +## Configuration-Based Clients + + + +Create clients from MCP configuration dictionaries, which can include multiple servers. While there is no official standard for MCP configuration format, FastMCP follows established conventions used by tools like Claude Desktop. + +### Configuration Format + +```python +config = { + "mcpServers": { + "server_name": { + # Remote HTTP/SSE server + "transport": "http", # or "sse" + "url": "https://api.example.com/mcp", + "headers": {"Authorization": "Bearer token"}, + "auth": "oauth" # or bearer token string + }, + "local_server": { + # Local stdio server + "transport": "stdio", + "command": "python", + "args": ["./server.py", "--verbose"], + "env": {"DEBUG": "true"}, + "cwd": "/path/to/server", + } + } +} +``` + +### Multi-Server Example + +```python +config = { + "mcpServers": { + "weather": {"url": "https://weather-api.example.com/mcp"}, + "assistant": {"command": "python", "args": ["./assistant_server.py"]} + } +} + +client = Client(config) + +async with client: + # Tools are prefixed with server names + weather_data = await client.call_tool("weather_get_forecast", {"city": "London"}) + response = await client.call_tool("assistant_answer_question", {"question": "What's the capital of France?"}) + + # Resources use prefixed URIs + icons = await client.read_resource("weather://weather/icons/sunny") + templates = await client.read_resource("resource://assistant/templates/list") +``` + +## Connection Lifecycle + +The client operates asynchronously and uses context managers for connection management: + +```python +async def example(): + client = Client("my_mcp_server.py") + + # Connection established here + async with client: + print(f"Connected: {client.is_connected()}") + + # Make multiple calls within the same session + tools = await client.list_tools() + result = await client.call_tool("greet", {"name": "World"}) + + # Connection closed automatically here + print(f"Connected: {client.is_connected()}") +``` + +## Operations + +FastMCP clients can interact with several types of server components: + +### Tools + +Tools are server-side functions that the client can execute with arguments. + +```python +async with client: + # List available tools + tools = await client.list_tools() + + # Execute a tool + result = await client.call_tool("multiply", {"a": 5, "b": 3}) + print(result.data) # 15 +``` + +See [Tools](/clients/tools) for detailed documentation. + +### Resources + +Resources are data sources that the client can read, either static or templated. + +```python +async with client: + # List available resources + resources = await client.list_resources() + + # Read a resource + content = await client.read_resource("file:///config/settings.json") + print(content[0].text) +``` + +See [Resources](/clients/resources) for detailed documentation. + +### Prompts + +Prompts are reusable message templates that can accept arguments. + +```python +async with client: + # List available prompts + prompts = await client.list_prompts() + + # Get a rendered prompt + messages = await client.get_prompt("analyze_data", {"data": [1, 2, 3]}) + print(messages.messages) +``` + +See [Prompts](/clients/prompts) for detailed documentation. + +### Server Connectivity + +Use `ping()` to verify the server is reachable: + +```python +async with client: + await client.ping() + print("Server is reachable") +``` + +## Client Configuration + +Clients can be configured with additional handlers and settings for specialized use cases. + +### Callback Handlers + +The client supports several callback handlers for advanced server interactions: + +```python +from fastmcp import Client +from fastmcp.client.logging import LogMessage + +async def log_handler(message: LogMessage): + print(f"Server log: {message.data}") + +async def progress_handler(progress: float, total: float | None, message: str | None): + print(f"Progress: {progress}/{total} - {message}") + +async def sampling_handler(messages, params, context): + # Integrate with your LLM service here + return "Generated response" + +client = Client( + "my_mcp_server.py", + log_handler=log_handler, + progress_handler=progress_handler, + sampling_handler=sampling_handler, + timeout=30.0 +) +``` + +The `Client` constructor accepts several configuration options: + +- `transport`: Transport instance or source for automatic inference +- `log_handler`: Handle server log messages +- `progress_handler`: Monitor long-running operations +- `sampling_handler`: Respond to server LLM requests +- `roots`: Provide local context to servers +- `timeout`: Default timeout for requests (in seconds) + +### Transport Configuration + +For detailed transport configuration (headers, authentication, environment variables), see the [Transports](/clients/transports) documentation. + +## Next Steps + +Explore the detailed documentation for each operation type: + +### Core Operations +- **[Tools](/clients/tools)** - Execute server-side functions and handle results +- **[Resources](/clients/resources)** - Access static and templated resources +- **[Prompts](/clients/prompts)** - Work with message templates and argument serialization + +### Advanced Features +- **[Logging](/clients/logging)** - Handle server log messages +- **[Progress](/clients/progress)** - Monitor long-running operations +- **[Sampling](/clients/sampling)** - Respond to server LLM requests +- **[Roots](/clients/roots)** - Provide local context to servers + +### Connection Details +- **[Transports](/clients/transports)** - Configure connection methods and parameters +- **[Authentication](/clients/auth/oauth)** - Set up OAuth and bearer token authentication + + +The FastMCP Client is designed as a foundational tool. Use it directly for deterministic operations, or build higher-level agentic systems on top of its reliable, type-safe interface. + \ No newline at end of file diff --git a/docs/clients/elicitation.mdx b/docs/clients/elicitation.mdx new file mode 100644 index 000000000..c11009822 --- /dev/null +++ b/docs/clients/elicitation.mdx @@ -0,0 +1,126 @@ +--- +title: User Elicitation +sidebarTitle: Elicitation +description: Handle server-initiated user input requests with structured schemas. +icon: message-question +tag: NEW +--- + +import { VersionBadge } from "/snippets/version-badge.mdx"; + + + +## What is Elicitation? + +Elicitation allows MCP servers to request structured input from users during tool execution. Instead of requiring all inputs upfront, servers can interactively ask users for information as needed - like prompting for missing parameters, requesting clarification, or gathering additional context. + +For example, a file management tool might ask "Which directory should I create?" or a data analysis tool might request "What date range should I analyze?" + +## How FastMCP Makes Elicitation Easy + +FastMCP's client provides a helpful abstraction layer that: + +- **Converts JSON schemas to Python types**: The raw MCP protocol uses JSON schemas, but FastMCP automatically converts these to Python dataclasses +- **Provides structured constructors**: Instead of manually building dictionaries that match the schema, you get dataclass constructors that ensure correct structure +- **Handles type conversion**: FastMCP takes care of converting between JSON representations and Python objects +- **Runtime introspection**: You can inspect the generated dataclass fields to understand the expected structure + +When you implement an elicitation handler, FastMCP gives you a dataclass type that matches the server's schema, making it easy to create properly structured responses without having to manually parse JSON schemas. + +## Elicitation Handler + +Provide an `elicitation_handler` function when creating the client. FastMCP automatically converts the server's JSON schema into a Python dataclass type, making it easy to construct the response: + +```python +from fastmcp import Client +from fastmcp.client.elicitation import ElicitResult + +async def elicitation_handler(message: str, response_type: type, params, context): + # Present the message to the user and collect input + user_input = input(f"{message}: ") + + # Create response using the provided dataclass type + # FastMCP converted the JSON schema to this Python type for you + response_data = response_type(value=user_input) + + # You can return data directly - FastMCP will implicitly accept the elicitation + return response_data + + # Or explicitly return an ElicitResult for more control + # return ElicitResult(action="accept", content=response_data) + +client = Client( + "my_mcp_server.py", + elicitation_handler=elicitation_handler, +) +``` + +### Handler Parameters + +The elicitation handler receives four parameters: + + + + The prompt message to display to the user + + + + A Python dataclass type that FastMCP created from the server's JSON schema. Use this to construct your response with proper typing and IDE support. If the server requests an empty object (indicating no response), this will be `None`. + + + + The original MCP elicitation request parameters, including the raw JSON schema in `params.requestedSchema` if you need it + + + + Request context containing metadata about the elicitation request + + + +### Response Actions + +The handler can return data directly (which implicitly accepts the elicitation) or an `ElicitResult` object for more control over the response action: + + + + How the user responded to the elicitation request + + + + The user's input data (required for "accept", omitted for "decline"/"cancel") + + + +**Action Types:** +- **`accept`**: User provided valid input - include their data in the `content` field +- **`decline`**: User chose not to provide the requested information - omit `content` +- **`cancel`**: User cancelled the entire operation - omit `content` + +## Basic Example + +```python +from fastmcp import Client +from fastmcp.client.elicitation import ElicitResult + +async def basic_elicitation_handler(message: str, response_type: type, params, context): + print(f"Server asks: {message}") + + # Simple text input for demonstration + user_response = input("Your response: ") + + if not user_response: + # For non-acceptance, use ElicitResult explicitly + return ElicitResult(action="decline") + + # Use the response_type dataclass to create a properly structured response + # FastMCP handles the conversion from JSON schema to Python type + # Return data directly - FastMCP will implicitly accept the elicitation + return response_type(value=user_response) + +client = Client( + "my_mcp_server.py", + elicitation_handler=basic_elicitation_handler +) +``` + + diff --git a/docs/clients/logging.mdx b/docs/clients/logging.mdx new file mode 100644 index 000000000..ffebdf87f --- /dev/null +++ b/docs/clients/logging.mdx @@ -0,0 +1,111 @@ +--- +title: Server Logging +sidebarTitle: Logging +description: Receive and handle log messages from MCP servers. +icon: receipt +--- + +import { VersionBadge } from '/snippets/version-badge.mdx' + + + +MCP servers can emit log messages to clients. The client can handle these logs through a log handler callback. + +## Log Handler + +Provide a `log_handler` function when creating the client. For robust logging, the log messages can be integrated with Python's standard `logging` module. + +```python +import logging +from fastmcp import Client +from fastmcp.client.logging import LogMessage + +# In a real app, you might configure this in your main entry point +logging.basicConfig( + level=logging.INFO, + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' +) + +# Get a logger for the module where the client is used +logger = logging.getLogger(__name__) + +# This mapping is useful for converting MCP level strings to Python's levels +LOGGING_LEVEL_MAP = logging.getLevelNamesMapping() + +async def log_handler(message: LogMessage): + """ + Handles incoming logs from the MCP server and forwards them + to the standard Python logging system. + """ + msg = message.data.get('msg') + extra = message.data.get('extra') + + # Convert the MCP log level to a Python log level + level = LOGGING_LEVEL_MAP.get(message.level.upper(), logging.INFO) + + # Log the message using the standard logging library + logger.log(level, msg, extra=extra) + + +client = Client( + "my_mcp_server.py", + log_handler=log_handler, +) +``` + +## Handling Structured Logs + +The `message.data` attribute is a dictionary that contains the log payload from the server. This enables structured logging, allowing you to receive rich, contextual information. + +The dictionary contains two keys: +- `msg`: The string log message. +- `extra`: A dictionary containing any extra data sent from the server. + +This structure is preserved even when logs are forwarded through a FastMCP proxy, making it a powerful tool for debugging complex, multi-server applications. + +### Handler Parameters + +The `log_handler` is called every time a log message is received. It receives a `LogMessage` object: + + + + + + The log level + + + + The logger name (optional, may be None) + + + + The log payload, containing `msg` and `extra` keys. + + + + + +```python +async def detailed_log_handler(message: LogMessage): + msg = message.data.get('msg') + extra = message.data.get('extra') + + if message.level == "error": + print(f"ERROR: {msg} | Details: {extra}") + elif message.level == "warning": + print(f"WARNING: {msg} | Details: {extra}") + else: + print(f"{message.level.upper()}: {msg}") +``` + +## Default Log Handling + +If you don't provide a custom `log_handler`, FastMCP's default handler routes server logs to the appropriate Python logging levels. The MCP levels are mapped as follows: `notice` โ†’ INFO; `alert` and `emergency` โ†’ CRITICAL. If the server includes a logger name, it is prefixed in the message, and any `extra` data is forwarded via the logging `extra` parameter. + +```python +client = Client("my_mcp_server.py") + +async with client: + # Server logs are forwarded at their proper severity (DEBUG/INFO/WARNING/ERROR/CRITICAL) + await client.call_tool("some_tool") +``` diff --git a/docs/clients/messages.mdx b/docs/clients/messages.mdx new file mode 100644 index 000000000..bad7150d1 --- /dev/null +++ b/docs/clients/messages.mdx @@ -0,0 +1,129 @@ +--- +title: Message Handling +sidebarTitle: Messages +description: Handle MCP messages, requests, and notifications with custom message handlers. +icon: envelope +--- + +import { VersionBadge } from "/snippets/version-badge.mdx"; + + + +MCP clients can receive various types of messages from servers, including requests that need responses and notifications that don't. The message handler provides a unified way to process all these messages. + +## Function-Based Handler + +The simplest way to handle messages is with a function that receives all messages: + +```python +from fastmcp import Client + +async def message_handler(message): + """Handle all MCP messages from the server.""" + if hasattr(message, 'root'): + method = message.root.method + print(f"Received: {method}") + + # Handle specific notifications + if method == "notifications/tools/list_changed": + print("Tools have changed - might want to refresh tool cache") + elif method == "notifications/resources/list_changed": + print("Resources have changed") + +client = Client( + "my_mcp_server.py", + message_handler=message_handler, +) +``` + +## Message Handler Class + +For fine-grained targeting, FastMCP provides a `MessageHandler` class you can subclass to take advantage of specific hooks: + +```python +from fastmcp import Client +from fastmcp.client.messages import MessageHandler +import mcp.types + +class MyMessageHandler(MessageHandler): + async def on_tool_list_changed( + self, notification: mcp.types.ToolListChangedNotification + ) -> None: + """Handle tool list changes specifically.""" + print("Tool list changed - refreshing available tools") + +client = Client( + "my_mcp_server.py", + message_handler=MyMessageHandler(), +) +``` + +### Available Handler Methods + +All handler methods receive a single argument - the specific message type: + + + + Called for ALL messages (requests and notifications) + + + + Called for requests that expect responses + + + + Called for notifications (fire-and-forget) + + + + Called when the server's tool list changes + + + + Called when the server's resource list changes + + + + Called when the server's prompt list changes + + + + Called for progress updates during long-running operations + + + + Called for log messages from the server + + + +## Example: Handling Tool Changes + +Here's a practical example of handling tool list changes: + +```python +from fastmcp.client.messages import MessageHandler +import mcp.types + +class ToolCacheHandler(MessageHandler): + def __init__(self): + self.cached_tools = [] + + async def on_tool_list_changed( + self, notification: mcp.types.ToolListChangedNotification + ) -> None: + """Clear tool cache when tools change.""" + print("Tools changed - clearing cache") + self.cached_tools = [] # Force refresh on next access + +client = Client("server.py", message_handler=ToolCacheHandler()) +``` + +## Handling Requests + +While the message handler receives server-initiated requests, for most use cases you should use the dedicated callback parameters instead: + +- **Sampling requests**: Use [`sampling_handler`](/clients/sampling) +- **Progress requests**: Use [`progress_handler`](/clients/progress) +- **Log requests**: Use [`log_handler`](/clients/logging) + +The message handler is primarily for monitoring and handling notifications rather than responding to requests. \ No newline at end of file diff --git a/docs/clients/progress.mdx b/docs/clients/progress.mdx new file mode 100644 index 000000000..ff3e0aa85 --- /dev/null +++ b/docs/clients/progress.mdx @@ -0,0 +1,70 @@ +--- +title: Progress Monitoring +sidebarTitle: Progress +description: Handle progress notifications from long-running server operations. +icon: bars-progress +--- + +import { VersionBadge } from '/snippets/version-badge.mdx' + + + +MCP servers can report progress during long-running operations. The client can receive these updates through a progress handler. + +## Progress Handler + +Set a progress handler when creating the client: + +```python +from fastmcp import Client + +async def my_progress_handler( + progress: float, + total: float | None, + message: str | None +) -> None: + if total is not None: + percentage = (progress / total) * 100 + print(f"Progress: {percentage:.1f}% - {message or ''}") + else: + print(f"Progress: {progress} - {message or ''}") + +client = Client( + "my_mcp_server.py", + progress_handler=my_progress_handler +) +``` + +### Handler Parameters + +The progress handler receives three parameters: + + + + + Current progress value + + + + Expected total value (may be None) + + + + Optional status message (may be None) + + + + +## Per-Call Progress Handler + +Override the progress handler for specific tool calls: + +```python +async with client: + # Override with specific progress handler for this call + result = await client.call_tool( + "long_running_task", + {"param": "value"}, + progress_handler=my_progress_handler + ) +``` diff --git a/docs/clients/prompts.mdx b/docs/clients/prompts.mdx new file mode 100644 index 000000000..a6d6e155b --- /dev/null +++ b/docs/clients/prompts.mdx @@ -0,0 +1,216 @@ +--- +title: Prompts +sidebarTitle: Prompts +description: Use server-side prompt templates with automatic argument serialization. +icon: message-lines +--- + +import { VersionBadge } from '/snippets/version-badge.mdx' + + + +Prompts are reusable message templates exposed by MCP servers. They can accept arguments to generate personalized message sequences for LLM interactions. + +## Listing Prompts + +Use `list_prompts()` to retrieve all available prompt templates: + +```python +async with client: + prompts = await client.list_prompts() + # prompts -> list[mcp.types.Prompt] + + for prompt in prompts: + print(f"Prompt: {prompt.name}") + print(f"Description: {prompt.description}") + if prompt.arguments: + print(f"Arguments: {[arg.name for arg in prompt.arguments]}") + # Access tags and other metadata + if hasattr(prompt, '_meta') and prompt._meta: + fastmcp_meta = prompt._meta.get('_fastmcp', {}) + print(f"Tags: {fastmcp_meta.get('tags', [])}") +``` + +### Filtering by Tags + + + +You can use the `meta` field to filter prompts based on their tags: + +```python +async with client: + prompts = await client.list_prompts() + + # Filter prompts by tag + analysis_prompts = [ + prompt for prompt in prompts + if hasattr(prompt, '_meta') and prompt._meta and + prompt._meta.get('_fastmcp', {}) and + 'analysis' in prompt._meta.get('_fastmcp', {}).get('tags', []) + ] + + print(f"Found {len(analysis_prompts)} analysis prompts") +``` + + +The `_meta` field is part of the standard MCP specification. FastMCP servers include tags and other metadata within a `_fastmcp` namespace (e.g., `_meta._fastmcp.tags`) to avoid conflicts with user-defined metadata. This behavior can be controlled with the server's `include_fastmcp_meta` setting - when disabled, the `_fastmcp` namespace won't be included. Other MCP server implementations may not provide this metadata structure. + + +## Using Prompts + +### Basic Usage + +Request a rendered prompt using `get_prompt()` with the prompt name and arguments: + +```python +async with client: + # Simple prompt without arguments + result = await client.get_prompt("welcome_message") + # result -> mcp.types.GetPromptResult + + # Access the generated messages + for message in result.messages: + print(f"Role: {message.role}") + print(f"Content: {message.content}") +``` + +### Prompts with Arguments + +Pass arguments as a dictionary to customize the prompt: + +```python +async with client: + # Prompt with simple arguments + result = await client.get_prompt("user_greeting", { + "name": "Alice", + "role": "administrator" + }) + + # Access the personalized messages + for message in result.messages: + print(f"Generated message: {message.content}") +``` + +## Automatic Argument Serialization + + + +FastMCP automatically serializes complex arguments to JSON strings as required by the MCP specification. This allows you to pass typed objects directly: + +```python +from dataclasses import dataclass + +@dataclass +class UserData: + name: str + age: int + +async with client: + # Complex arguments are automatically serialized + result = await client.get_prompt("analyze_user", { + "user": UserData(name="Alice", age=30), # Automatically serialized to JSON + "preferences": {"theme": "dark"}, # Dict serialized to JSON string + "scores": [85, 92, 78], # List serialized to JSON string + "simple_name": "Bob" # Strings passed through unchanged + }) +``` + +The client handles serialization using `pydantic_core.to_json()` for consistent formatting. FastMCP servers can automatically deserialize these JSON strings back to the expected types. + +### Serialization Examples + +```python +async with client: + result = await client.get_prompt("data_analysis", { + # These will be automatically serialized to JSON strings: + "config": { + "format": "csv", + "include_headers": True, + "delimiter": "," + }, + "filters": [ + {"field": "age", "operator": ">", "value": 18}, + {"field": "status", "operator": "==", "value": "active"} + ], + # This remains a string: + "report_title": "Monthly Analytics Report" + }) +``` + +## Working with Prompt Results + +The `get_prompt()` method returns a `GetPromptResult` object containing a list of messages: + +```python +async with client: + result = await client.get_prompt("conversation_starter", {"topic": "climate"}) + + # Access individual messages + for i, message in enumerate(result.messages): + print(f"Message {i + 1}:") + print(f" Role: {message.role}") + print(f" Content: {message.content.text if hasattr(message.content, 'text') else message.content}") +``` + +## Raw MCP Protocol Access + +For access to the complete MCP protocol objects, use the `*_mcp` methods: + +```python +async with client: + # Raw MCP method returns full protocol object + prompts_result = await client.list_prompts_mcp() + # prompts_result -> mcp.types.ListPromptsResult + + prompt_result = await client.get_prompt_mcp("example_prompt", {"arg": "value"}) + # prompt_result -> mcp.types.GetPromptResult +``` + +## Multi-Server Clients + +When using multi-server clients, prompts are accessible without prefixing (unlike tools): + +```python +async with client: # Multi-server client + # Prompts from any server are directly accessible + result1 = await client.get_prompt("weather_prompt", {"city": "London"}) + result2 = await client.get_prompt("assistant_prompt", {"query": "help"}) +``` + +## Common Prompt Patterns + +### System Messages + +Many prompts generate system messages for LLM configuration: + +```python +async with client: + result = await client.get_prompt("system_configuration", { + "role": "helpful assistant", + "expertise": "python programming" + }) + + # Typically returns messages with role="system" + system_message = result.messages[0] + print(f"System prompt: {system_message.content}") +``` + +### Conversation Templates + +Prompts can generate multi-turn conversation templates: + +```python +async with client: + result = await client.get_prompt("interview_template", { + "candidate_name": "Alice", + "position": "Senior Developer" + }) + + # Multiple messages for a conversation flow + for message in result.messages: + print(f"{message.role}: {message.content}") +``` + + +Prompt arguments and their expected types depend on the specific prompt implementation. Check the server's documentation or use `list_prompts()` to see available arguments for each prompt. + \ No newline at end of file diff --git a/docs/clients/resources.mdx b/docs/clients/resources.mdx new file mode 100644 index 000000000..b5120fdf1 --- /dev/null +++ b/docs/clients/resources.mdx @@ -0,0 +1,204 @@ +--- +title: Resource Operations +sidebarTitle: Resources +description: Access static and templated resources from MCP servers. +icon: folder-open +--- + +import { VersionBadge } from '/snippets/version-badge.mdx' + + + +Resources are data sources exposed by MCP servers. They can be static files or dynamic templates that generate content based on parameters. + +## Types of Resources + +MCP servers expose two types of resources: + +- **Static Resources**: Fixed content accessible via URI (e.g., configuration files, documentation) +- **Resource Templates**: Dynamic resources that accept parameters to generate content (e.g., API endpoints, database queries) + +## Listing Resources + +### Static Resources + +Use `list_resources()` to retrieve all static resources available on the server: + +```python +async with client: + resources = await client.list_resources() + # resources -> list[mcp.types.Resource] + + for resource in resources: + print(f"Resource URI: {resource.uri}") + print(f"Name: {resource.name}") + print(f"Description: {resource.description}") + print(f"MIME Type: {resource.mimeType}") + # Access tags and other metadata + if hasattr(resource, '_meta') and resource._meta: + fastmcp_meta = resource._meta.get('_fastmcp', {}) + print(f"Tags: {fastmcp_meta.get('tags', [])}") +``` + +### Resource Templates + +Use `list_resource_templates()` to retrieve available resource templates: + +```python +async with client: + templates = await client.list_resource_templates() + # templates -> list[mcp.types.ResourceTemplate] + + for template in templates: + print(f"Template URI: {template.uriTemplate}") + print(f"Name: {template.name}") + print(f"Description: {template.description}") + # Access tags and other metadata + if hasattr(template, '_meta') and template._meta: + fastmcp_meta = template._meta.get('_fastmcp', {}) + print(f"Tags: {fastmcp_meta.get('tags', [])}") +``` + +### Filtering by Tags + + + +You can use the `meta` field to filter resources based on their tags: + +```python +async with client: + resources = await client.list_resources() + + # Filter resources by tag + config_resources = [ + resource for resource in resources + if hasattr(resource, '_meta') and resource._meta and + resource._meta.get('_fastmcp', {}) and + 'config' in resource._meta.get('_fastmcp', {}).get('tags', []) + ] + + print(f"Found {len(config_resources)} config resources") +``` + + +The `_meta` field is part of the standard MCP specification. FastMCP servers include tags and other metadata within a `_fastmcp` namespace (e.g., `_meta._fastmcp.tags`) to avoid conflicts with user-defined metadata. This behavior can be controlled with the server's `include_fastmcp_meta` setting - when disabled, the `_fastmcp` namespace won't be included. Other MCP server implementations may not provide this metadata structure. + + +## Reading Resources + +### Static Resources + +Read a static resource using its URI: + +```python +async with client: + # Read a static resource + content = await client.read_resource("file:///path/to/README.md") + # content -> list[mcp.types.TextResourceContents | mcp.types.BlobResourceContents] + + # Access text content + if hasattr(content[0], 'text'): + print(content[0].text) + + # Access binary content + if hasattr(content[0], 'blob'): + print(f"Binary data: {len(content[0].blob)} bytes") +``` + +### Resource Templates + +Read from a resource template by providing the URI with parameters: + +```python +async with client: + # Read a resource generated from a template + # For example, a template like "weather://{{city}}/current" + weather_content = await client.read_resource("weather://london/current") + + # Access the generated content + print(weather_content[0].text) # Assuming text JSON response +``` + +## Content Types + +Resources can return different content types: + +### Text Resources + +```python +async with client: + content = await client.read_resource("resource://config/settings.json") + + for item in content: + if hasattr(item, 'text'): + print(f"Text content: {item.text}") + print(f"MIME type: {item.mimeType}") +``` + +### Binary Resources + +```python +async with client: + content = await client.read_resource("resource://images/logo.png") + + for item in content: + if hasattr(item, 'blob'): + print(f"Binary content: {len(item.blob)} bytes") + print(f"MIME type: {item.mimeType}") + + # Save to file + with open("downloaded_logo.png", "wb") as f: + f.write(item.blob) +``` + +## Working with Multi-Server Clients + +When using multi-server clients, resource URIs are automatically prefixed with the server name: + +```python +async with client: # Multi-server client + # Access resources from different servers + weather_icons = await client.read_resource("weather://weather/icons/sunny") + templates = await client.read_resource("resource://assistant/templates/list") + + print(f"Weather icon: {weather_icons[0].blob}") + print(f"Templates: {templates[0].text}") +``` + +## Raw MCP Protocol Access + +For access to the complete MCP protocol objects, use the `*_mcp` methods: + +```python +async with client: + # Raw MCP methods return full protocol objects + resources_result = await client.list_resources_mcp() + # resources_result -> mcp.types.ListResourcesResult + + templates_result = await client.list_resource_templates_mcp() + # templates_result -> mcp.types.ListResourceTemplatesResult + + content_result = await client.read_resource_mcp("resource://example") + # content_result -> mcp.types.ReadResourceResult +``` + +## Common Resource URI Patterns + +Different MCP servers may use various URI schemes: + +```python +# File system resources +"file:///path/to/file.txt" + +# Custom protocol resources +"weather://london/current" +"database://users/123" + +# Generic resource protocol +"resource://config/settings" +"resource://templates/email" +``` + + +Resource URIs and their formats depend on the specific MCP server implementation. Check the server's documentation for available resources and their URI patterns. + \ No newline at end of file diff --git a/docs/clients/roots.mdx b/docs/clients/roots.mdx new file mode 100644 index 000000000..2a8d8c1f9 --- /dev/null +++ b/docs/clients/roots.mdx @@ -0,0 +1,42 @@ +--- +title: Client Roots +sidebarTitle: Roots +description: Provide local context and resource boundaries to MCP servers. +icon: folder-tree +--- + +import { VersionBadge } from '/snippets/version-badge.mdx' + + + +Roots are a way for clients to inform servers about the resources they have access to. Servers can use this information to adjust behavior or provide more relevant responses. + +## Setting Static Roots + +Provide a list of roots when creating the client: + + +```python Static Roots +from fastmcp import Client + +client = Client( + "my_mcp_server.py", + roots=["/path/to/root1", "/path/to/root2"] +) +``` + +```python Dynamic Roots Callback +from fastmcp import Client +from fastmcp.client.roots import RequestContext + +async def roots_callback(context: RequestContext) -> list[str]: + print(f"Server requested roots (Request ID: {context.request_id})") + return ["/path/to/root1", "/path/to/root2"] + +client = Client( + "my_mcp_server.py", + roots=roots_callback +) +``` + + diff --git a/docs/clients/sampling.mdx b/docs/clients/sampling.mdx new file mode 100644 index 000000000..08011c208 --- /dev/null +++ b/docs/clients/sampling.mdx @@ -0,0 +1,201 @@ +--- +title: LLM Sampling +sidebarTitle: Sampling +description: Handle server-initiated LLM sampling requests. +icon: robot +--- + +import { VersionBadge } from "/snippets/version-badge.mdx"; + + + +MCP servers can request LLM completions from clients. The client handles these requests through a sampling handler callback. + +## Sampling Handler + +Provide a `sampling_handler` function when creating the client: + +```python +from fastmcp import Client +from fastmcp.client.sampling import ( + SamplingMessage, + SamplingParams, + RequestContext, +) + +async def sampling_handler( + messages: list[SamplingMessage], + params: SamplingParams, + context: RequestContext +) -> str: + # Your LLM integration logic here + # Extract text from messages and generate a response + return "Generated response based on the messages" + +client = Client( + "my_mcp_server.py", + sampling_handler=sampling_handler, +) +``` + +### Handler Parameters + +The sampling handler receives three parameters: + + + + + + The role of the message. + + + + The content of the message. + + TextContent is most common, and has a `.text` attribute. + + + + + + + + The messages to sample from + + + + The server's preferences for which model to select. The client MAY ignore + these preferences. + + + The hints to use for model selection. + + + + The cost priority for model selection. + + + + The speed priority for model selection. + + + + The intelligence priority for model selection. + + + + + + An optional system prompt the server wants to use for sampling. + + + + A request to include context from one or more MCP servers (including the caller), to + be attached to the prompt. + + + + The sampling temperature. + + + + The maximum number of tokens to sample. + + + + The stop sequences to use for sampling. + + + + Optional metadata to pass through to the LLM provider. + + + + + + + + Unique identifier for the MCP request + + + + + +## Basic Example + +```python +from fastmcp import Client +from fastmcp.client.sampling import SamplingMessage, SamplingParams, RequestContext + +async def basic_sampling_handler( + messages: list[SamplingMessage], + params: SamplingParams, + context: RequestContext +) -> str: + # Extract message content + conversation = [] + for message in messages: + content = message.content.text if hasattr(message.content, 'text') else str(message.content) + conversation.append(f"{message.role}: {content}") + + # Use the system prompt if provided + system_prompt = params.systemPrompt or "You are a helpful assistant." + + # Here you would integrate with your preferred LLM service + # This is just a placeholder response + return f"Response based on conversation: {' | '.join(conversation)}" + +client = Client( + "my_mcp_server.py", + sampling_handler=basic_sampling_handler +) +``` + +## Sampling fallback + +Client support for sampling is optional, if the client does not support sampling, the server will report an error indicating +that the client does not support sampling. + +A `sampling_handler` can also be provided to the FastMCP server, which will be used to handle sampling requests if the client +does not support sampling. This sampling handler bypasses the client and sends sampling requests directly to the LLM provider. + +Sampling handlers can be implemented using any LLM provider but a sample implementation for OpenAI is provided as a Contrib +module. Sampling lacks the full capabilities of typical LLM completions. For this reason, the OpenAI sampling handler, pointed at +a third-party provider's OpenAI-compatible API, is often sufficient to implement a sampling handler. + +```python +import asyncio +import os + +from mcp.types import ContentBlock +from openai import OpenAI + +from fastmcp import FastMCP +from fastmcp.experimental.sampling.handlers.openai import OpenAISamplingHandler +from fastmcp.server.context import Context + + +async def async_main(): + server = FastMCP( + name="OpenAI Sampling Fallback Example", + sampling_handler=OpenAISamplingHandler( + default_model="gpt-4o-mini", + client=OpenAI( + api_key=os.getenv("API_KEY"), + base_url=os.getenv("BASE_URL"), + ), + ), + ) + + @server.tool + async def test_sample_fallback(ctx: Context) -> ContentBlock: + return await ctx.sample( + messages=["hello world!"], + ) + + await server.run_http_async() + + +if __name__ == "__main__": + asyncio.run(async_main()) +``` \ No newline at end of file diff --git a/docs/clients/tools.mdx b/docs/clients/tools.mdx new file mode 100644 index 000000000..58d0c0d28 --- /dev/null +++ b/docs/clients/tools.mdx @@ -0,0 +1,270 @@ +--- +title: Tool Operations +sidebarTitle: Tools +description: Discover and execute server-side tools with the FastMCP client. +icon: wrench +--- + +import { VersionBadge } from '/snippets/version-badge.mdx' + + + +Tools are executable functions exposed by MCP servers. The FastMCP client provides methods to discover available tools and execute them with arguments. + +## Discovering Tools + +Use `list_tools()` to retrieve all tools available on the server: + +```python +async with client: + tools = await client.list_tools() + # tools -> list[mcp.types.Tool] + + for tool in tools: + print(f"Tool: {tool.name}") + print(f"Description: {tool.description}") + if tool.inputSchema: + print(f"Parameters: {tool.inputSchema}") + # Access tags and other metadata + if hasattr(tool, 'meta') and tool.meta: + fastmcp_meta = tool.meta.get('_fastmcp', {}) + print(f"Tags: {fastmcp_meta.get('tags', [])}") +``` + +### Filtering by Tags + + + +You can use the `meta` field to filter tools based on their tags: + +```python +async with client: + tools = await client.list_tools() + + # Filter tools by tag + analysis_tools = [ + tool for tool in tools + if hasattr(tool, 'meta') and tool.meta and + tool.meta.get('_fastmcp', {}) and + 'analysis' in tool.meta.get('_fastmcp', {}).get('tags', []) + ] + + print(f"Found {len(analysis_tools)} analysis tools") +``` + + +The `meta` field is part of the standard MCP specification. FastMCP servers include tags and other metadata within a `_fastmcp` namespace (e.g., `meta._fastmcp.tags`) to avoid conflicts with user-defined metadata. This behavior can be controlled with the server's `include_fastmcp_meta` setting - when disabled, the `_fastmcp` namespace won't be included. Other MCP server implementations may not provide this metadata structure. + + +## Executing Tools + +### Basic Execution + +Execute a tool using `call_tool()` with the tool name and arguments: + +```python +async with client: + # Simple tool call + result = await client.call_tool("add", {"a": 5, "b": 3}) + # result -> CallToolResult with structured and unstructured data + + # Access structured data (automatically deserialized) + print(result.data) # 8 (int) or {"result": 8} for primitive types + + # Access traditional content blocks + print(result.content[0].text) # "8" (TextContent) +``` + +### Advanced Execution Options + +The `call_tool()` method supports additional parameters for timeout control and progress monitoring: + +```python +async with client: + # With timeout (aborts if execution takes longer than 2 seconds) + result = await client.call_tool( + "long_running_task", + {"param": "value"}, + timeout=2.0 + ) + + # With progress handler (to track execution progress) + result = await client.call_tool( + "long_running_task", + {"param": "value"}, + progress_handler=my_progress_handler + ) +``` + +**Parameters:** +- `name`: The tool name (string) +- `arguments`: Dictionary of arguments to pass to the tool (optional) +- `timeout`: Maximum execution time in seconds (optional, overrides client-level timeout) +- `progress_handler`: Progress callback function (optional, overrides client-level handler) + +## Handling Results + + + +Tool execution returns a `CallToolResult` object with both structured and traditional content. FastMCP's standout feature is the `.data` property, which doesn't just provide raw JSON but actually hydrates complete Python objects including complex types like datetimes, UUIDs, and custom classes. + +### CallToolResult Properties + + + + **FastMCP exclusive**: Fully hydrated Python objects with complex type support (datetimes, UUIDs, custom classes). Goes beyond JSON to provide complete object reconstruction from output schemas. + + + + Standard MCP content blocks (`TextContent`, `ImageContent`, `AudioContent`, etc.) available from all MCP servers. + + + + Standard MCP structured JSON data as sent by the server, available from all MCP servers that support structured outputs. + + + + Boolean indicating if the tool execution failed. + + + +### Structured Data Access + +FastMCP's `.data` property provides fully hydrated Python objects, not just JSON dictionaries. This includes complex type reconstruction: + +```python +from datetime import datetime +from uuid import UUID + +async with client: + result = await client.call_tool("get_weather", {"city": "London"}) + + # FastMCP reconstructs complete Python objects from the server's output schema + weather = result.data # Server-defined WeatherReport object + print(f"Temperature: {weather.temperature}ยฐC at {weather.timestamp}") + print(f"Station: {weather.station_id}") + print(f"Humidity: {weather.humidity}%") + + # The timestamp is a real datetime object, not a string! + assert isinstance(weather.timestamp, datetime) + assert isinstance(weather.station_id, UUID) + + # Compare with raw structured JSON (standard MCP) + print(f"Raw JSON: {result.structured_content}") + # {"temperature": 20, "timestamp": "2024-01-15T14:30:00Z", "station_id": "123e4567-..."} + + # Traditional content blocks (standard MCP) + print(f"Text content: {result.content[0].text}") +``` + +### Fallback Behavior + +For tools without output schemas or when deserialization fails, `.data` will be `None`: + +```python +async with client: + result = await client.call_tool("legacy_tool", {"param": "value"}) + + if result.data is not None: + # Structured output available and successfully deserialized + print(f"Structured: {result.data}") + else: + # No structured output or deserialization failed - use content blocks + for content in result.content: + if hasattr(content, 'text'): + print(f"Text result: {content.text}") + elif hasattr(content, 'data'): + print(f"Binary data: {len(content.data)} bytes") +``` + +### Primitive Type Unwrapping + + +FastMCP servers automatically wrap non-object results (like `int`, `str`, `bool`) in a `{"result": value}` structure to create valid structured outputs. FastMCP clients understand this convention and automatically unwrap the value in `.data` for convenience, so you get the original primitive value instead of a wrapper object. + + +```python +async with client: + result = await client.call_tool("calculate_sum", {"a": 5, "b": 3}) + + # FastMCP client automatically unwraps for convenience + print(result.data) # 8 (int) - the original value + + # Raw structured content shows the server-side wrapping + print(result.structured_content) # {"result": 8} + + # Other MCP clients would need to manually access ["result"] + # value = result.structured_content["result"] # Not needed with FastMCP! +``` + +## Error Handling + +### Exception-Based Error Handling + +By default, `call_tool()` raises a `ToolError` if the tool execution fails: + +```python +from fastmcp.exceptions import ToolError + +async with client: + try: + result = await client.call_tool("potentially_failing_tool", {"param": "value"}) + print("Tool succeeded:", result.data) + except ToolError as e: + print(f"Tool failed: {e}") +``` + +### Manual Error Checking + +You can disable automatic error raising and manually check the result: + +```python +async with client: + result = await client.call_tool( + "potentially_failing_tool", + {"param": "value"}, + raise_on_error=False + ) + + if result.is_error: + print(f"Tool failed: {result.content[0].text}") + else: + print(f"Tool succeeded: {result.data}") +``` + +### Raw MCP Protocol Access + +For complete control, use `call_tool_mcp()` which returns the raw MCP protocol object: + +```python +async with client: + result = await client.call_tool_mcp("potentially_failing_tool", {"param": "value"}) + # result -> mcp.types.CallToolResult + + if result.isError: + print(f"Tool failed: {result.content}") + else: + print(f"Tool succeeded: {result.content}") + # Note: No automatic deserialization with call_tool_mcp() +``` + +## Argument Handling + +Arguments are passed as a dictionary to the tool: + +```python +async with client: + # Simple arguments + result = await client.call_tool("greet", {"name": "World"}) + + # Complex arguments + result = await client.call_tool("process_data", { + "config": {"format": "json", "validate": True}, + "items": [1, 2, 3, 4, 5], + "metadata": {"source": "api", "version": "1.0"} + }) +``` + + +For multi-server clients, tool names are automatically prefixed with the server name (e.g., `weather_get_forecast` for a tool named `get_forecast` on the `weather` server). + \ No newline at end of file diff --git a/docs/clients/transports.mdx b/docs/clients/transports.mdx new file mode 100644 index 000000000..b7129acc2 --- /dev/null +++ b/docs/clients/transports.mdx @@ -0,0 +1,383 @@ +--- +title: Client Transports +sidebarTitle: Transports +description: Configure how FastMCP Clients connect to and communicate with servers. +icon: link +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + + + +The FastMCP `Client` communicates with MCP servers through transport objects that handle the underlying connection mechanics. While the client can automatically select a transport based on what you pass to it, instantiating transports explicitly gives you full control over configurationโ€”environment variables, authentication, session management, and more. + +Think of transports as configurable adapters between your client code and MCP servers. Each transport type handles a different communication pattern: subprocesses with pipes, HTTP connections, or direct in-memory calls. + +## Choosing the Right Transport + +- **Use [STDIO Transport](#stdio-transport)** when you need to run local MCP servers with full control over their environment and lifecycle +- **Use [Remote Transports](#remote-transports)** when connecting to production services or shared MCP servers running independently +- **Use [In-Memory Transport](#in-memory-transport)** for testing FastMCP servers without subprocess or network overhead +- **Use [MCP JSON Configuration](#mcp-json-configuration-transport)** when you need to connect to multiple servers defined in configuration files + +## STDIO Transport + +STDIO (Standard Input/Output) transport communicates with MCP servers through subprocess pipes. This is the standard mechanism used by desktop clients like Claude Desktop and is the primary way to run local MCP servers. + +### The Client Runs the Server + + +**Critical Concept**: When using STDIO transport, your client actually launches and manages the server process. This is fundamentally different from network transports where you connect to an already-running server. Understanding this relationship is key to using STDIO effectively. + + +With STDIO transport, your client: +- Starts the server as a subprocess when you connect +- Manages the server's lifecycle (start, stop, restart) +- Controls the server's environment and configuration +- Communicates through stdin/stdout pipes + +This architecture enables powerful local integrations but requires understanding environment isolation and process management. + +### Environment Isolation + +STDIO servers run in isolated environments by default. This is a security feature enforced by the MCP protocol to prevent accidental exposure of sensitive data. + +When your client launches an MCP server: +- The server does NOT inherit your shell's environment variables +- API keys, paths, and other configuration must be explicitly passed +- The working directory and system paths may differ from your shell + +To pass environment variables to your server, use the `env` parameter: + +```python +from fastmcp import Client + +# If your server needs environment variables (like API keys), +# you must explicitly pass them: +client = Client( + "my_server.py", + env={"API_KEY": "secret", "DEBUG": "true"} +) + +# This won't work - the server runs in isolation: +# export API_KEY="secret" # in your shell +# client = Client("my_server.py") # server can't see API_KEY +``` + +### Basic Usage + +To use STDIO transport, you create a transport instance with the command and arguments needed to run your server: + +```python +from fastmcp.client.transports import StdioTransport + +transport = StdioTransport( + command="python", + args=["my_server.py"] +) +client = Client(transport) +``` + +You can configure additional settings like environment variables, working directory, or command arguments: + +```python +transport = StdioTransport( + command="python", + args=["my_server.py", "--verbose"], + env={"LOG_LEVEL": "DEBUG"}, + cwd="/path/to/server" +) +client = Client(transport) +``` + +For convenience, the client can also infer STDIO transport from file paths, but this doesn't allow configuration: + +```python +from fastmcp import Client + +client = Client("my_server.py") # Limited - no configuration options +``` + +### Environment Variables + +Since STDIO servers don't inherit your environment, you need strategies for passing configuration. Here are two common approaches: + +**Selective forwarding** passes only the variables your server actually needs: + +```python +import os +from fastmcp.client.transports import StdioTransport + +required_vars = ["API_KEY", "DATABASE_URL", "REDIS_HOST"] +env = { + var: os.environ[var] + for var in required_vars + if var in os.environ +} + +transport = StdioTransport( + command="python", + args=["server.py"], + env=env +) +client = Client(transport) +``` + +**Loading from .env files** keeps configuration separate from code: + +```python +from dotenv import dotenv_values +from fastmcp.client.transports import StdioTransport + +env = dotenv_values(".env") +transport = StdioTransport( + command="python", + args=["server.py"], + env=env +) +client = Client(transport) +``` + +### Session Persistence + +STDIO transports maintain sessions across multiple client contexts by default (`keep_alive=True`). This improves performance by reusing the same subprocess for multiple connections, but can be controlled when you need isolation. + +By default, the subprocess persists between connections: + +```python +from fastmcp.client.transports import StdioTransport + +transport = StdioTransport( + command="python", + args=["server.py"] +) +client = Client(transport) + +async def efficient_multiple_operations(): + async with client: + await client.ping() + + async with client: # Reuses the same subprocess + await client.call_tool("process_data", {"file": "data.csv"}) +``` + +For complete isolation between connections, disable session persistence: + +```python +transport = StdioTransport( + command="python", + args=["server.py"], + keep_alive=False +) +client = Client(transport) +``` + +Use `keep_alive=False` when you need complete isolation (e.g., in test suites) or when server state could cause issues between connections. + +### Specialized STDIO Transports + +FastMCP provides convenience transports that are thin wrappers around `StdioTransport` with pre-configured commands: + +- **`PythonStdioTransport`** - Uses `python` command for `.py` files +- **`NodeStdioTransport`** - Uses `node` command for `.js` files +- **`UvStdioTransport`** - Uses `uv` for Python packages (uses `env_vars` parameter) +- **`UvxStdioTransport`** - Uses `uvx` for Python packages (uses `env_vars` parameter) +- **`NpxStdioTransport`** - Uses `npx` for Node packages (uses `env_vars` parameter) + +For most use cases, instantiate `StdioTransport` directly with your desired command. These specialized transports are primarily useful for client inference shortcuts. + +## Remote Transports + +Remote transports connect to MCP servers running as web services. This is a fundamentally different model from STDIO transportsโ€”instead of your client launching and managing a server process, you connect to an already-running service that manages its own environment and lifecycle. + +### Streamable HTTP Transport + + + +Streamable HTTP is the recommended transport for production deployments, providing efficient bidirectional streaming over HTTP connections. + +- **Class:** `StreamableHttpTransport` +- **Server compatibility:** FastMCP servers running with `mcp run --transport http` + +The transport requires a URL and optionally supports custom headers for authentication and configuration: + +```python +from fastmcp.client.transports import StreamableHttpTransport + +# Basic connection +transport = StreamableHttpTransport(url="https://api.example.com/mcp") +client = Client(transport) + +# With custom headers for authentication +transport = StreamableHttpTransport( + url="https://api.example.com/mcp", + headers={ + "Authorization": "Bearer your-token-here", + "X-Custom-Header": "value" + } +) +client = Client(transport) +``` + +For convenience, FastMCP also provides authentication helpers: + +```python +from fastmcp.client.auth import BearerAuth + +client = Client( + "https://api.example.com/mcp", + auth=BearerAuth("your-token-here") +) +``` + +### SSE Transport (Legacy) + +Server-Sent Events transport is maintained for backward compatibility but is superseded by Streamable HTTP for new deployments. + +- **Class:** `SSETransport` +- **Server compatibility:** FastMCP servers running with `mcp run --transport sse` + +SSE transport supports the same configuration options as Streamable HTTP: + +```python +from fastmcp.client.transports import SSETransport + +transport = SSETransport( + url="https://api.example.com/sse", + headers={"Authorization": "Bearer token"} +) +client = Client(transport) +``` + +Use Streamable HTTP for new deployments unless you have specific infrastructure requirements for SSE. + +## In-Memory Transport + +In-memory transport connects directly to a FastMCP server instance within the same Python process. This eliminates both subprocess management and network overhead, making it ideal for testing and development. + +- **Class:** `FastMCPTransport` + + +Unlike STDIO transports, in-memory servers have full access to your Python process's environment. They share the same memory space and environment variables as your client codeโ€”no isolation or explicit environment passing required. + + +```python +from fastmcp import FastMCP, Client +import os + +mcp = FastMCP("TestServer") + +@mcp.tool +def greet(name: str) -> str: + prefix = os.environ.get("GREETING_PREFIX", "Hello") + return f"{prefix}, {name}!" + +client = Client(mcp) + +async with client: + result = await client.call_tool("greet", {"name": "World"}) +``` + +## MCP JSON Configuration Transport + + + +This transport supports the emerging MCP JSON configuration standard for defining multiple servers: + +- **Class:** `MCPConfigTransport` + +```python +config = { + "mcpServers": { + "weather": { + "url": "https://weather.example.com/mcp", + "transport": "http" + }, + "assistant": { + "command": "python", + "args": ["./assistant.py"], + "env": {"LOG_LEVEL": "INFO"} + } + } +} + +client = Client(config) + +async with client: + # Tools are namespaced by server + weather = await client.call_tool("weather_get_forecast", {"city": "NYC"}) + answer = await client.call_tool("assistant_ask", {"question": "What?"}) +``` + +### Tool Transformation with FastMCP and MCPConfig + +FastMCP supports basic tool transformations to be defined alongside the MCP Servers in the MCPConfig file. + +```python +config = { + "mcpServers": { + "weather": { + "url": "https://weather.example.com/mcp", + "transport": "http", + "tools": { } # <--- This is the tool transformation section + } + } +} +``` + +With these transformations, you can transform (change) the name, title, description, tags, enablement, and arguments of a tool. + +For each argument the tool takes, you can transform (change) the name, description, default, visibility, whether it's required, and you can provide example values. + +In the following example, we're transforming the `weather_get_forecast` tool to only retrieve the weather for `Miami` and hiding the `city` argument from the client. + +```python +tool_transformations = { + "weather_get_forecast": { + "name": "miami_weather", + "description": "Get the weather for Miami", + "arguments": { + "city": { + "name": "city", + "default": "Miami", + "hide": True, + } + } + } +} + +config = { + "mcpServers": { + "weather": { + "url": "https://weather.example.com/mcp", + "transport": "http", + "tools": tool_transformations + } + } +} +``` + +#### Allowlisting and Blocklisting Tools + +Tools can be allowlisted or blocklisted from the client by applying `tags` to the tools on the server. In the following example, we're allowlisting only tools marked with the `forecast` tag, all other tools will be unavailable to the client. + +```python +tool_transformations = { + "weather_get_forecast": { + "enabled": True, + "tags": ["forecast"] + } +} + + +config = { + "mcpServers": { + "weather": { + "url": "https://weather.example.com/mcp", + "transport": "http", + "tools": tool_transformations, + "include_tags": ["forecast"] + } + } +} +``` \ No newline at end of file diff --git a/docs/community/README.md b/docs/community/README.md new file mode 100644 index 000000000..b61f5cf6d --- /dev/null +++ b/docs/community/README.md @@ -0,0 +1,22 @@ +# Community Section + +This directory contains community-contributed content and showcases for FastMCP. + +## Structure + +- `showcase.mdx` - Main community showcase page featuring high-quality projects and examples + +## Adding Content + +To add new community content: +1. Create a new MDX file in this directory +2. Update `docs.json` to include it in the navigation +3. Follow the existing format for consistency + +## Guidelines + +Community content should: +- Demonstrate best practices +- Provide educational value +- Include proper documentation +- Be maintained and up-to-date \ No newline at end of file diff --git a/docs/community/showcase.mdx b/docs/community/showcase.mdx new file mode 100644 index 000000000..c0a9e3545 --- /dev/null +++ b/docs/community/showcase.mdx @@ -0,0 +1,59 @@ +--- +title: 'Community Showcase' +description: 'High-quality projects and examples from the FastMCP community' +icon: 'users' +--- + +import { YouTubeEmbed } from '/snippets/youtube-embed.mdx' + +## Featured Projects + +Discover exemplary MCP servers and implementations created by our community. These projects demonstrate best practices and innovative uses of FastMCP. + +### Learning Resources + + + A comprehensive educational example demonstrating FastMCP best practices with professional dual-transport server implementation, interactive test client, and detailed documentation. + + +#### Video Tutorials + +**Build Remote MCP Servers w/ Python & FastMCP** - Claude Integrations Tutorial by Greg + Code + + + +**FastMCP โ€” the best way to build an MCP server with Python** - Tutorial by ZazenCodes + + + +**Speedrun a MCP server for Claude Desktop (fastmcp)** - Tutorial by Nate from Prefect + + + +### Community Examples + +Have you built something interesting with FastMCP? We'd love to feature high-quality examples here! Start a [discussion on GitHub](https://github.com/jlowin/fastmcp/discussions) to share your project. + +## Contributing + +To get your project featured: + +1. Ensure your project demonstrates best practices +2. Include comprehensive documentation +3. Add clear usage examples +4. Open a discussion in our [GitHub Discussions](https://github.com/jlowin/fastmcp/discussions) + +We review submissions regularly and feature projects that provide value to the FastMCP community. + +## Further Reading + +- [Contrib Modules](/patterns/contrib) - Community-contributed modules that are distributed with FastMCP itself \ No newline at end of file diff --git a/docs/css/banner.css b/docs/css/banner.css new file mode 100644 index 000000000..093d9b797 --- /dev/null +++ b/docs/css/banner.css @@ -0,0 +1,67 @@ +/* Banner styling -- improve readability with better contrast */ +#banner { + background: #f1f5f9 !important; + color: #1e293b !important; + font-size: 0.95rem !important; + font-weight: 600 !important; + padding-top: 12px !important; + padding-bottom: 12px !important; + position: relative !important; + overflow: hidden !important; +} + +#banner::before { + content: ""; + position: absolute; + top: 0; + left: 0; + width: 100%; + height: 100%; + background: linear-gradient( + 90deg, + rgba(6, 182, 212, 0.25) 0%, + rgba(6, 182, 212, 0.05) 25%, + rgba(6, 182, 212, 0.35) 50%, + rgba(6, 182, 212, 0.08) 75%, + rgba(6, 182, 212, 0.28) 100% + ); + background-size: 300% 100%; + animation: colorWave 14s ease-in-out infinite alternate; + pointer-events: none; +} + +.dark #banner { + background: #475569 !important; + color: #f1f5f9 !important; +} + +.dark #banner::before { + background: linear-gradient( + 90deg, + rgba(247, 37, 133, 0.35) 0%, + rgba(247, 37, 133, 0.08) 25%, + rgba(247, 37, 133, 0.45) 50%, + rgba(247, 37, 133, 0.12) 75%, + rgba(247, 37, 133, 0.38) 100% + ); + background-size: 300% 100%; +} + +@keyframes colorWave { + 0% { + background-position: 0% 0%; + } + 100% { + background-position: 100% 0%; + } +} + +#banner * { + color: #1e293b !important; + margin: 0 !important; +} + +.dark #banner * { + color: #f1f5f9 !important; +} + diff --git a/docs/css/python-sdk.css b/docs/css/python-sdk.css new file mode 100644 index 000000000..72a64c21a --- /dev/null +++ b/docs/css/python-sdk.css @@ -0,0 +1,3 @@ +a:has(svg.icon) { + border: none !important; +} \ No newline at end of file diff --git a/docs/css/style.css b/docs/css/style.css new file mode 100644 index 000000000..8b36b42cd --- /dev/null +++ b/docs/css/style.css @@ -0,0 +1,20 @@ +img.nav-logo { + max-width: 200px; +} + +/* Code highlighting -- target only inline code elements, not code blocks */ +p code:not(pre code), +table code:not(pre code), +.prose code:not(pre code), +li code:not(pre code), +h1 code:not(pre code), +h2 code:not(pre code), +h3 code:not(pre code), +h4 code:not(pre code), +h5 code:not(pre code), +h6 code:not(pre code) { + color: #f72585 !important; + background-color: rgba(247, 37, 133, 0.09); +} + + diff --git a/docs/css/version-badge.css b/docs/css/version-badge.css new file mode 100644 index 000000000..daff22177 --- /dev/null +++ b/docs/css/version-badge.css @@ -0,0 +1,39 @@ +/* Version badge -- display a badge with the current version of the documentation */ +.version-badge { + display: inline-block; + align-items: center; + gap: 0.3em; + font-size: 1em; + margin-top: 0px; + margin-bottom: 0px; + padding-top: 6px; + padding-bottom: 6px; + padding-left: 20px; + padding-right: 20px; + font-family: "Inter", sans-serif; + color: #ff5400; + background: #fef2f2; + border: 1px solid rgba(220, 38, 38, 0.3); + border-radius: 12px; + box-shadow: none; + vertical-align: middle; + position: relative; + transition: box-shadow 0.2s, transform 0.15s; +} + +.version-badge-container { + margin: 0; + padding: 0; +} + +.version-badge:hover { + box-shadow: 0 2px 8px 0 rgba(160, 132, 252, 0.1); + transform: translateY(-1px) scale(1.03); +} + +.dark .version-badge { + color: #f1f5f9; + background: #334155; + border: 1px solid #64748b; +} + diff --git a/docs/deployment/fastmcp-cloud.mdx b/docs/deployment/fastmcp-cloud.mdx new file mode 100644 index 000000000..f806e1386 --- /dev/null +++ b/docs/deployment/fastmcp-cloud.mdx @@ -0,0 +1,89 @@ +--- +title: FastMCP Cloud +sidebarTitle: FastMCP Cloud +description: The fastest way to deploy your MCP server +icon: cloud +tag: NEW +--- + +[FastMCP Cloud](https://fastmcp.cloud) is a managed platform for hosting MCP servers, built by the FastMCP team. While the FastMCP framework will always be fully open-source, we created FastMCP Cloud to solve the deployment challenges we've seen developers face. Our goal is to provide the absolute fastest way to make your MCP server available to LLM clients like Claude and Cursor. + +FastMCP Cloud is a young product and we welcome your feedback. Please join our [Discord](https://discord.com/invite/aGsSC3yDF4) to share your thoughts and ideas, and you can expect to see new features and improvements every week. + + + +FastMCP Cloud supports both **FastMCP 2.0** servers and also **FastMCP 1.0** servers that were created with the official MCP Python SDK. + + + +FastMCP Cloud is completely free while in beta! + + +## Prerequisites + +To use FastMCP Cloud, you'll need a [GitHub](https://github.com) account. In addition, you'll need a GitHub repo that contains a FastMCP server instance. If you don't want to create one yet, you can proceed to [step 1](#step-1-create-a-project) and use the FastMCP Cloud quickstart repo. + +Your repo can be public or private, but must include at least a Python file that contains a FastMCP server instance. + +To ensure your file is compatible with FastMCP Cloud, you can run `fastmcp inspect ` to see what FastMCP Cloud will see when it runs your server. + + +If you have a `requirements.txt` or `pyproject.toml` in the repo, FastMCP Cloud will automatically detect your server's dependencies and install them for you. Note that your file *can* have an `if __name__ == "__main__"` block, but it will be ignored by FastMCP Cloud. + +For example, a minimal server file might look like: + +```python +from fastmcp import FastMCP + +mcp = FastMCP("MyServer") + +@mcp.tool +def hello(name: str) -> str: + return f"Hello, {name}!" +``` + +## Getting Started + +There are just three steps to deploying a server to FastMCP Cloud: + +### Step 1: Create a Project + +Visit [fastmcp.cloud](https://fastmcp.cloud) and sign in with your GitHub account. Then, create a project. Each project corresponds to a GitHub repo, and you can create one from either your own repo or using the FastMCP Cloud quickstart repo. + +FastMCP Cloud Quickstart Screen + +Next, you'll be prompted to configure your project. + +FastMCP Cloud Configuration Screen + +The configuration screen lets you specify: +- **Name**: The name of your project. This will be used to generate a unique URL for your server. +- **Entrypoint**: The Python file containing your FastMCP server (e.g., `echo.py`). This field has the same syntax as the `fastmcp run` command, for example `echo.py:my_server` to specify a specific object in the file. +- **Authentication**: If disabled, your server is open to the public. If enabled, only other members of your FastMCP Cloud organization will be able to connect. + +Note that FastMCP Cloud will automatically detect yours server's Python dependencies from either a `requirements.txt` or `pyproject.toml` file. + +### Step 2: Deploy Your Server + +Once you configure your project, FastMCP Cloud will: +1. Clone the repository +2. Build your FastMCP server +3. Deploy it to a unique URL +4. Make it immediately available for connections + +FastMCP Cloud Deployment Screen + +FastMCP Cloud will monitor your repo and redeploy your server whenever you push a change to the `main` branch. In addition, FastMCP Cloud will build and deploy servers for every PR your open, hosting them on unique URLs, so you can test changes before updating your production server. + +### Step 3: Connect to Your Server + +Once your server is deployed, it will be accessible at a URL like: + +``` +https://your-project-name.fastmcp.app/mcp +``` + +You should be able to connect to it as soon as you see the deployment succeed! FastMCP Cloud provides instant connection options for popular LLM clients: + +FastMCP Cloud Connection Screen + diff --git a/docs/deployment/running-server.mdx b/docs/deployment/running-server.mdx new file mode 100644 index 000000000..28f1217a7 --- /dev/null +++ b/docs/deployment/running-server.mdx @@ -0,0 +1,258 @@ +--- +title: Running Your Server +sidebarTitle: Running +description: Learn how to run your FastMCP server locally for development and testing +icon: circle-play +--- + +FastMCP servers can be run in different ways depending on your needs. This guide focuses on running servers locally for development and testing. For production deployment to a URL, see the [Self-Hosted Deployment](/deployment/self-hosted) guide. + +## The `run()` Method + +Every FastMCP server needs to be started to accept connections. The simplest way to run a server is by calling the `run()` method on your FastMCP instance. This method starts the server and blocks until it's stopped, handling all the connection management for you. + + +For maximum compatibility, it's best practice to place the `run()` call within an `if __name__ == "__main__":` block. This ensures the server starts only when the script is executed directly, not when imported as a module. + + +```python {9-10} my_server.py +from fastmcp import FastMCP + +mcp = FastMCP(name="MyServer") + +@mcp.tool +def hello(name: str) -> str: + return f"Hello, {name}!" + +if __name__ == "__main__": + mcp.run() +``` + +You can now run this MCP server by executing `python my_server.py`. + +## Transport Protocols + +MCP servers communicate with clients through different transport protocols. Think of transports as the "language" your server speaks to communicate with clients. FastMCP supports three main transport protocols, each designed for specific use cases and deployment scenarios. + +The choice of transport determines how clients connect to your server, what network capabilities are available, and how many clients can connect simultaneously. Understanding these transports helps you choose the right approach for your application. + +### STDIO Transport (Default) + +STDIO (Standard Input/Output) is the default transport for FastMCP servers. When you call `run()` without arguments, your server uses STDIO transport. This transport communicates through standard input and output streams, making it perfect for command-line tools and desktop applications like Claude Desktop. + +With STDIO transport, the client spawns a new server process for each session and manages its lifecycle. The server reads MCP messages from stdin and writes responses to stdout. This is why STDIO servers don't stay running - they're started on-demand by the client. + +```python +from fastmcp import FastMCP + +mcp = FastMCP("MyServer") + +@mcp.tool +def hello(name: str) -> str: + return f"Hello, {name}!" + +if __name__ == "__main__": + mcp.run() # Uses STDIO transport by default +``` + +STDIO is ideal for: +- Local development and testing +- Claude Desktop integration +- Command-line tools +- Single-user applications + +### HTTP Transport (Streamable) + +HTTP transport turns your MCP server into a web service accessible via a URL. This transport uses the Streamable HTTP protocol, which allows clients to connect over the network. Unlike STDIO where each client gets its own process, an HTTP server can handle multiple clients simultaneously. + +The Streamable HTTP protocol provides full bidirectional communication between client and server, supporting all MCP operations including streaming responses. This makes it the recommended choice for network-based deployments. + +To use HTTP transport, specify it in the `run()` method along with networking options: + +```python +from fastmcp import FastMCP + +mcp = FastMCP("MyServer") + +@mcp.tool +def hello(name: str) -> str: + return f"Hello, {name}!" + +if __name__ == "__main__": + # Start an HTTP server on port 8000 + mcp.run(transport="http", host="127.0.0.1", port=8000) +``` + +Your server is now accessible at `http://localhost:8000/mcp/`. This URL is the MCP endpoint that clients will connect to. HTTP transport enables: +- Network accessibility +- Multiple concurrent clients +- Integration with web infrastructure +- Remote deployment capabilities + +For production HTTP deployment with authentication and advanced configuration, see the [Self-Hosted Deployment](/deployment/self-hosted) guide. + +### SSE Transport (Legacy) + +Server-Sent Events (SSE) transport was the original HTTP-based transport for MCP. While still supported for backward compatibility, it has limitations compared to the newer Streamable HTTP transport. SSE only supports server-to-client streaming, making it less efficient for bidirectional communication. + +```python +if __name__ == "__main__": + # SSE transport - use HTTP instead for new projects + mcp.run(transport="sse", host="127.0.0.1", port=8000) +``` + +We recommend using HTTP transport instead of SSE for all new projects. SSE remains available only for compatibility with older clients that haven't upgraded to Streamable HTTP. + +### Choosing the Right Transport + +Each transport serves different needs. STDIO is perfect when you need simple, local execution - it's what Claude Desktop and most command-line tools expect. HTTP transport is essential when you need network access, want to serve multiple clients, or plan to deploy your server remotely. SSE exists only for backward compatibility and shouldn't be used in new projects. + +Consider your deployment scenario: Are you building a tool for local use? STDIO is your best choice. Need a centralized service that multiple clients can access? HTTP transport is the way to go. + +## The FastMCP CLI + +FastMCP provides a powerful command-line interface for running servers without modifying the source code. The CLI can automatically find and run your server with different transports, manage dependencies, and handle development workflows: + +```bash +fastmcp run server.py +``` + +The CLI automatically finds a FastMCP instance in your file (named `mcp`, `server`, or `app`) and runs it with the specified options. This is particularly useful for testing different transports or configurations without changing your code. + +### Dependency Management + +The CLI integrates with `uv` to manage Python environments and dependencies: + +```bash +# Run with a specific Python version +fastmcp run server.py --python 3.11 + +# Run with additional packages +fastmcp run server.py --with pandas --with numpy + +# Run with dependencies from a requirements file +fastmcp run server.py --with-requirements requirements.txt + +# Combine multiple options +fastmcp run server.py --python 3.10 --with httpx --transport http + +# Run within a specific project directory +fastmcp run server.py --project /path/to/project +``` + + +When using `--python`, `--with`, `--project`, or `--with-requirements`, the server runs via `uv run` subprocess instead of using your local environment. + + +### Passing Arguments to Servers + +When servers accept command line arguments (using argparse, click, or other libraries), you can pass them after `--`: + +```bash +fastmcp run config_server.py -- --config config.json +fastmcp run database_server.py -- --database-path /tmp/db.sqlite --debug +``` + +This is useful for servers that need configuration files, database paths, API keys, or other runtime options. + +For more CLI features including development mode with the MCP Inspector, see the [CLI documentation](/patterns/cli). + +### Async Usage + +FastMCP servers are built on async Python, but the framework provides both synchronous and asynchronous APIs to fit your application's needs. The `run()` method we've been using is actually a synchronous wrapper around the async server implementation. + +For applications that are already running in an async context, FastMCP provides the `run_async()` method: + +```python {10-12} +from fastmcp import FastMCP +import asyncio + +mcp = FastMCP(name="MyServer") + +@mcp.tool +def hello(name: str) -> str: + return f"Hello, {name}!" + +async def main(): + # Use run_async() in async contexts + await mcp.run_async(transport="http", port=8000) + +if __name__ == "__main__": + asyncio.run(main()) +``` + + +The `run()` method cannot be called from inside an async function because it creates its own async event loop internally. If you attempt to call `run()` from inside an async function, you'll get an error about the event loop already running. + +Always use `run_async()` inside async functions and `run()` in synchronous contexts. + + +Both `run()` and `run_async()` accept the same transport arguments, so all the examples above apply to both methods. + +## Custom Routes + +When using HTTP transport, you might want to add custom web endpoints alongside your MCP server. This is useful for health checks, status pages, or simple APIs. FastMCP lets you add custom routes using the `@custom_route` decorator: + +```python +from fastmcp import FastMCP +from starlette.requests import Request +from starlette.responses import PlainTextResponse + +mcp = FastMCP("MyServer") + +@mcp.custom_route("/health", methods=["GET"]) +async def health_check(request: Request) -> PlainTextResponse: + return PlainTextResponse("OK") + +@mcp.tool +def process(data: str) -> str: + return f"Processed: {data}" + +if __name__ == "__main__": + mcp.run(transport="http") # Health check at http://localhost:8000/health +``` + +Custom routes are served by the same web server as your MCP endpoint. They're available at the root of your domain while the MCP endpoint is at `/mcp/`. For more complex web applications, consider [mounting your MCP server into a FastAPI or Starlette app](/deployment/self-hosted#integration-with-web-frameworks). + +## Alternative Initialization Patterns + +The `if __name__ == "__main__"` pattern works well for standalone scripts, but some deployment scenarios require different approaches. FastMCP handles these cases automatically. + +### CLI-Only Servers + +When using the FastMCP CLI, you don't need the `if __name__` block at all. The CLI will find your FastMCP instance and run it: + +```python +# server.py +from fastmcp import FastMCP + +mcp = FastMCP("MyServer") # CLI looks for 'mcp', 'server', or 'app' + +@mcp.tool +def process(data: str) -> str: + return f"Processed: {data}" + +# No if __name__ block needed - CLI will find and run 'mcp' +``` + +### ASGI Applications + +For ASGI deployment (running with Uvicorn or similar), you'll want to create an ASGI application object. This approach is common in production deployments where you need more control over the server configuration: + +```python +# app.py +from fastmcp import FastMCP + +def create_app(): + mcp = FastMCP("MyServer") + + @mcp.tool + def process(data: str) -> str: + return f"Processed: {data}" + + return mcp.http_app() + +app = create_app() # Uvicorn will use this +``` + +See the [Self-Hosted Deployment](/deployment/self-hosted) guide for more ASGI deployment patterns. \ No newline at end of file diff --git a/docs/deployment/self-hosted.mdx b/docs/deployment/self-hosted.mdx new file mode 100644 index 000000000..201e00bcb --- /dev/null +++ b/docs/deployment/self-hosted.mdx @@ -0,0 +1,209 @@ +--- +title: Self-Hosted Remote MCP +sidebarTitle: Self-Hosted +description: Deploy your FastMCP server as a remote MCP service accessible via URL +icon: server +--- + + +STDIO transport is perfect for local development and desktop applications. But to unlock the full potential of MCPโ€”centralized services, multi-client access, and network availabilityโ€”you need remote HTTP deployment. + + +This guide walks you through deploying your FastMCP server as a remote MCP service that's accessible via a URL. Once deployed, your MCP server will be available over the network, allowing multiple clients to connect simultaneously and enabling integration with cloud-based LLM applications. This guide focuses specifically on remote MCP deployment, not local STDIO servers. + +## Choosing Your Approach + +FastMCP provides two ways to deploy your server as an HTTP service. Understanding the trade-offs helps you choose the right approach for your needs. + +The **direct HTTP server** approach is simpler and perfect for getting started quickly. You modify your server's `run()` method to use HTTP transport, and FastMCP handles all the web server configuration. This approach works well for standalone deployments where you want your MCP server to be the only service running on a port. + +The **ASGI application** approach gives you more control and flexibility. Instead of running the server directly, you create an ASGI application that can be served by production-grade servers like Uvicorn or Gunicorn. This approach is better when you need advanced server features like multiple workers, custom middleware, or when you're integrating with existing web applications. + +### Direct HTTP Server + +The simplest way to get your MCP server online is to use the built-in `run()` method with HTTP transport. This approach handles all the server configuration for you and is ideal when you want a standalone MCP server without additional complexity. + +```python server.py +from fastmcp import FastMCP + +mcp = FastMCP("My Server") + +@mcp.tool +def process_data(input: str) -> str: + """Process data on the server""" + return f"Processed: {input}" + +if __name__ == "__main__": + mcp.run(transport="http", host="0.0.0.0", port=8000) +``` + +Run your server with a simple Python command: +```bash +python server.py +``` + +Your server is now accessible at `http://localhost:8000/mcp/` (or use your server's actual IP address for remote access). + +This approach is ideal when you want to get online quickly with minimal configuration. It's perfect for internal tools, development environments, or simple deployments where you don't need advanced server features. The built-in server handles all the HTTP details, letting you focus on your MCP implementation. + +### ASGI Application + +For production deployments, you'll often want more control over how your server runs. FastMCP can create a standard ASGI application that works with any ASGI server like Uvicorn, Gunicorn, or Hypercorn. This approach is particularly useful when you need to configure advanced server options, run multiple workers, or integrate with existing infrastructure. + +```python app.py +from fastmcp import FastMCP + +mcp = FastMCP("My Server") + +@mcp.tool +def process_data(input: str) -> str: + """Process data on the server""" + return f"Processed: {input}" + +# Create ASGI application +app = mcp.http_app() +``` + +Run with any ASGI server - here's an example with Uvicorn: +```bash +uvicorn app:app --host 0.0.0.0 --port 8000 +``` + +Your server is accessible at the same URL: `http://localhost:8000/mcp/` (or use your server's actual IP address for remote access). + +The ASGI approach shines in production environments where you need reliability and performance. You can run multiple worker processes to handle concurrent requests, add custom middleware for logging or monitoring, integrate with existing deployment pipelines, or mount your MCP server as part of a larger application. This flexibility makes it the preferred choice for serious deployments. + +## Configuring Your Server + +### Custom Path + +By default, your MCP server is accessible at `/mcp/` on your domain. You can customize this path to fit your URL structure or avoid conflicts with existing endpoints. This is particularly useful when integrating MCP into an existing application or following specific API conventions. + +```python +# Option 1: With mcp.run() +mcp.run(transport="http", host="0.0.0.0", port=8000, path="/api/mcp/") + +# Option 2: With ASGI app +app = mcp.http_app(path="/api/mcp/") +``` + +Now your server is accessible at `http://localhost:8000/api/mcp/`. + +### Authentication + + +Authentication is **highly recommended** for remote MCP servers. Some LLM clients require authentication for remote servers and will refuse to connect without it. + + +FastMCP supports multiple authentication methods to secure your remote server. See the [Authentication Overview](/servers/auth/authentication) for complete configuration options including Bearer tokens, JWT, and OAuth. + +### Health Checks + +Health check endpoints are essential for monitoring your deployed server and ensuring it's responding correctly. FastMCP allows you to add custom routes alongside your MCP endpoints, making it easy to implement health checks that work with both deployment approaches. + +```python +from starlette.responses import JSONResponse + +@mcp.custom_route("/health", methods=["GET"]) +async def health_check(request): + return JSONResponse({"status": "healthy", "service": "mcp-server"}) +``` + +This health endpoint will be available at `http://localhost:8000/health` and can be used by load balancers, monitoring systems, or deployment platforms to verify your server is running. + +## Integration with Web Frameworks + +If you already have a web application running, you can add MCP capabilities by mounting a FastMCP server as a sub-application. This allows you to expose MCP tools alongside your existing API endpoints, sharing the same domain and infrastructure. The MCP server becomes just another route in your application, making it easy to manage and deploy. + +For detailed integration guides, see: +- [FastAPI Integration](/integrations/fastapi) +- [ASGI / Starlette Integration](/integrations/asgi) + +Here's a quick example showing how to add MCP to an existing FastAPI application: + +```python +from fastapi import FastAPI +from fastmcp import FastMCP + +# Your existing API +api = FastAPI() + +@api.get("/api/status") +def status(): + return {"status": "ok"} + +# Create your MCP server +mcp = FastMCP("API Tools") + +@mcp.tool +def query_database(query: str) -> dict: + """Run a database query""" + return {"result": "data"} + +# Mount MCP at /mcp +api.mount("/mcp", mcp.http_app()) + +# Run with: uvicorn app:api --host 0.0.0.0 --port 8000 +``` + +Your existing API remains at `http://localhost:8000/api/` while MCP is available at `http://localhost:8000/mcp/`. + +## Production Deployment + +### Running with Uvicorn + +When deploying to production, you'll want to optimize your server for performance and reliability. Uvicorn provides several options to improve your server's capabilities, including running multiple worker processes to handle concurrent requests and enabling enhanced logging for monitoring. + +```bash +# Install uvicorn with standard extras for better performance +pip install 'uvicorn[standard]' + +# Run with multiple workers for better concurrency +uvicorn app:app --host 0.0.0.0 --port 8000 --workers 4 + +# Enable detailed logging for monitoring +uvicorn app:app --host 0.0.0.0 --port 8000 --log-level info +``` + +### Environment Variables + +Production deployments should never hardcode sensitive information like API keys or authentication tokens. Instead, use environment variables to configure your server at runtime. This keeps your code secure and makes it easy to deploy the same code to different environments with different configurations. + +Here's an example using bearer token authentication (though OAuth is recommended for production): + +```python +import os +from fastmcp import FastMCP +from fastmcp.server.auth import BearerTokenAuth + +# Read configuration from environment +auth_token = os.environ.get("MCP_AUTH_TOKEN") +if auth_token: + auth = BearerTokenAuth(token=auth_token) + mcp = FastMCP("Production Server", auth=auth) +else: + mcp = FastMCP("Production Server") + +app = mcp.http_app() +``` + +Deploy with your secrets safely stored in environment variables: +```bash +MCP_AUTH_TOKEN=secret uvicorn app:app --host 0.0.0.0 --port 8000 +``` + +## Testing Your Deployment + +Once your server is deployed, you'll need to verify it's accessible and functioning correctly. For comprehensive testing strategies including connectivity tests, client testing, and authentication testing, see the [Testing Your Server](/development/tests) guide. + +## Hosting Your Server + +This guide has shown you how to create an HTTP-accessible MCP server, but you'll still need a hosting provider to make it available on the internet. Your FastMCP server can run anywhere that supports Python web applications: + +- **Cloud VMs** (AWS EC2, Google Compute Engine, Azure VMs) +- **Container platforms** (Cloud Run, Container Instances, ECS) +- **Platform-as-a-Service** (Railway, Render, Vercel) +- **Edge platforms** (Cloudflare Workers) +- **Kubernetes clusters** (self-managed or managed) + +The key requirements are Python 3.10+ support and the ability to expose an HTTP port. Most providers will require you to package your server (requirements.txt, Dockerfile, etc.) according to their deployment format. For managed, zero-configuration deployment, see [FastMCP Cloud](/deployment/fastmcp-cloud). \ No newline at end of file diff --git a/docs/deployment/server-configuration.mdx b/docs/deployment/server-configuration.mdx new file mode 100644 index 000000000..789a8005b --- /dev/null +++ b/docs/deployment/server-configuration.mdx @@ -0,0 +1,640 @@ +--- +title: "Project Configuration" +sidebarTitle: "Project Configuration" +description: Use fastmcp.json for portable, declarative project configuration +icon: file-code +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + + + +FastMCP supports declarative configuration through `fastmcp.json` files. This is the canonical and preferred way to configure FastMCP projects, providing a single source of truth for server settings, dependencies, and deployment options that replaces complex command-line arguments. + +The `fastmcp.json` file is designed to be a portable description of your server configuration that can be shared across environments and teams. When running from a `fastmcp.json` file, you can override any configuration values using CLI arguments. + +## Overview + +The `fastmcp.json` configuration file allows you to define all aspects of your FastMCP server in a structured, shareable format. Instead of remembering command-line arguments or writing shell scripts, you declare your server's configuration once and use it everywhere. + +When you have a `fastmcp.json` file, running your server becomes as simple as: + +```bash +# Run the server using the configuration +fastmcp run fastmcp.json + +# Or if fastmcp.json exists in the current directory +fastmcp run +``` + +This configuration approach ensures reproducible deployments across different environments, from local development to production servers. It works seamlessly with Claude Desktop, VS Code extensions, and any MCP-compatible client. + +## File Structure + +The `fastmcp.json` configuration answers three fundamental questions about your server: + +- **Source** = WHERE does your server code live? +- **Environment** = WHAT environment setup does it require? +- **Deployment** = HOW should the server run? + +This conceptual model helps you understand the purpose of each configuration section and organize your settings effectively. The configuration file maps directly to these three concerns: + +```json +{ + "$schema": "https://gofastmcp.com/public/schemas/fastmcp.json/v1.json", + "source": { + // WHERE: Location of your server code + "type": "filesystem", // Optional, defaults to "filesystem" + "path": "server.py", + "entrypoint": "mcp" + }, + "environment": { + // WHAT: Environment setup and dependencies + "type": "uv", // Optional, defaults to "uv" + "python": ">=3.10", + "dependencies": ["pandas", "numpy"] + }, + "deployment": { + // HOW: Runtime configuration + "transport": "stdio", + "log_level": "INFO" + } +} +``` + +Only the `source` field is required. The `environment` and `deployment` sections are optional and provide additional configuration when needed. + +### JSON Schema Support + +FastMCP provides JSON schemas for IDE autocomplete and validation. Add the schema reference to your `fastmcp.json` for enhanced developer experience: + +```json +{ + "$schema": "https://gofastmcp.com/public/schemas/fastmcp.json/v1.json", + "source": { + "path": "server.py", + "entrypoint": "mcp" + } +} +``` + +Two schema URLs are available: +- **Version-specific**: `https://gofastmcp.com/public/schemas/fastmcp.json/v1.json` +- **Latest version**: `https://gofastmcp.com/public/schemas/fastmcp.json/latest.json` + +Modern IDEs like VS Code will automatically provide autocomplete suggestions, validation, and inline documentation when the schema is specified. + +### Source Configuration + +The source configuration determines **WHERE** your server code lives. It tells FastMCP how to find and load your server, whether it's a local Python file, a remote repository, or hosted in the cloud. This section is required and forms the foundation of your configuration. + + + + The server source configuration that determines where your server code lives. + + + The source type identifier that determines which implementation to use. Currently supports `"filesystem"` for local files. Future releases will add support for `"git"` and `"cloud"` source types. + + + + When `type` is `"filesystem"` (or omitted), the source points to a local Python file containing your FastMCP server: + + + Path to the Python file containing your FastMCP server. + + + + Name of the server instance or factory function within the module: + - Can be a FastMCP server instance (e.g., `mcp = FastMCP("MyServer")`) + - Can be a function with no arguments that returns a FastMCP server + - If not specified, FastMCP searches for common names: `mcp`, `server`, or `app` + + + **Example:** + ```json + "source": { + "type": "filesystem", + "path": "src/server.py", + "entrypoint": "mcp" + } + ``` + + Note: File paths are resolved relative to the configuration file's location. + + + + + +**Future Source Types** + +Future releases will support additional source types: +- **Git repositories** (`type: "git"`) for loading server code directly from version control +- **FastMCP Cloud** (`type: "cloud"`) for hosted servers with automatic scaling and management + + +### Environment Configuration + +The environment configuration determines **WHAT** environment setup your server requires. It controls the build-time setup of your Python environment, ensuring your server runs with the exact Python version and dependencies it requires. This section creates isolated, reproducible environments across different systems. + +FastMCP uses an extensible environment system with a base `Environment` class that can be implemented by different environment providers. Currently, FastMCP supports the `UVEnvironment` for Python environment management using `uv`'s powerful dependency resolver. + + + + Optional environment configuration. When specified, FastMCP uses the appropriate environment implementation to set up your server's runtime. + + + The environment type identifier that determines which implementation to use. Currently supports `"uv"` for Python environments managed by uv. If omitted, defaults to `"uv"`. + + + + When `type` is `"uv"` (or omitted), the environment uses uv to manage Python dependencies: + + + Python version constraint. Examples: + - Exact version: `"3.12"` + - Minimum version: `">=3.10"` + - Version range: `">=3.10,<3.13"` + + + + List of pip packages with optional version specifiers (PEP 508 format). + ```json + "dependencies": ["pandas>=2.0", "requests", "httpx"] + ``` + + + + Path to a requirements.txt file, resolved relative to the config file location. + ```json + "requirements": "requirements.txt" + ``` + + + + Path to a project directory containing pyproject.toml for uv project management. + ```json + "project": "." + ``` + + + + List of paths to packages to install in editable/development mode. Useful for local development when you want changes to be reflected immediately. Supports multiple packages for monorepo setups or shared libraries. + ```json + "editable": ["."] + ``` + Or with multiple packages: + ```json + "editable": [".", "../shared-lib", "/path/to/another-package"] + ``` + + + **Example:** + ```json + "environment": { + "type": "uv", + "python": ">=3.10", + "dependencies": ["pandas", "numpy"], + "editable": ["."] + } + ``` + + Note: When any UVEnvironment field is specified, FastMCP automatically creates an isolated environment using `uv` before running your server. + + + + +When environment configuration is provided, FastMCP: +1. Detects the environment type (defaults to `"uv"` if not specified) +2. Creates an isolated environment using the appropriate provider +3. Installs the specified dependencies +4. Runs your server in this clean environment + +This build-time setup ensures your server always has the dependencies it needs, without polluting your system Python or conflicting with other projects. + + +**Future Environment Types** + +Similar to source types, future releases may support additional environment types for different runtime requirements, such as Docker containers or language-specific environments beyond Python. + + +### Deployment Configuration + +The deployment configuration controls **HOW** your server runs. It defines the runtime behavior including network settings, environment variables, and execution context. These settings determine how your server operates when it executes, from transport protocols to logging levels. + +Environment variables are included in this section because they're runtime configuration that affects how your server behaves when it executes, not how its environment is built. The deployment configuration is applied every time your server starts, controlling its operational characteristics. + + + + Optional runtime configuration for the server. + + + + Protocol for client communication: + - `"stdio"`: Standard input/output for desktop clients + - `"http"`: Network-accessible HTTP server + - `"sse"`: Server-sent events + + + + Network interface to bind (HTTP transport only): + - `"127.0.0.1"`: Local connections only + - `"0.0.0.0"`: All network interfaces + + + + Port number for HTTP transport. + + + + URL path for the MCP endpoint when using HTTP transport. + + + + Server logging verbosity. Options: + - `"DEBUG"`: Detailed debugging information + - `"INFO"`: General informational messages + - `"WARNING"`: Warning messages + - `"ERROR"`: Error messages only + - `"CRITICAL"`: Critical errors only + + + + Environment variables to set when running the server. Supports `${VAR_NAME}` syntax for runtime interpolation. + ```json + "env": { + "API_KEY": "secret-key", + "DATABASE_URL": "postgres://${DB_USER}@${DB_HOST}/mydb" + } + ``` + + + + Working directory for the server process. Relative paths are resolved from the config file location. + + + + Command-line arguments to pass to the server, passed after `--` to the server's argument parser. + ```json + "args": ["--config", "server-config.json"] + ``` + + + + + +#### Environment Variable Interpolation + +The `env` field in deployment configuration supports runtime interpolation of environment variables using `${VAR_NAME}` syntax. This enables dynamic configuration based on your deployment environment: + +```json +{ + "deployment": { + "env": { + "API_URL": "https://api.${ENVIRONMENT}.example.com", + "DATABASE_URL": "postgres://${DB_USER}:${DB_PASS}@${DB_HOST}/myapp", + "CACHE_KEY": "myapp_${ENVIRONMENT}_${VERSION}" + } + } +} +``` + +When the server starts, FastMCP replaces `${ENVIRONMENT}`, `${DB_USER}`, etc. with values from your system's environment variables. If a variable doesn't exist, the placeholder is preserved as-is. + +**Example**: If your system has `ENVIRONMENT=production` and `DB_HOST=db.example.com`: +```json +// Configuration +{ + "deployment": { + "env": { + "API_URL": "https://api.${ENVIRONMENT}.example.com", + "DB_HOST": "${DB_HOST}" + } + } +} + +// Result at runtime +{ + "API_URL": "https://api.production.example.com", + "DB_HOST": "db.example.com" +} +``` + +This feature is particularly useful for: +- Deploying the same configuration across development, staging, and production +- Keeping sensitive values out of configuration files +- Building dynamic URLs and connection strings +- Creating environment-specific prefixes or suffixes + +## Usage with CLI Commands + +FastMCP automatically detects and uses a file specifically named `fastmcp.json` in the current directory, making server execution simple and consistent. Files with FastMCP configuration format but different names are not auto-detected and must be specified explicitly: + +```bash +# Auto-detect fastmcp.json in current directory +cd my-project +fastmcp run # No arguments needed! + +# Or specify a configuration file explicitly +fastmcp run prod.fastmcp.json + +# Skip environment setup when already in a uv environment +fastmcp run fastmcp.json --skip-env + +# Skip source preparation when source is already prepared +fastmcp run fastmcp.json --skip-source + +# Skip both environment and source preparation +fastmcp run fastmcp.json --skip-env --skip-source +``` + +### Pre-building Environments + +You can use `fastmcp project prepare` to create a persistent uv project with all dependencies pre-installed: + +```bash +# Create a persistent environment +fastmcp project prepare fastmcp.json --output-dir ./env + +# Use the pre-built environment to run the server +fastmcp run fastmcp.json --project ./env +``` + +This pattern separates environment setup (slow) from server execution (fast), useful for deployment scenarios. + +### Using an Existing Environment + +By default, FastMCP creates an isolated environment with `uv` based on your configuration. When you already have a suitable Python environment, use the `--skip-env` flag to skip environment creation: + +```bash +fastmcp run fastmcp.json --skip-env +``` + +**When you already have an environment:** +- You're in an activated virtual environment with all dependencies installed +- You're inside a Docker container with pre-installed dependencies +- You're in a CI/CD pipeline that pre-builds the environment +- You're using a system-wide installation with all required packages +- You're in a uv-managed environment (prevents infinite recursion) + +This flag tells FastMCP: "I already have everything installed, just run the server." + +### Using an Existing Source + +When working with source types that require preparation (future support for git repositories or cloud sources), use the `--skip-source` flag when you already have the source code available: + +```bash +fastmcp run fastmcp.json --skip-source +``` + +**When you already have the source:** +- You've previously cloned a git repository and don't need to re-fetch +- You have a cached copy of a cloud-hosted server +- You're in a CI/CD pipeline where source checkout is a separate step +- You're iterating locally on already-downloaded code + +This flag tells FastMCP: "I already have the source code, skip any download/clone steps." + +Note: For filesystem sources (local Python files), this flag has no effect since they don't require preparation. + +The configuration file works with all FastMCP commands: +- **`run`** - Start the server in production mode +- **`dev`** - Launch with the Inspector UI for development +- **`inspect`** - View server capabilities and configuration +- **`install`** - Install to Claude Desktop, Cursor, or other MCP clients + +When no file argument is provided, FastMCP searches the current directory for `fastmcp.json`. This means you can simply navigate to your project directory and run `fastmcp run` to start your server with all its configured settings. + +### CLI Override Behavior + +Command-line arguments take precedence over configuration file values, allowing ad-hoc adjustments without modifying the file: + +```bash +# Config specifies port 3000, CLI overrides to 8080 +fastmcp run fastmcp.json --port 8080 + +# Config specifies stdio, CLI overrides to HTTP +fastmcp run fastmcp.json --transport http + +# Add extra dependencies not in config +fastmcp run fastmcp.json --with requests --with httpx +``` + +This precedence order enables: +- Quick testing of different settings +- Environment-specific overrides in deployment scripts +- Debugging with increased log levels +- Temporary configuration changes + +### Custom Naming Patterns + +You can use different configuration files for different environments: + +- `fastmcp.json` - Default configuration +- `dev.fastmcp.json` - Development settings +- `prod.fastmcp.json` - Production settings +- `test_fastmcp.json` - Test configuration + +Any file with "fastmcp.json" in the name is recognized as a configuration file. + +## Examples + + + + +A minimal configuration for a simple server: + +```json +{ + "$schema": "https://gofastmcp.com/public/schemas/fastmcp.json/v1.json", + "source": { + "path": "server.py", + "entrypoint": "mcp" + } +} +``` +This configuration explicitly specifies the server entrypoint (`mcp`), making it clear which server instance or factory function to use. Uses all defaults: STDIO transport, no special dependencies, standard logging. + + + +A configuration optimized for local development: + +```json +{ + "$schema": "https://gofastmcp.com/public/schemas/fastmcp.json/v1.json", + // WHERE does the server live? + "source": { + "path": "src/server.py", + "entrypoint": "app" + }, + // WHAT dependencies does it need? + "environment": { + "type": "uv", + "python": "3.12", + "dependencies": ["fastmcp[dev]"], + "editable": "." + }, + // HOW should it run? + "deployment": { + "transport": "http", + "host": "127.0.0.1", + "port": 8000, + "log_level": "DEBUG", + "env": { + "DEBUG": "true", + "ENV": "development" + } + } +} +``` + + + +A production-ready configuration with full dependency management: + +```json +{ + "$schema": "https://gofastmcp.com/public/schemas/fastmcp.json/v1.json", + // WHERE does the server live? + "source": { + "path": "app/main.py", + "entrypoint": "mcp_server" + }, + // WHAT dependencies does it need? + "environment": { + "python": "3.11", + "requirements": "requirements/production.txt", + "project": "." + }, + // HOW should it run? + "deployment": { + "transport": "http", + "host": "0.0.0.0", + "port": 3000, + "path": "/api/mcp/", + "log_level": "INFO", + "env": { + "ENV": "production", + "API_BASE_URL": "https://api.example.com", + "DATABASE_URL": "postgresql://user:pass@db.example.com/prod" + }, + "cwd": "/app", + "args": ["--workers", "4"] + } +} +``` + + + +Configuration for a data analysis server with scientific packages: + +```json +{ + "$schema": "https://gofastmcp.com/public/schemas/fastmcp.json/v1.json", + "source": { + "path": "analysis_server.py", + "entrypoint": "mcp" + }, + "environment": { + "python": "3.11", + "dependencies": [ + "pandas>=2.0", + "numpy", + "scikit-learn", + "matplotlib", + "jupyterlab" + ] + }, + "deployment": { + "transport": "stdio", + "env": { + "MATPLOTLIB_BACKEND": "Agg", + "DATA_PATH": "./datasets" + } + } +} +``` + + + +You can maintain multiple configuration files for different environments: + +**dev.fastmcp.json**: +```json +{ + "$schema": "https://gofastmcp.com/public/schemas/fastmcp.json/v1.json", + "source": { + "path": "server.py", + "entrypoint": "mcp" + }, + "deployment": { + "transport": "http", + "log_level": "DEBUG" + } +} +``` + +**prod.fastmcp.json**: +```json +{ + "$schema": "https://gofastmcp.com/public/schemas/fastmcp.json/v1.json", + "source": { + "path": "server.py", + "entrypoint": "mcp" + }, + "environment": { + "requirements": "requirements/production.txt" + }, + "deployment": { + "transport": "http", + "host": "0.0.0.0", + "log_level": "WARNING" + } +} +``` + +Run different configurations: +```bash +fastmcp run dev.fastmcp.json # Development +fastmcp run prod.fastmcp.json # Production +``` + + + +## Migrating from CLI Arguments + +If you're currently using command-line arguments or shell scripts, migrating to `fastmcp.json` simplifies your workflow. Here's how common CLI patterns map to configuration: + +**CLI Command**: +```bash +uv run --with pandas --with requests \ + fastmcp run server.py \ + --transport http \ + --port 8000 \ + --log-level INFO +``` + +**Equivalent fastmcp.json**: +```json +{ + "$schema": "https://gofastmcp.com/public/schemas/fastmcp.json/v1.json", + "source": { + "path": "server.py", + "entrypoint": "mcp" + }, + "environment": { + "dependencies": ["pandas", "requests"] + }, + "deployment": { + "transport": "http", + "port": 8000, + "log_level": "INFO" + } +} +``` + +Now simply run: +```bash +fastmcp run # Automatically finds and uses fastmcp.json +``` + +The configuration file approach provides better documentation, easier sharing, and consistent execution across different environments while maintaining the flexibility to override settings when needed. \ No newline at end of file diff --git a/docs/development/contributing.mdx b/docs/development/contributing.mdx new file mode 100644 index 000000000..c97dcf565 --- /dev/null +++ b/docs/development/contributing.mdx @@ -0,0 +1,187 @@ +--- +title: "Contributing" +description: "Development workflow for FastMCP contributors" +icon: code-pull-request +--- + +Contributing to FastMCP means joining a community that values clean, maintainable code and thoughtful API design. All contributions are valued - from fixing typos in documentation to implementing major features. + +## Issues + +### Issue First, Code Second + +**Every pull request requires a corresponding issue - no exceptions.** This requirement creates a collaborative space where approach, scope, and alignment are established before code is written. Issues serve as design documents where maintainers and contributors discuss implementation strategy, identify potential conflicts with existing patterns, and ensure proposed changes advance FastMCP's vision. + +**FastMCP is an opinionated framework, not a kitchen sink.** The maintainers have strong beliefs about what FastMCP should and shouldn't do. Just because something takes N lines of code and you want it in fewer lines doesn't mean FastMCP should take on the maintenance burden or endorse that pattern. This is judged at the maintainers' discretion. + +Use issues to understand scope BEFORE opening PRs. The issue discussion determines whether a feature belongs in core, contrib, or not at all. + +### Writing Good Issues + +FastMCP is an extremely highly-trafficked repository maintained by a very small team. Issues that appear to transfer burden to maintainers without any effort to validate the problem will be closed. Please help the maintainers help you by always providing a minimal reproducible example and clearly describing the problem. + +**LLM-generated issues will be closed immediately.** Issues that contain paragraphs of unnecessary explanation, verbose problem descriptions, or obvious LLM authorship patterns obfuscate the actual problem and transfer burden to maintainers. + +Write clear, concise issues that: +- State the problem directly +- Provide a minimal reproducible example +- Skip unnecessary background or context +- Take responsibility for clear communication + +Issues may be labeled "Invalid" simply due to confusion caused by verbosity or not adhering to the guidelines outlined here. + +## Pull Requests + +PRs that deviate from FastMCP's core principles will be rejected regardless of implementation quality. **PRs are NOT for iterating on ideas** - they should only be opened for ideas that already have a bias toward acceptance based on issue discussion. + + +### Development Environment + +#### Installation + +To contribute to FastMCP, you'll need to set up a development environment with all necessary tools and dependencies. + +```bash +# Clone the repository +git clone https://github.com/jlowin/fastmcp.git +cd fastmcp + +# Install all dependencies including dev tools +uv sync + +# Install pre-commit hooks +uv run pre-commit install +``` + +In addition, some development commands require [just](https://github.com/casey/just) to be installed. + +Pre-commit hooks will run automatically on every commit to catch issues before they reach CI. If you see failures, fix them before committing - never commit broken code expecting to fix it later. + +### Development Standards + +#### Scope + +Large pull requests create review bottlenecks and quality risks. Unless you're fixing a discrete bug or making an incredibly well-scoped change, keep PRs small and focused. + +A PR that changes 50 lines across 3 files can be thoroughly reviewed in minutes. A PR that changes 500 lines across 20 files requires hours of careful analysis and often hides subtle issues. + +Breaking large features into smaller PRs: +- Creates better review experiences +- Makes git history clear +- Simplifies debugging with bisect +- Reduces merge conflicts +- Gets your code merged faster + +#### Code Quality + +FastMCP values clarity over cleverness. Every line you write will be maintained by someone else - possibly years from now, possibly without context about your decisions. + +**PRs can be rejected for two opposing reasons:** +1. **Insufficient quality** - Code that doesn't meet our standards for clarity, maintainability, or idiomaticity +2. **Overengineering** - Code that is overbearing, unnecessarily complex, or tries to be too clever + +The focus is on idiomatic, high-quality Python. FastMCP uses patterns like `NotSet` type as an alternative to `None` in certain situations - follow existing patterns. + +#### Required Practices + +**Full type annotations** on all functions and methods. They catch bugs before runtime and serve as inline documentation. + +**Async/await patterns** for all I/O operations. Even if your specific use case doesn't need concurrency, consistency means users can compose features without worrying about blocking operations. + +**Descriptive names** make code self-documenting. `auth_token` is clear; `tok` requires mental translation. + +**Specific exception types** make error handling predictable. Catching `ValueError` tells readers exactly what error you expect. Never use bare `except` clauses. + +#### Anti-Patterns to Avoid + +**Complex one-liners** are hard to debug and modify. Break operations into clear steps. + +**Mutable default arguments** cause subtle bugs. Use `None` as the default and create the mutable object inside the function. + +**Breaking established patterns** confuses readers. If you must deviate, discuss in the issue first. + +### Pre-Commit Checks + +```bash +# Runs automatically on commit, or manually: +uv run pre-commit run --all-files +``` + +This runs three critical tools: +- **Ruff**: Linting and formatting +- **ty**: Static type checking +- **Pytest**: Core test suite + +CI will reject PRs that fail these checks. Always run them locally first. + +### Testing + +Tests are documentation that shows how features work. Good tests give reviewers confidence and help future maintainers understand intent. + +```bash +# Run specific test directory +uv run pytest tests/server/ -v + +# Run all tests before submitting PR +uv run pytest +``` + +Every new feature needs tests. See the [Testing Guide](/development/tests) for patterns and requirements. + +### Documentation + +A feature doesn't exist unless it's documented. Note that FastMCP's hosted documentation always tracks the main branch - users who want historical documentation can clone the repo, checkout a specific tag, and host it themselves. + +```bash +# Preview documentation locally +just docs +``` + +Documentation requirements: +- **Explain concepts in prose first** - Code without context is just syntax +- **Complete, runnable examples** - Every code block should be copy-pasteable +- **Register in docs.json** - Makes pages appear in navigation +- **Version badges** - Mark when features were added using `` + +#### SDK Documentation + +FastMCP's SDK documentation is auto-generated from the source code docstrings and type annotations. It is automatically updated on every merge to main by a GitHub Actions workflow, so users are *not* responsible for keeping the documentation up to date. However, to generate it proactively, you can use the following command: + +```bash +just api-ref-all +``` + +### Submitting Your PR + +#### Before Submitting + +1. **Run all checks**: `uv run pre-commit run --all-files && uv run pytest` +2. **Keep scope small**: One feature or fix per PR +3. **Write clear description**: Your PR description becomes permanent documentation +4. **Update docs**: Include documentation for API changes + +#### PR Description + +Write PR descriptions that explain: +- What problem you're solving +- Why you chose this approach +- Any trade-offs or alternatives considered +- Migration path for breaking changes + +Focus on the "why" - the code shows the "what". Keep it concise but complete. + +#### What We Look For + +**Framework Philosophy**: FastMCP is NOT trying to do all things or provide all shortcuts. Features are rejected when they don't align with the framework's vision, even if perfectly implemented. The burden of proof is on the PR to demonstrate value. + +**Code Quality**: We verify code follows existing patterns. Consistency reduces cognitive load. When every module works similarly, developers understand new code quickly. + +**Test Coverage**: Not every line needs testing, but every behavior does. Tests document intent and protect against regressions. + +**Breaking Changes**: May be acceptable in minor versions but must be clearly documented. See the [versioning policy](/development/releases#versioning-policy). + +## Special Modules + +**`contrib`**: Community-maintained patterns and utilities. Original authors maintain their contributions. Not representative of the core framework. + +**`experimental`**: Maintainer-developed features that may preview future functionality. Can break or be deleted at any time without notice. Pin your FastMCP version when using these features. \ No newline at end of file diff --git a/docs/development/releases.mdx b/docs/development/releases.mdx new file mode 100644 index 000000000..346462736 --- /dev/null +++ b/docs/development/releases.mdx @@ -0,0 +1,79 @@ +--- +title: "Releases" +description: "FastMCP versioning and release process" +icon: "truck-fast" +--- + +FastMCP releases frequently to deliver features quickly in the rapidly evolving MCP ecosystem. We use semantic versioning pragmatically - the Model Context Protocol is young, patterns are still emerging, and waiting for perfect stability would mean missing opportunities to empower developers with better tools. + +## Versioning Policy + +### Semantic Versioning + +**Major (x.0.0)**: Complete API redesigns + +Major versions represent fundamental shifts. FastMCP 2.x is entirely different from 1.x in both implementation and design philosophy. + +**Minor (2.x.0)**: New features and evolution + + +Unlike traditional semantic versioning, minor versions **may** include [breaking changes](#breaking-changes) when necessary for the ecosystem's evolution. This flexibility is essential in a young ecosystem where perfect backwards compatibility would prevent important improvements. + + +FastMCP always targets the most current MCP Protocol version. Breaking changes in the MCP spec or MCP SDK automatically flow through to FastMCP - we prioritize staying current with the latest features and conventions over maintaining compatibility with older protocol versions. + +**Patch (2.0.x)**: Bug fixes and refinements + +Patch versions contain only bug fixes without breaking changes. These are safe updates you can apply with confidence. + +### Breaking Changes + +We permit breaking changes in minor versions because the MCP ecosystem is rapidly evolving. Refusing to break problematic APIs would accumulate design debt that eventually makes the framework unusable. Each breaking change represents a deliberate decision to keep FastMCP aligned with the ecosystem's evolution. + +When breaking changes occur: +- They only happen in minor versions (e.g., 2.3.x to 2.4.0) +- Release notes explain what changed and how to migrate +- We provide deprecation warnings at least 1 minor version in advance when possible +- Changes must substantially benefit users to justify disruption + +The public API is what's covered by our compatibility guarantees - these are the parts of FastMCP you can rely on to remain stable within a minor version. The public API consists of: +- `FastMCP` server class, `Client` class, and FastMCP `Context` +- Core MCP components: `Tool`, `Prompt`, `Resource`, `ResourceTemplate`, and transports +- Their public methods and documented behaviors + +Everything else (utilities, private methods, internal modules) may change without notice. This boundary lets us refactor internals and improve implementation details without breaking your code. For production stability, pin to specific versions. + + +The `fastmcp.server.auth` module was introduced in 2.12.0 and is exempted from this policy temporarily, meaning it is *expected* to have breaking changes even on patch versions. This is because auth is a rapidly evolving part of the MCP spec and it would be dangerous to be beholden to old decisions. Please pin your FastMCP version if using authentication in production. + +We expect this exemption to last through at least the 2.12.x and 2.13.x release series. + + +### Production Use + +Pin to exact versions: +``` +fastmcp==2.11.0 # Good +fastmcp>=2.11.0 # Bad - will install breaking changes +``` + +## Creating Releases + +Our release process is intentionally simple: + +1. Create GitHub release with tag `vMAJOR.MINOR.PATCH` (e.g., `v2.11.0`) +2. Generate release notes automatically, and curate or add additional editorial information as needed +3. GitHub releases automatically trigger PyPI deployments + +This automation lets maintainers focus on code quality rather than release mechanics. + +### Release Cadence + +We follow a feature-driven release cadence rather than a fixed schedule. Minor versions ship approximately every 3-4 weeks when significant functionality is ready. + +Patch releases ship promptly for: +- Critical bug fixes +- Security updates (immediate release) +- Regression fixes + +This approach means you get improvements as soon as they're ready rather than waiting for arbitrary release dates. diff --git a/docs/development/tests.mdx b/docs/development/tests.mdx new file mode 100644 index 000000000..7263d7dea --- /dev/null +++ b/docs/development/tests.mdx @@ -0,0 +1,350 @@ +--- +title: "Tests" +description: "Testing patterns and requirements for FastMCP" +icon: vial +--- + +Good tests are the foundation of reliable software. In FastMCP, we treat tests as first-class documentation that demonstrates how features work while protecting against regressions. Every new capability needs comprehensive tests that demonstrate correctness. + +## FastMCP Tests + +### Running Tests + +```bash +# Run all tests +uv run pytest + +# Run specific test file +uv run pytest tests/server/test_auth.py + +# Run with coverage +uv run pytest --cov=fastmcp + +# Skip integration tests for faster runs +uv run pytest -m "not integration" + +# Skip tests that spawn processes +uv run pytest -m "not integration and not client_process" +``` + +Tests should complete in under 1 second unless marked as integration tests. This speed encourages running them frequently, catching issues early. + +### Test Organization + +Our test organization mirrors the `src/` directory structure, creating a predictable mapping between code and tests. When you're working on `src/fastmcp/server/auth.py`, you'll find its tests in `tests/server/test_auth.py`. In rare cases tests are split further - for example, the OpenAPI tests are so comprehensive they're split across multiple files. + +### Test Markers + +We use pytest markers to categorize tests that require special resources or take longer to run: + +```python +@pytest.mark.integration +async def test_github_api_integration(): + """Test GitHub API integration with real service.""" + token = os.getenv("FASTMCP_GITHUB_TOKEN") + if not token: + pytest.skip("FASTMCP_GITHUB_TOKEN not available") + + # Test against real GitHub API + client = GitHubClient(token) + repos = await client.list_repos("jlowin") + assert "fastmcp" in [repo.name for repo in repos] + +@pytest.mark.client_process +async def test_stdio_transport(): + """Test STDIO transport with separate process.""" + # This spawns a subprocess + async with Client("python examples/simple_echo.py") as client: + result = await client.call_tool("echo", {"message": "test"}) + assert result.content[0].text == "test" +``` + +## Writing Tests + + +### Test Requirements + +Following these practices creates maintainable, debuggable test suites that serve as both documentation and regression protection. + +#### Single Behavior Per Test + +Each test should verify exactly one behavior. When it fails, you need to know immediately what broke. A test that checks five things gives you five potential failure points to investigate. A test that checks one thing points directly to the problem. + + + +```python Good: Atomic Test +async def test_tool_registration(): + """Test that tools are properly registered with the server.""" + mcp = FastMCP("test-server") + + @mcp.tool + def add(a: int, b: int) -> int: + return a + b + + tools = mcp.list_tools() + assert len(tools) == 1 + assert tools[0].name == "add" +``` + +```python Bad: Multi-Behavior Test +async def test_server_functionality(): + """Test multiple server features at once.""" + mcp = FastMCP("test-server") + + # Tool registration + @mcp.tool + def add(a: int, b: int) -> int: + return a + b + + # Resource creation + @mcp.resource("config://app") + def get_config(): + return {"version": "1.0"} + + # Authentication setup + mcp.auth = BearerTokenProvider({"token": "user"}) + + # What exactly are we testing? If this fails, what broke? + assert mcp.list_tools() + assert mcp.list_resources() + assert mcp.auth is not None +``` + + + +#### Self-Contained Setup + +Every test must create its own setup. Tests should be runnable in any order, in parallel, or in isolation. When a test fails, you should be able to run just that test to reproduce the issue. + + + +```python Good: Self-Contained +async def test_tool_execution_with_error(): + """Test that tool errors are properly handled.""" + mcp = FastMCP("test-server") + + @mcp.tool + def divide(a: int, b: int) -> float: + if b == 0: + raise ValueError("Cannot divide by zero") + return a / b + + async with Client(mcp) as client: + with pytest.raises(Exception): + await client.call_tool("divide", {"a": 10, "b": 0}) +``` + +```python Bad: Test Dependencies +# Global state that tests depend on +test_server = None + +def test_setup_server(): + """Setup for other tests.""" + global test_server + test_server = FastMCP("shared-server") + +def test_server_works(): + """Test server functionality.""" + # Depends on test_setup_server running first + assert test_server is not None +``` + + + +#### Clear Intent + +Test names and assertions should make the verified behavior obvious. A developer reading your test should understand what feature it validates and how that feature should behave. + +```python +async def test_authenticated_tool_requires_valid_token(): + """Test that authenticated users can access protected tools.""" + mcp = FastMCP("test-server") + mcp.auth = BearerTokenProvider({"secret-token": "test-user"}) + + @mcp.tool + def protected_action() -> str: + return "success" + + async with Client(mcp, auth=BearerAuth("secret-token")) as client: + result = await client.call_tool("protected_action", {}) + assert result.content[0].text == "success" +``` + +#### Using Fixtures + +Use fixtures to create reusable data, server configurations, or other resources for your tests. Note that you should **not** open FastMCP clients in your fixtures as it can create hard-to-diagnose issues with event loops. + +```python +import pytest +from fastmcp import FastMCP, Client + +@pytest.fixture +def weather_server(): + server = FastMCP("WeatherServer") + + @server.tool + def get_temperature(city: str) -> dict: + temps = {"NYC": 72, "LA": 85, "Chicago": 68} + return {"city": city, "temp": temps.get(city, 70)} + + return server + +async def test_temperature_tool(weather_server): + async with Client(weather_server) as client: + result = await client.call_tool("get_temperature", {"city": "LA"}) + assert result.data == {"city": "LA", "temp": 85} +``` + +#### Effective Assertions + +Assertions should be specific and provide context on failure. When a test fails during CI, the assertion message should tell you exactly what went wrong. + +```python +# Basic assertion - minimal context on failure +assert result.status == "success" + +# Better - explains what was expected +assert result.status == "success", f"Expected successful operation, got {result.status}: {result.error}" +``` + +Try not to have too many assertions in a single test unless you truly need to check various aspects of the same behavior. In general, assertions of different behaviors should be in separate tests. + +#### Inline Snapshots + +FastMCP uses `inline-snapshot` for testing complex data structures. On first run of `pytest --inline-snapshot=create` with an empty `snapshot()`, pytest will auto-populate the expected value. To update snapshots after intentional changes, run `pytest --inline-snapshot=fix`. This is particularly useful for testing JSON schemas and API responses. + +```python +from inline_snapshot import snapshot + +async def test_tool_schema_generation(): + """Test that tool schemas are generated correctly.""" + mcp = FastMCP("test-server") + + @mcp.tool + def calculate_tax(amount: float, rate: float = 0.1) -> dict: + """Calculate tax on an amount.""" + return {"amount": amount, "tax": amount * rate, "total": amount * (1 + rate)} + + tools = mcp.list_tools() + schema = tools[0].inputSchema + + # First run: snapshot() is empty, gets auto-populated + # Subsequent runs: compares against stored snapshot + assert schema == snapshot({ + "type": "object", + "properties": { + "amount": {"type": "number"}, + "rate": {"type": "number", "default": 0.1} + }, + "required": ["amount"] + }) +``` + +### In-Memory Testing + +FastMCP uses in-memory transport for testing, where servers and clients communicate directly. The majority of functionality can be tested in a deterministic fashion this way. We use more complex setups only when testing transports themselves. + +The in-memory transport runs the real MCP protocol implementation without network overhead. Instead of deploying your server or managing network connections, you pass your server instance directly to the client. Everything runs in the same Python process - you can set breakpoints anywhere and step through with your debugger. + +```python +from fastmcp import FastMCP, Client + +# Create your server +server = FastMCP("WeatherServer") + +@server.tool +def get_temperature(city: str) -> dict: + """Get current temperature for a city""" + temps = {"NYC": 72, "LA": 85, "Chicago": 68} + return {"city": city, "temp": temps.get(city, 70)} + +async def test_weather_operations(): + # Pass server directly - no deployment needed + async with Client(server) as client: + result = await client.call_tool("get_temperature", {"city": "NYC"}) + assert result.data == {"city": "NYC", "temp": 72} +``` + +This pattern makes tests deterministic and fast - typically completing in milliseconds rather than seconds. + +### Mocking External Dependencies + +FastMCP servers are standard Python objects, so you can mock external dependencies using your preferred approach: + +```python +from unittest.mock import AsyncMock + +async def test_database_tool(): + server = FastMCP("DataServer") + + # Mock the database + mock_db = AsyncMock() + mock_db.fetch_users.return_value = [ + {"id": 1, "name": "Alice"}, + {"id": 2, "name": "Bob"} + ] + + @server.tool + async def list_users() -> list: + return await mock_db.fetch_users() + + async with Client(server) as client: + result = await client.call_tool("list_users", {}) + assert len(result.data) == 2 + assert result.data[0]["name"] == "Alice" + mock_db.fetch_users.assert_called_once() +``` + +### Testing Network Transports + +While in-memory testing covers most unit testing needs, you'll occasionally need to test actual network transports. Use the `run_server_in_process` utility to spawn a server in a separate process for testing: + +```python +import pytest +from fastmcp.utilities.tests import run_server_in_process +from fastmcp import FastMCP, Client +from fastmcp.client.transports import StreamableHttpTransport + +def run_server(host: str, port: int) -> None: + """Function to run in subprocess.""" + server = FastMCP("TestServer") + + @server.tool + def greet(name: str) -> str: + return f"Hello, {name}!" + + server.run(host=host, port=port) + +@pytest.fixture +async def http_server(): + """Fixture that runs server in subprocess.""" + with run_server_in_process(run_server, transport="http") as url: + yield f"{url}/mcp" + +async def test_http_transport(http_server: str): + """Test actual HTTP transport behavior.""" + async with Client( + transport=StreamableHttpTransport(http_server) + ) as client: + result = await client.ping() + assert result is True + + greeting = await client.call_tool("greet", {"name": "World"}) + assert greeting.data == "Hello, World!" +``` + +The `run_server_in_process` utility handles server lifecycle, port allocation, and cleanup automatically. This pattern is essential for testing transport-specific behavior like timeouts, headers, and authentication. Note that FastMCP often uses the `client_process` marker to isolate tests that spawn processes, as they can create contention in CI. + +### Documentation Testing + +Documentation requires the same validation as code. The `just docs` command launches a local Mintlify server that renders your documentation exactly as users will see it: + +```bash +# Start local documentation server with hot reload +just docs + +# Or run Mintlify directly +mintlify dev +``` + +The local server watches for changes and automatically refreshes. This preview catches formatting issues and helps you see documentation as users will experience it. \ No newline at end of file diff --git a/docs/docs.json b/docs/docs.json new file mode 100644 index 000000000..758eca70f --- /dev/null +++ b/docs/docs.json @@ -0,0 +1,462 @@ +{ + "$schema": "https://mintlify.com/docs.json", + "appearance": { + "default": "system", + "strict": false + }, + "background": { + "color": { + "dark": "#222831", + "light": "#EEEEEE" + }, + "decoration": "windows" + }, + "banner": { + "content": "Host your server on [FastMCP Cloud](https://fastmcp.cloud) for free!" + }, + "colors": { + "dark": "#f72585", + "light": "#4cc9f0", + "primary": "#2d00f7" + }, + "contextual": { + "options": [ + "copy", + "view" + ] + }, + "description": "The fast, Pythonic way to build MCP servers and clients.", + "favicon": { + "dark": "/assets/brand/favicon.svg", + "light": "/assets/brand/favicon.svg" + }, + "footer": { + "socials": { + "bluesky": "https://bsky.app/profile/jlowin.dev", + "github": "https://github.com/jlowin/fastmcp", + "x": "https://x.com/jlowin" + } + }, + "integrations": { + "ga4": { + "measurementId": "G-64R5W1TJXG" + } + }, + "interaction": { + "drilldown": false + }, + "logo": { + "dark": "/assets/brand/wordmark-white.png", + "light": "/assets/brand/wordmark.png" + }, + "name": "FastMCP", + "navbar": { + "links": [ + { + "href": "https://fastmcp.cloud", + "icon": "cloud", + "label": "FastMCP Cloud" + } + ], + "primary": { + "href": "https://github.com/jlowin/fastmcp", + "type": "github" + } + }, + "navigation": { + "tabs": [ + { + "groups": [ + { + "group": "Get Started", + "pages": [ + "getting-started/welcome", + "getting-started/installation", + "getting-started/quickstart" + ] + }, + { + "group": "Servers", + "pages": [ + "servers/server", + { + "group": "Core Components", + "icon": "toolbox", + "pages": [ + "servers/tools", + "servers/resources", + "servers/prompts" + ] + }, + { + "group": "Advanced Features", + "icon": "stars", + "pages": [ + "servers/context", + "servers/proxy", + "servers/composition", + "servers/elicitation", + "servers/logging", + "servers/progress", + "servers/sampling", + "servers/middleware" + ] + }, + { + "group": "Authentication", + "icon": "shield-check", + "pages": [ + "servers/auth/authentication", + "servers/auth/token-verification", + "servers/auth/remote-oauth", + "servers/auth/oauth-proxy", + "servers/auth/oidc-proxy", + "servers/auth/full-oauth-server" + ] + }, + { + "group": "Deployment", + "icon": "rocket", + "pages": [ + "deployment/running-server", + "deployment/server-configuration", + "deployment/self-hosted", + "deployment/fastmcp-cloud" + ] + } + ] + }, + { + "group": "Clients", + "pages": [ + { + "group": "Essentials", + "icon": "cube", + "pages": [ + "clients/client", + "clients/transports" + ] + }, + { + "group": "Core Operations", + "icon": "handshake", + "pages": [ + "clients/tools", + "clients/resources", + "clients/prompts" + ] + }, + { + "group": "Advanced Features", + "icon": "stars", + "pages": [ + "clients/elicitation", + "clients/logging", + "clients/progress", + "clients/sampling", + "clients/messages", + "clients/roots" + ] + }, + { + "group": "Authentication", + "icon": "user-shield", + "pages": [ + "clients/auth/oauth", + "clients/auth/bearer" + ] + } + ] + }, + { + "group": "Integrations", + "pages": [ + { + "group": "Authentication", + "icon": "key", + "pages": [ + "integrations/auth0", + "integrations/authkit", + "integrations/aws-cognito", + "integrations/azure", + "integrations/descope", + "integrations/github", + "integrations/scalekit", + "integrations/google", + "integrations/workos" + ] + }, + { + "group": "Authorization", + "icon": "shield-check", + "pages": [ + "integrations/eunomia-authorization", + "integrations/permit" + ] + }, + { + "group": "AI Assistants", + "icon": "robot", + "pages": [ + "integrations/chatgpt", + "integrations/claude-code", + "integrations/claude-desktop", + "integrations/cursor", + "integrations/gemini-cli", + "integrations/mcp-json-configuration" + ] + }, + { + "group": "AI SDKs", + "icon": "code", + "pages": [ + "integrations/anthropic", + "integrations/gemini", + "integrations/openai" + ] + }, + { + "group": "Web Frameworks", + "icon": "globe", + "pages": [ + "integrations/asgi", + "integrations/fastapi", + "integrations/openapi" + ] + } + ] + }, + { + "group": "Patterns", + "pages": [ + "patterns/tool-transformation", + "patterns/decorating-methods", + "patterns/cli", + "patterns/contrib" + ] + }, + { + "group": "Development", + "pages": [ + "development/contributing", + "development/tests", + "development/releases" + ] + } + ], + "tab": "Documentation" + }, + { + "pages": [ + "updates", + "changelog" + ], + "tab": "What's New" + }, + { + "anchors": [ + { + "anchor": "Python SDK", + "icon": "python", + "pages": [ + "python-sdk/fastmcp-exceptions", + "python-sdk/fastmcp-mcp_config", + "python-sdk/fastmcp-settings", + { + "group": "fastmcp.cli", + "pages": [ + "python-sdk/fastmcp-cli-__init__", + "python-sdk/fastmcp-cli-claude", + "python-sdk/fastmcp-cli-cli", + { + "group": "install", + "pages": [ + "python-sdk/fastmcp-cli-install-__init__", + "python-sdk/fastmcp-cli-install-claude_code", + "python-sdk/fastmcp-cli-install-claude_desktop", + "python-sdk/fastmcp-cli-install-cursor", + "python-sdk/fastmcp-cli-install-gemini_cli", + "python-sdk/fastmcp-cli-install-mcp_json", + "python-sdk/fastmcp-cli-install-shared" + ] + }, + "python-sdk/fastmcp-cli-run" + ] + }, + { + "group": "fastmcp.client", + "pages": [ + "python-sdk/fastmcp-client-__init__", + { + "group": "auth", + "pages": [ + "python-sdk/fastmcp-client-auth-__init__", + "python-sdk/fastmcp-client-auth-bearer", + "python-sdk/fastmcp-client-auth-oauth" + ] + }, + "python-sdk/fastmcp-client-client", + "python-sdk/fastmcp-client-elicitation", + "python-sdk/fastmcp-client-logging", + "python-sdk/fastmcp-client-messages", + "python-sdk/fastmcp-client-oauth_callback", + "python-sdk/fastmcp-client-progress", + "python-sdk/fastmcp-client-roots", + "python-sdk/fastmcp-client-sampling", + "python-sdk/fastmcp-client-transports" + ] + }, + { + "group": "fastmcp.prompts", + "pages": [ + "python-sdk/fastmcp-prompts-__init__", + "python-sdk/fastmcp-prompts-prompt", + "python-sdk/fastmcp-prompts-prompt_manager" + ] + }, + { + "group": "fastmcp.resources", + "pages": [ + "python-sdk/fastmcp-resources-__init__", + "python-sdk/fastmcp-resources-resource", + "python-sdk/fastmcp-resources-resource_manager", + "python-sdk/fastmcp-resources-template", + "python-sdk/fastmcp-resources-types" + ] + }, + { + "group": "fastmcp.server", + "pages": [ + "python-sdk/fastmcp-server-__init__", + { + "group": "auth", + "pages": [ + "python-sdk/fastmcp-server-auth-__init__", + "python-sdk/fastmcp-server-auth-auth", + "python-sdk/fastmcp-server-auth-oauth_proxy", + "python-sdk/fastmcp-server-auth-oidc_proxy", + { + "group": "providers", + "pages": [ + "python-sdk/fastmcp-server-auth-providers-__init__", + "python-sdk/fastmcp-server-auth-providers-auth0", + "python-sdk/fastmcp-server-auth-providers-aws", + "python-sdk/fastmcp-server-auth-providers-azure", + "python-sdk/fastmcp-server-auth-providers-bearer", + "python-sdk/fastmcp-server-auth-providers-descope", + "python-sdk/fastmcp-server-auth-providers-github", + "python-sdk/fastmcp-server-auth-providers-google", + "python-sdk/fastmcp-server-auth-providers-in_memory", + "python-sdk/fastmcp-server-auth-providers-jwt", + "python-sdk/fastmcp-server-auth-providers-scalekit", + "python-sdk/fastmcp-server-auth-providers-workos" + ] + }, + "python-sdk/fastmcp-server-auth-redirect_validation" + ] + }, + "python-sdk/fastmcp-server-context", + "python-sdk/fastmcp-server-dependencies", + "python-sdk/fastmcp-server-elicitation", + "python-sdk/fastmcp-server-http", + "python-sdk/fastmcp-server-low_level", + { + "group": "middleware", + "pages": [ + "python-sdk/fastmcp-server-middleware-__init__", + "python-sdk/fastmcp-server-middleware-error_handling", + "python-sdk/fastmcp-server-middleware-logging", + "python-sdk/fastmcp-server-middleware-middleware", + "python-sdk/fastmcp-server-middleware-rate_limiting", + "python-sdk/fastmcp-server-middleware-timing" + ] + }, + "python-sdk/fastmcp-server-openapi", + "python-sdk/fastmcp-server-proxy", + "python-sdk/fastmcp-server-server" + ] + }, + { + "group": "fastmcp.tools", + "pages": [ + "python-sdk/fastmcp-tools-__init__", + "python-sdk/fastmcp-tools-tool", + "python-sdk/fastmcp-tools-tool_manager", + "python-sdk/fastmcp-tools-tool_transform" + ] + }, + { + "group": "fastmcp.utilities", + "pages": [ + "python-sdk/fastmcp-utilities-__init__", + "python-sdk/fastmcp-utilities-auth", + "python-sdk/fastmcp-utilities-cli", + "python-sdk/fastmcp-utilities-components", + "python-sdk/fastmcp-utilities-exceptions", + "python-sdk/fastmcp-utilities-http", + "python-sdk/fastmcp-utilities-inspect", + "python-sdk/fastmcp-utilities-json_schema", + "python-sdk/fastmcp-utilities-json_schema_type", + "python-sdk/fastmcp-utilities-logging", + "python-sdk/fastmcp-utilities-mcp_config", + { + "group": "mcp_server_config", + "pages": [ + "python-sdk/fastmcp-utilities-mcp_server_config-__init__", + { + "group": "v1", + "pages": [ + "python-sdk/fastmcp-utilities-mcp_server_config-v1-__init__", + { + "group": "environments", + "pages": [ + "python-sdk/fastmcp-utilities-mcp_server_config-v1-environments-__init__", + "python-sdk/fastmcp-utilities-mcp_server_config-v1-environments-base", + "python-sdk/fastmcp-utilities-mcp_server_config-v1-environments-uv" + ] + }, + "python-sdk/fastmcp-utilities-mcp_server_config-v1-mcp_server_config", + { + "group": "sources", + "pages": [ + "python-sdk/fastmcp-utilities-mcp_server_config-v1-sources-__init__", + "python-sdk/fastmcp-utilities-mcp_server_config-v1-sources-base", + "python-sdk/fastmcp-utilities-mcp_server_config-v1-sources-filesystem" + ] + } + ] + } + ] + }, + "python-sdk/fastmcp-utilities-openapi", + "python-sdk/fastmcp-utilities-storage", + "python-sdk/fastmcp-utilities-tests", + "python-sdk/fastmcp-utilities-types" + ] + } + ] + } + ], + "tab": "SDK Reference" + } + ] + }, + "redirects": [ + { + "destination": "/servers/proxy", + "source": "/patterns/proxy" + }, + { + "destination": "/servers/composition", + "source": "/patterns/composition" + } + ], + "search": { + "prompt": "Search the docs..." + }, + "theme": "mint", + "thumbnails": { + "appearance": "light", + "background": "/assets/brand/thumbnail-background.png" + } +} diff --git a/docs/getting-started/installation.mdx b/docs/getting-started/installation.mdx new file mode 100644 index 000000000..8da0df91c --- /dev/null +++ b/docs/getting-started/installation.mdx @@ -0,0 +1,82 @@ +--- +title: Installation +icon: arrow-down-to-line +--- +## Install FastMCP + +We recommend using [uv](https://docs.astral.sh/uv/getting-started/installation/) to install and manage FastMCP. + +If you plan to use FastMCP in your project, you can add it as a dependency with: + +```bash +uv add fastmcp +``` + +Alternatively, you can install it directly with `pip` or `uv pip`: + + ```bash uv + uv pip install fastmcp + ``` + + ```bash pip + pip install fastmcp + ``` + + +### Verify Installation + +To verify that FastMCP is installed correctly, you can run the following command: + +```bash +fastmcp version +``` + +You should see output like the following: + +```bash +$ fastmcp version + +FastMCP version: 2.11.3 +MCP version: 1.12.4 +Python version: 3.12.2 +Platform: macOS-15.3.1-arm64-arm-64bit +FastMCP root path: ~/Developer/fastmcp +``` +## Upgrading from the Official MCP SDK + +Upgrading from the official MCP SDK's FastMCP 1.0 to FastMCP 2.0 is generally straightforward. The core server API is highly compatible, and in many cases, changing your import statement from `from mcp.server.fastmcp import FastMCP` to `from fastmcp import FastMCP` will be sufficient. + + +```python {5} +# Before +# from mcp.server.fastmcp import FastMCP + +# After +from fastmcp import FastMCP + +mcp = FastMCP("My MCP Server") +``` + + +Prior to `fastmcp==2.3.0` and `mcp==1.8.0`, the 2.x API always mirrored the official 1.0 API. However, as the projects diverge, this can not be guaranteed. You may see deprecation warnings if you attempt to use 1.0 APIs in FastMCP 2.x. Please refer to this documentation for details on new capabilities. + + +## Versioning Policy + +FastMCP follows semantic versioning with pragmatic adaptations for the rapidly evolving MCP ecosystem. Breaking changes may occur in minor versions (e.g., 2.3.x to 2.4.0) when necessary to stay current with the MCP Protocol. + +For production use, always pin to exact versions: +``` +fastmcp==2.11.0 # Good +fastmcp>=2.11.0 # Bad - will install breaking changes +``` + +See the full [versioning and release policy](/development/releases#versioning-policy) for details on our public API, deprecation practices, and breaking change philosophy. + +## Contributing to FastMCP + +Interested in contributing to FastMCP? See the [Contributing Guide](/development/contributing) for details on: +- Setting up your development environment +- Running tests and pre-commit hooks +- Submitting issues and pull requests +- Code standards and review process diff --git a/docs/getting-started/quickstart.mdx b/docs/getting-started/quickstart.mdx new file mode 100644 index 000000000..1c5c2330e --- /dev/null +++ b/docs/getting-started/quickstart.mdx @@ -0,0 +1,136 @@ +--- +title: Quickstart +icon: rocket-launch +--- + +Welcome! This guide will help you quickly set up FastMCP, run your first MCP server, and deploy a server to FastMCP Cloud. + +If you haven't already installed FastMCP, follow the [installation instructions](/getting-started/installation). + +## Create a FastMCP Server + +A FastMCP server is a collection of tools, resources, and other MCP components. To create a server, start by instantiating the `FastMCP` class. + +Create a new file called `my_server.py` and add the following code: + +```python my_server.py +from fastmcp import FastMCP + +mcp = FastMCP("My MCP Server") +``` + + +That's it! You've created a FastMCP server, albeit a very boring one. Let's add a tool to make it more interesting. + + +## Add a Tool + +To add a tool that returns a simple greeting, write a function and decorate it with `@mcp.tool` to register it with the server: + +```python my_server.py {5-7} +from fastmcp import FastMCP + +mcp = FastMCP("My MCP Server") + +@mcp.tool +def greet(name: str) -> str: + return f"Hello, {name}!" +``` + + +## Run the Server + +The simplest way to run your FastMCP server is to call its `run()` method. You can choose between different transports, like `stdio` for local servers, or `http` for remote access: + + + +```python my_server.py (stdio) {9, 10} +from fastmcp import FastMCP + +mcp = FastMCP("My MCP Server") + +@mcp.tool +def greet(name: str) -> str: + return f"Hello, {name}!" + +if __name__ == "__main__": + mcp.run() +``` + +```python my_server.py (HTTP) {9, 10} +from fastmcp import FastMCP + +mcp = FastMCP("My MCP Server") + +@mcp.tool +def greet(name: str) -> str: + return f"Hello, {name}!" + +if __name__ == "__main__": + mcp.run(transport="http", port=8000) +``` + + + +This lets us run the server with `python my_server.py`. The stdio transport is the traditional way to connect MCP servers to clients, while the HTTP transport enables remote connections. + + +Why do we need the `if __name__ == "__main__":` block? + +The `__main__` block is recommended for consistency and compatibility, ensuring your server works with all MCP clients that execute your server file as a script. Users who will exclusively run their server with the FastMCP CLI can omit it, as the CLI imports the server object directly. + + +### Using the FastMCP CLI + +You can also use the `fastmcp run` command to start your server. Note that the FastMCP CLI **does not** execute the `__main__` block of your server file. Instead, it imports your server object and runs it with whatever transport and options you provide. + +For example, to run this server with the default stdio transport (no matter how you called `mcp.run()`), you can use the following command: +```bash +fastmcp run my_server.py:mcp +``` + +To run this server with the HTTP transport, you can use the following command: +```bash +fastmcp run my_server.py:mcp --transport http --port 8000 +``` + +## Call Your Server + +Once your server is running with HTTP transport, you can connect to it with a FastMCP client or any LLM client that supports the MCP protocol: + +```python my_client.py +import asyncio +from fastmcp import Client + +client = Client("http://localhost:8000/mcp") + +async def call_tool(name: str): + async with client: + result = await client.call_tool("greet", {"name": name}) + print(result) + +asyncio.run(call_tool("Ford")) +``` + +Note that: +- FastMCP clients are asynchronous, so we need to use `asyncio.run` to run the client +- We must enter a client context (`async with client:`) before using the client +- You can make multiple client calls within the same context + +## Deploy to FastMCP Cloud + +[FastMCP Cloud](https://fastmcp.cloud) is a hosting service run by the FastMCP team at [Prefect](https://www.prefect.io/fastmcp). It is optimized to deploy authenticated FastMCP servers as quickly as possible, giving you a secure URL that you can plug into any LLM client. + + +FastMCP Cloud is **free for personal servers** and offers simple pay-as-you-go pricing for teams. + + +To deploy your server, you'll need a [GitHub account](https://github.com). Once you have one, you can deploy your server in three steps: + +1. Push your `my_server.py` file to a GitHub repository +2. Sign in to [FastMCP Cloud](https://fastmcp.cloud) with your GitHub account +3. Create a new project from your repository and enter `my_server.py:mcp` as the server entrypoint + +That's it! FastMCP Cloud will build and deploy your server, making it available at a URL like `https://your-project.fastmcp.app/mcp`. You can chat with it to test its functionality, or connect to it from any LLM client that supports the MCP protocol. + +For more details, see the [FastMCP Cloud guide](/deployment/fastmcp-cloud). diff --git a/docs/getting-started/welcome.mdx b/docs/getting-started/welcome.mdx new file mode 100644 index 000000000..bc03f0fb1 --- /dev/null +++ b/docs/getting-started/welcome.mdx @@ -0,0 +1,110 @@ +--- +title: "Welcome to FastMCP 2.0!" +sidebarTitle: "Welcome!" +description: The fast, Pythonic way to build MCP servers and clients. +icon: hand-wave +--- +'F' logo on a watercolor background +'F' logo on a watercolor background + + +**FastMCP is the standard framework for building MCP applications.** The [Model Context Protocol](https://modelcontextprotocol.io/) (MCP) provides a standardized way to connect LLMs to tools and data, and FastMCP makes it production-ready with clean, Pythonic code: + +```python {1} +from fastmcp import FastMCP + +mcp = FastMCP("Demo ๐Ÿš€") + +@mcp.tool +def add(a: int, b: int) -> int: + """Add two numbers""" + return a + b + +if __name__ == "__main__": + mcp.run() +``` + + +## Beyond Basic MCP + +FastMCP pioneered Python MCP development, with FastMCP 1.0 being incorporated into the [official MCP SDK](https://github.com/modelcontextprotocol/python-sdk) in 2024. + +**This is FastMCP 2.0** โ€” the actively maintained version that extends far beyond basic protocol implementation. While the SDK provides core functionality, FastMCP 2.0 delivers everything needed for production: advanced MCP patterns (server composition, proxying, OpenAPI/FastAPI generation, tool transformation), enterprise auth (Google, GitHub, Azure, Auth0, WorkOS, and more), deployment tools, testing frameworks, and comprehensive client libraries. + +Ready to build? Start with our [installation guide](/getting-started/installation) or jump straight to the [quickstart](/getting-started/quickstart). + + +## What is MCP? + +The Model Context Protocol lets you build servers that expose data and functionality to LLM applications in a secure, standardized way. It is often described as "the USB-C port for AI", providing a uniform way to connect LLMs to resources they can use. It may be easier to think of it as an API, but specifically designed for LLM interactions. MCP servers can: + +- Expose data through `Resources` (think of these sort of like GET endpoints; they are used to load information into the LLM's context) +- Provide functionality through `Tools` (sort of like POST endpoints; they are used to execute code or otherwise produce a side effect) +- Define interaction patterns through `Prompts` (reusable templates for LLM interactions) +- And more! + +FastMCP provides a high-level, Pythonic interface for building, managing, and interacting with these servers. + +## Why FastMCP? + +FastMCP handles all the complex protocol details so you can focus on building. In most cases, decorating a Python function is all you need โ€” FastMCP handles the rest. + +๐Ÿš€ **Fast**: High-level interface means less code and faster development + +๐Ÿ€ **Simple**: Build MCP servers with minimal boilerplate + +๐Ÿ **Pythonic**: Feels natural to Python developers + +๐Ÿ” **Complete**: Everything for production โ€” enterprise auth (Google, GitHub, Azure, Auth0, WorkOS), deployment tools, testing frameworks, client libraries, and more + +FastMCP provides the shortest path from idea to production. Deploy locally, to the cloud with [FastMCP Cloud](https://fastmcp.cloud) (free for personal servers), or to your own infrastructure. + +FastMCP is made with ๐Ÿ’™ by [Prefect](https://www.prefect.io/). + + + +## LLM-Friendly Docs + +The FastMCP documentation is available in multiple LLM-friendly formats: + +### MCP Server + +The FastMCP docs are accessible via MCP! The server URL is `https://gofastmcp.com/mcp`. + +In fact, you can use FastMCP to search the FastMCP docs: + +```python +import asyncio +from fastmcp import Client + +async def main(): + async with Client("https://gofastmcp.com/mcp") as client: + result = await client.call_tool( + name="SearchFastMcp", + arguments={"query": "deploy a FastMCP server"} + ) + print(result) + +asyncio.run(main()) +``` + +### Plain Text Formats + +The docs are also available in [llms.txt format](https://llmstxt.org/): +- [llms.txt](https://gofastmcp.com/llms.txt) - A sitemap listing all documentation pages +- [llms-full.txt](https://gofastmcp.com/llms-full.txt) - The entire documentation in one file (may exceed context windows) + +Any page can be accessed as markdown by appending `.md` to the URL. For example, this page becomes `https://gofastmcp.com/getting-started/welcome.md`. + +You can also copy any page as markdown by pressing "Cmd+C" (or "Ctrl+C" on Windows) on your keyboard. diff --git a/docs/integrations/anthropic.mdx b/docs/integrations/anthropic.mdx new file mode 100644 index 000000000..b8156ac95 --- /dev/null +++ b/docs/integrations/anthropic.mdx @@ -0,0 +1,228 @@ +--- +title: Anthropic API ๐Ÿค FastMCP +sidebarTitle: Anthropic API +description: Connect FastMCP servers to the Anthropic API +icon: message-code +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + + +Anthropic's [Messages API](https://docs.anthropic.com/en/api/messages) supports MCP servers as remote tool sources. This tutorial will show you how to create a FastMCP server and deploy it to a public URL, then how to call it from the Messages API. + + +Currently, the MCP connector only accesses **tools** from MCP serversโ€”it queries the `list_tools` endpoint and exposes those functions to Claude. Other MCP features like resources and prompts are not currently supported. You can read more about the MCP connector in the [Anthropic documentation](https://docs.anthropic.com/en/docs/agents-and-tools/mcp-connector). + + +## Create a Server + +First, create a FastMCP server with the tools you want to expose. For this example, we'll create a server with a single tool that rolls dice. + +```python server.py +import random +from fastmcp import FastMCP + +mcp = FastMCP(name="Dice Roller") + +@mcp.tool +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)] + +if __name__ == "__main__": + mcp.run(transport="http", port=8000) +``` + +## Deploy the Server + +Your server must be deployed to a public URL in order for Anthropic to access it. The MCP connector supports both SSE and Streamable HTTP transports. + +For development, you can use tools like `ngrok` to temporarily expose a locally-running server to the internet. We'll do that for this example (you may need to install `ngrok` and create a free account), but you can use any other method to deploy your server. + +Assuming you saved the above code as `server.py`, you can run the following two commands in two separate terminals to deploy your server and expose it to the internet: + + +```bash FastMCP server +python server.py +``` + +```bash ngrok +ngrok http 8000 +``` + + + +This exposes your unauthenticated server to the internet. Only run this command in a safe environment if you understand the risks. + + +## Call the Server + +To use the Messages API with MCP servers, you'll need to install the Anthropic Python SDK (not included with FastMCP): + +```bash +pip install anthropic +``` + +You'll also need to authenticate with Anthropic. You can do this by setting the `ANTHROPIC_API_KEY` environment variable. Consult the Anthropic SDK documentation for more information. + +```bash +export ANTHROPIC_API_KEY="your-api-key" +``` + +Here is an example of how to call your server from Python. Note that you'll need to replace `https://your-server-url.com` with the actual URL of your server. In addition, we use `/mcp/` as the endpoint because we deployed a streamable-HTTP server with the default path; you may need to use a different endpoint if you customized your server's deployment. **At this time you must also include the `extra_headers` parameter with the `anthropic-beta` header.** + +```python {5, 13-22} +import anthropic +from rich import print + +# Your server URL (replace with your actual URL) +url = 'https://your-server-url.com' + +client = anthropic.Anthropic() + +response = client.beta.messages.create( + model="claude-sonnet-4-20250514", + max_tokens=1000, + messages=[{"role": "user", "content": "Roll a few dice!"}], + mcp_servers=[ + { + "type": "url", + "url": f"{url}/mcp/", + "name": "dice-server", + } + ], + extra_headers={ + "anthropic-beta": "mcp-client-2025-04-04" + } +) + +print(response.content) +``` + +If you run this code, you'll see something like the following output: + +```text +I'll roll some dice for you! Let me use the dice rolling tool. + +I rolled 3 dice and got: 4, 2, 6 + +The results were 4, 2, and 6. Would you like me to roll again or roll a different number of dice? +``` + + +## Authentication + + + +The MCP connector supports OAuth authentication through authorization tokens, which means you can secure your server while still allowing Anthropic to access it. + +### Server Authentication + +The simplest way to add authentication to the server is to use a bearer token scheme. + +For this example, we'll quickly generate our own tokens with FastMCP's `RSAKeyPair` utility, but this may not be appropriate for production use. For more details, see the complete server-side [Token Verification](/servers/auth/token-verification) documentation. + +We'll start by creating an RSA key pair to sign and verify tokens. + +```python +from fastmcp.server.auth.providers.jwt import RSAKeyPair + +key_pair = RSAKeyPair.generate() +access_token = key_pair.create_token(audience="dice-server") +``` + + +FastMCP's `RSAKeyPair` utility is for development and testing only. + + +Next, we'll create a `JWTVerifier` to authenticate the server. + +```python +from fastmcp import FastMCP +from fastmcp.server.auth import JWTVerifier + +auth = JWTVerifier( + public_key=key_pair.public_key, + audience="dice-server", +) + +mcp = FastMCP(name="Dice Roller", auth=auth) +``` + +Here is a complete example that you can copy/paste. For simplicity and the purposes of this example only, it will print the token to the console. **Do NOT do this in production!** + +```python server.py [expandable] +from fastmcp import FastMCP +from fastmcp.server.auth import JWTVerifier +from fastmcp.server.auth.providers.jwt import RSAKeyPair +import random + +key_pair = RSAKeyPair.generate() +access_token = key_pair.create_token(audience="dice-server") + +auth = JWTVerifier( + public_key=key_pair.public_key, + audience="dice-server", +) + +mcp = FastMCP(name="Dice Roller", auth=auth) + +@mcp.tool +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)] + +if __name__ == "__main__": + print(f"\n---\n\n๐Ÿ”‘ Dice Roller access token:\n\n{access_token}\n\n---\n") + mcp.run(transport="http", port=8000) +``` + +### Client Authentication + +If you try to call the authenticated server with the same Anthropic code we wrote earlier, you'll get an error indicating that the server rejected the request because it's not authenticated. + +```python +Error code: 400 - { + "type": "error", + "error": { + "type": "invalid_request_error", + "message": "MCP server 'dice-server' requires authentication. Please provide an authorization_token.", + }, +} +``` + +To authenticate the client, you can pass the token using the `authorization_token` parameter in your MCP server configuration: + +```python {8, 21} +import anthropic +from rich import print + +# Your server URL (replace with your actual URL) +url = 'https://your-server-url.com' + +# Your access token (replace with your actual token) +access_token = 'your-access-token' + +client = anthropic.Anthropic() + +response = client.beta.messages.create( + model="claude-sonnet-4-20250514", + max_tokens=1000, + messages=[{"role": "user", "content": "Roll a few dice!"}], + mcp_servers=[ + { + "type": "url", + "url": f"{url}/mcp/", + "name": "dice-server", + "authorization_token": access_token + } + ], + extra_headers={ + "anthropic-beta": "mcp-client-2025-04-04" + } +) + +print(response.content) +``` + +You should now see the dice roll results in the output. diff --git a/docs/integrations/asgi.mdx b/docs/integrations/asgi.mdx new file mode 100644 index 000000000..66e7e4055 --- /dev/null +++ b/docs/integrations/asgi.mdx @@ -0,0 +1,213 @@ +--- +title: ASGI / Starlette ๐Ÿค FastMCP +sidebarTitle: ASGI / Starlette +description: Integrate FastMCP servers into ASGI applications +icon: server +--- + +import { VersionBadge } from '/snippets/version-badge.mdx' + + + +FastMCP servers can be integrated into existing ASGI applications, allowing you to add MCP functionality to your web applications. This is useful for: + +- Adding MCP functionality to an existing website or API +- Mounting MCP servers under specific URL paths +- Combining multiple services in a single application +- Leveraging existing authentication and middleware + +## Basic Usage + +To integrate a FastMCP server into an ASGI application, use the `http_app()` method to obtain a Starlette application instance: + + +The `http_app()` method is new in FastMCP 2.3.2. In older versions, use `sse_app()` for SSE transport or `streamable_http_app()` for Streamable HTTP transport. + + +```python +from fastmcp import FastMCP + +mcp = FastMCP("MyServer") + +@mcp.tool +def hello(name: str) -> str: + return f"Hello, {name}!" + +# Get a Starlette app instance for Streamable HTTP transport (recommended) +http_app = mcp.http_app() + +# For legacy SSE transport (deprecated) +sse_app = mcp.http_app(transport="sse") +``` + +The returned Starlette application can be integrated with other ASGI-compatible web frameworks. The MCP server's endpoint is mounted at `/mcp/` for Streamable HTTP transport and `/sse/` for SSE transport. + +### Configuration Options + +You can customize the endpoint path and access the FastMCP server instance: + +```python +# Custom endpoint path +http_app = mcp.http_app(path="/custom-mcp-path") + +# Access the FastMCP server from middleware/routes +# The server is available at: request.app.state.fastmcp_server +``` + +### Adding Custom Routes + +You can add custom web routes directly to your FastMCP server using the `@custom_route` decorator: + +```python +from fastmcp import FastMCP +from starlette.requests import Request +from starlette.responses import JSONResponse + +mcp = FastMCP("MyServer") + +@mcp.custom_route("/api/status", methods=["GET"]) +async def get_status(request: Request): + return JSONResponse({"server": "running"}) + +http_app = mcp.http_app() +``` + +#### Health Check Endpoints + +Health checks are commonly needed for monitoring and load balancing: + +```python +from fastmcp import FastMCP +from starlette.requests import Request +from starlette.responses import JSONResponse + +mcp = FastMCP("MyServer") + +@mcp.custom_route("/health", methods=["GET"]) +async def health_check(request: Request): + return JSONResponse({"status": "healthy"}) + +http_app = mcp.http_app() +``` + +The health endpoint will be available at `/health` alongside your MCP endpoint at `/mcp/`. + +## Starlette Integration + +Mount your FastMCP server in another Starlette application: + +```python +from fastmcp import FastMCP +from starlette.applications import Starlette +from starlette.routing import Mount + +# Create your FastMCP server +mcp = FastMCP("MyServer") + +@mcp.tool +def analyze(data: str) -> dict: + return {"result": f"Analyzed: {data}"} + +# Create the ASGI app +mcp_app = mcp.http_app(path='/mcp') + +# Create a Starlette app and mount the MCP server +app = Starlette( + routes=[ + Mount("/mcp-server", app=mcp_app), + # Add other routes as needed + ], + lifespan=mcp_app.lifespan, +) +``` + +The MCP endpoint will be available at `/mcp-server/mcp/` of the resulting Starlette app. + + +For Streamable HTTP transport, you **must** pass the lifespan context from the FastMCP app to the resulting Starlette app, as nested lifespans are not recognized. Otherwise, the FastMCP server's session manager will not be properly initialized. + + +### Nested Mounts + +You can create complex routing structures by nesting mounts: + +```python +from fastmcp import FastMCP +from starlette.applications import Starlette +from starlette.routing import Mount + +# Create your FastMCP server +mcp = FastMCP("MyServer") + +# Create the ASGI app +mcp_app = mcp.http_app(path='/mcp') + +# Create nested application structure +inner_app = Starlette(routes=[Mount("/inner", app=mcp_app)]) +app = Starlette( + routes=[Mount("/outer", app=inner_app)], + lifespan=mcp_app.lifespan, +) +``` + +In this setup, the MCP server is accessible at the `/outer/inner/mcp/` path. + +## Custom Middleware + + + +Add custom Starlette middleware to your FastMCP ASGI apps by passing a list of middleware instances: + +```python +from fastmcp import FastMCP +from starlette.middleware import Middleware +from starlette.middleware.cors import CORSMiddleware + +# Create your FastMCP server +mcp = FastMCP("MyServer") + +# Define custom middleware +custom_middleware = [ + Middleware( + CORSMiddleware, + allow_origins=["*"], + allow_methods=["*"], + allow_headers=["*"], + ) +] + +# Create ASGI app with middleware +http_app = mcp.http_app(custom_middleware=custom_middleware) +``` + +## Running the Server + +To run your ASGI application, use an ASGI server like `uvicorn`: + +```python +import uvicorn + +if __name__ == "__main__": + uvicorn.run(app, host="0.0.0.0", port=8000) +``` + +Or from the command line: + +```bash +uvicorn path.to.your.app:app --host 0.0.0.0 --port 8000 +``` + +## Framework-Specific Integration + +### FastAPI + +For FastAPI-specific integration patterns including both mounting MCP servers into FastAPI apps and generating MCP servers from FastAPI apps, see the [FastAPI Integration guide](/integrations/fastapi). + +### Other ASGI Frameworks + +The patterns shown here work with any ASGI-compatible framework. The key requirements are: + +1. Mount the FastMCP ASGI app at your desired path +2. Pass the lifespan context to your root application +3. Configure any necessary middleware or authentication + diff --git a/docs/integrations/auth0.mdx b/docs/integrations/auth0.mdx new file mode 100644 index 000000000..0c8b9ff48 --- /dev/null +++ b/docs/integrations/auth0.mdx @@ -0,0 +1,231 @@ +--- +title: Auth0 OAuth ๐Ÿค FastMCP +sidebarTitle: Auth0 +description: Secure your FastMCP server with Auth0 OAuth +icon: shield-check +tag: NEW +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + + + +This guide shows you how to secure your FastMCP server using **Auth0 OAuth**. While Auth0 does have support for Dynamic Client Registration, it is not enabled by default so this integration uses the [**OIDC Proxy**](/servers/auth/oidc-proxy) pattern to bridge Auth0's dynamic OIDC configuration with MCP's authentication requirements. + +## Configuration + +### Prerequisites + +Before you begin, you will need: +1. An **[Auth0 Account](https://auth0.com/)** with access to create Applications +2. Your FastMCP server's URL (can be localhost for development, e.g., `http://localhost:8000`) + +### Step 1: Create an Auth0 Application + +Create an Application in your Auth0 settings to get the credentials needed for authentication: + + + + Go to **Applications โ†’ Applications** in your Auth0 account. + + Click **"+ Create Application"** to create a new application. + + + + - **Name**: Choose a name users will recognize (e.g., "My FastMCP Server") + - **Choose an application type**: Choose "Single Page Web Applications" + - Click **Create** to create the application + + + + Select the "Settings" tab for your application, then find the "Application URIs" section. + + - **Allowed Callback URLs**: Your server URL + `/auth/callback` (e.g., `http://localhost:8000/auth/callback`) + - Click **Save** to save your changes + + + The callback URL must match exactly. The default path is `/auth/callback`, but you can customize it using the `redirect_path` parameter. + + + + If you want to use a custom callback path (e.g., `/auth/auth0/callback`), make sure to set the same path in both your Auth0 Application settings and the `redirect_path` parameter when configuring the Auth0Provider. + + + + + After creating the app, in the "Basic Information" section you'll see: + + - **Client ID**: A public identifier like `tv2ObNgaZAWWhhycr7Bz1LU2mxlnsmsB` + - **Client Secret**: A private hidden value that should always be stored securely + + + Store these credentials securely. Never commit them to version control. Use environment variables or a secrets manager in production. + + + + + Go to **Applications โ†’ APIs** in your Auth0 account. + + - Find the API that you want to use for your application + - **API Audience**: A URL that uniquely identifies the API + + + Store this along with of the credentials above. Never commit this to version control. Use environment variables or a secrets manager in production. + + + + +### Step 2: FastMCP Configuration + +Create your FastMCP server using the `Auth0Provider`. + +```python server.py +from fastmcp import FastMCP +from fastmcp.server.auth.providers.auth0 import Auth0Provider + +# The Auth0Provider utilizes Auth0 OIDC configuration +auth_provider = Auth0Provider( + config_url="https://.../.well-known/openid-configuration", # Your Auth0 configuration URL + client_id="tv2ObNgaZAWWhhycr7Bz1LU2mxlnsmsB", # Your Auth0 application Client ID + client_secret="vPYqbjemq...", # Your Auth0 application Client Secret + audience="https://...", # Your Auth0 API audience + base_url="http://localhost:8000", # Must match your application configuration + # redirect_path="/auth/callback" # Default value, customize if needed +) + +mcp = FastMCP(name="Auth0 Secured App", auth=auth_provider) + +# Add a protected tool to test authentication +@mcp.tool +async def get_token_info() -> dict: + """Returns information about the Auth0 token.""" + from fastmcp.server.dependencies import get_access_token + + token = get_access_token() + + return { + "issuer": token.claims.get("iss"), + "audience": token.claims.get("aud"), + "scope": token.claims.get("scope") + } +``` + +## Testing + +### Running the Server + +Start your FastMCP server with HTTP transport to enable OAuth flows: + +```bash +fastmcp run server.py --transport http --port 8000 +``` + +Your server is now running and protected by Auth0 authentication. + +### Testing with a Client + +Create a test client that authenticates with your Auth0-protected server: + +```python test_client.py +from fastmcp import Client +import asyncio + +async def main(): + # The client will automatically handle Auth0 OAuth flows + async with Client("http://localhost:8000/mcp/", auth="oauth") as client: + # First-time connection will open Auth0 login in your browser + print("โœ“ Authenticated with Auth0!") + + # Test the protected tool + result = await client.call_tool("get_token_info") + print(f"Auth0 audience: {result['audience']}") + +if __name__ == "__main__": + asyncio.run(main()) +``` + +When you run the client for the first time: +1. Your browser will open to Auth0's authorization page +2. After you authorize the app, you'll be redirected back +3. The client receives the token and can make authenticated requests + + +The client caches tokens locally, so you won't need to re-authenticate for subsequent runs unless the token expires or you explicitly clear the cache. + + +## Environment Variables + +For production deployments, use environment variables instead of hardcoding credentials. + +### Provider Selection + +Setting this environment variable allows the Auth0 provider to be used automatically without explicitly instantiating it in code. + + + +Set to `fastmcp.server.auth.providers.auth0.Auth0Provider` to use Auth0 authentication. + + + +### Auth0-Specific Configuration + +These environment variables provide default values for the Auth0 provider, whether it's instantiated manually or configured via `FASTMCP_SERVER_AUTH`. + + + +Your Auth0 Application Configuration URL (e.g., `https://.../.well-known/openid-configuration`) + + + +Your Auth0 Application Client ID (e.g., `tv2ObNgaZAWWhhycr7Bz1LU2mxlnsmsB`) + + + +Your Auth0 Application Client Secret (e.g., `vPYqbjemq...`) + + + +Your Auth0 API Audience + + + +Public URL of your FastMCP server for OAuth callbacks + + + +Redirect path configured in your Auth0 Application + + + +Comma-, space-, or JSON-separated list of required AUth0 scopes (e.g., `openid email` or `["openid","email"]`) + + + +Example `.env` file: +```bash +# Use the Auth0 provider +FASTMCP_SERVER_AUTH=fastmcp.server.auth.providers.auth0.Auth0Provider + +# Auth0 configuration and credentials +FASTMCP_SERVER_AUTH_AUTH0_CONFIG_URL=https://.../.well-known/openid-configuration +FASTMCP_SERVER_AUTH_AUTH0_CLIENT_ID=tv2ObNgaZAWWhhycr7Bz1LU2mxlnsmsB +FASTMCP_SERVER_AUTH_AUTH0_CLIENT_SECRET=vPYqbjemq... +FASTMCP_SERVER_AUTH_AUTH0_AUDIENCE=https://... +FASTMCP_SERVER_AUTH_AUTH0_BASE_URL=https://your-server.com +FASTMCP_SERVER_AUTH_AUTH0_REQUIRED_SCOPES=openid,email +``` + +With environment variables set, your server code simplifies to: + +```python server.py +from fastmcp import FastMCP + +# Authentication is automatically configured from environment +mcp = FastMCP(name="Auth0 Secured App") + +@mcp.tool +async def search_logs() -> list[str]: + """Search the service logs.""" + # Your tool implementation here + pass +``` diff --git a/docs/integrations/authkit.mdx b/docs/integrations/authkit.mdx new file mode 100644 index 000000000..51552781d --- /dev/null +++ b/docs/integrations/authkit.mdx @@ -0,0 +1,133 @@ +--- +title: AuthKit ๐Ÿค FastMCP +sidebarTitle: AuthKit +description: Secure your FastMCP server with AuthKit by WorkOS +icon: shield-check +tag: NEW +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + + + +This guide shows you how to secure your FastMCP server using WorkOS's **AuthKit**, a complete authentication and user management solution. This integration uses the [**Remote OAuth**](/servers/auth/remote-oauth) pattern, where AuthKit handles user login and your FastMCP server validates the tokens. + + +## Configuration +### Prerequisites + +Before you begin, you will need: +1. A **[WorkOS Account](https://workos.com/)** and a new **Project**. +2. An **[AuthKit](https://www.authkit.com/)** instance configured within your WorkOS project. +3. Your FastMCP server's URL (can be localhost for development, e.g., `http://localhost:8000`). + +### Step 1: AuthKit Configuration + +In your WorkOS Dashboard, enable AuthKit and configure the following settings: + + + + Go to **Applications โ†’ Configuration** and enable **Dynamic Client Registration**. This allows MCP clients register with your application automatically. + + ![Enable Dynamic Client Registration](./images/authkit/enable_dcr.png) + + + + Find your **AuthKit Domain** on the configuration page. It will look like `https://your-project-12345.authkit.app`. You'll need this for your FastMCP server configuration. + + + +### Step 2: FastMCP Configuration + +Create your FastMCP server file and use the `AuthKitProvider` to handle all the OAuth integration automatically: + +```python server.py +from fastmcp import FastMCP +from fastmcp.server.auth.providers.workos import AuthKitProvider + +# The AuthKitProvider automatically discovers WorkOS endpoints +# and configures JWT token validation +auth_provider = AuthKitProvider( + authkit_domain="https://your-project-12345.authkit.app", + base_url="http://localhost:8000" # Use your actual server URL +) + +mcp = FastMCP(name="AuthKit Secured App", auth=auth_provider) +``` + +## Testing + +To test your server, you can use the `fastmcp` CLI to run it locally. Assuming you've saved the above code to `server.py` (after replacing the `authkit_domain` and `base_url` with your actual values!), you can run the following command: + +```bash +fastmcp run server.py --transport http --port 8000 +``` + +Now, you can use a FastMCP client to test that you can reach your server after authenticating: + +```python +from fastmcp import Client +import asyncio + +async def main(): + async with Client("http://localhost:8000/mcp/", auth="oauth") as client: + assert await client.ping() + +if __name__ == "__main__": + asyncio.run(main()) +``` + + +## Environment Variables + + + +For production deployments, use environment variables instead of hardcoding credentials. + +### Provider Selection + +Setting this environment variable allows the AuthKit provider to be used automatically without explicitly instantiating it in code. + + + +Set to `fastmcp.server.auth.providers.workos.AuthKitProvider` to use AuthKit authentication. + + + +### AuthKit-Specific Configuration + +These environment variables provide default values for the AuthKit provider, whether it's instantiated manually or configured via `FASTMCP_SERVER_AUTH`. + + + +Your AuthKit domain (e.g., `https://your-project-12345.authkit.app`) + + + +Public URL of your FastMCP server (e.g., `https://your-server.com` or `http://localhost:8000` for development) + + + +Comma-, space-, or JSON-separated list of required OAuth scopes (e.g., `openid profile email` or `["openid", "profile", "email"]`) + + + +Example `.env` file: +```bash +# Use the AuthKit provider +FASTMCP_SERVER_AUTH=fastmcp.server.auth.providers.workos.AuthKitProvider + +# AuthKit configuration +FASTMCP_SERVER_AUTH_AUTHKITPROVIDER_AUTHKIT_DOMAIN=https://your-project-12345.authkit.app +FASTMCP_SERVER_AUTH_AUTHKITPROVIDER_BASE_URL=https://your-server.com +FASTMCP_SERVER_AUTH_AUTHKITPROVIDER_REQUIRED_SCOPES=openid,profile,email +``` + +With environment variables set, your server code simplifies to: + +```python server.py +from fastmcp import FastMCP + +# Authentication is automatically configured from environment +mcp = FastMCP(name="AuthKit Secured App") +``` \ No newline at end of file diff --git a/docs/integrations/aws-cognito.mdx b/docs/integrations/aws-cognito.mdx new file mode 100644 index 000000000..d89f6a1be --- /dev/null +++ b/docs/integrations/aws-cognito.mdx @@ -0,0 +1,322 @@ +--- +title: AWS Cognito OAuth ๐Ÿค FastMCP +sidebarTitle: AWS Cognito +description: Secure your FastMCP server with AWS Cognito user pools +icon: aws +tag: NEW +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + + + +This guide shows you how to secure your FastMCP server using **AWS Cognito user pools**. Since AWS Cognito doesn't support Dynamic Client Registration, this integration uses the [**OAuth Proxy**](/servers/auth/oauth-proxy) pattern to bridge AWS Cognito's traditional OAuth with MCP's authentication requirements. It also includes robust JWT token validation, ensuring enterprise-grade authentication. + +## Configuration + +### Prerequisites + +Before you begin, you will need: +1. An **[AWS Account](https://aws.amazon.com/)** with access to create AWS Cognito user pools +2. Basic familiarity with AWS Cognito concepts (user pools, app clients) +3. Your FastMCP server's URL (can be localhost for development, e.g., `http://localhost:8000`) + +### Step 1: Create an AWS Cognito User Pool and App Client + +Set up AWS Cognito user pool with an app client to get the credentials needed for authentication: + + + + Go to the **[AWS Cognito Console](https://console.aws.amazon.com/cognito/)** and ensure you're in your desired AWS region. + + Select **"User pools"** from the side navigation (click on the hamburger icon at the top left in case you don't see any), and click **"Create user pool"** to create a new user pool. + + + + AWS Cognito now provides a streamlined setup experience: + + 1. **Application type**: Select **"Traditional web application"** (this is the correct choice for FastMCP server-side authentication) + 2. **Name your application**: Enter a descriptive name (e.g., `FastMCP Server`) + + The traditional web application type automatically configures: + - Server-side authentication with client secrets + - Authorization code grant flow + - Appropriate security settings for confidential clients + + + Choose "Traditional web application" rather than SPA, Mobile app, or Machine-to-machine options. This ensures proper OAuth 2.0 configuration for FastMCP. + + + + + AWS will guide you through configuration options: + + - **Sign-in identifiers**: Choose how users will sign in (email, username, or phone) + - **Required attributes**: Select any additional user information you need + - **Return URL**: Add your callback URL (e.g., `http://localhost:8000/auth/callback` for development) + + + The simplified interface handles most OAuth security settings automatically based on your application type selection. + + + + + Review your configuration and click **"Create user pool"**. + + After creation, you'll see your user pool details. Save these important values: + - **User pool ID** (format: `eu-central-1_XXXXXXXXX`) + - **Client ID** (found under โ†’ "Applications" โ†’ "App clients" in the side navigation โ†’ \ โ†’ "App client information") + - **Client Secret** (found under โ†’ "Applications" โ†’ "App clients" in the side navigation โ†’ \ โ†’ "App client information") + + + The user pool ID and app client credentials are all you need for FastMCP configuration. + + + + + Under "Login pages" in your app client's settings, you can double check and adjust the OAuth configuration: + + - **Allowed callback URLs**: Add your server URL + `/auth/callback` (e.g., `http://localhost:8000/auth/callback`) + - **Allowed sign-out URLs**: Optional, for logout functionality + - **OAuth 2.0 grant types**: Ensure "Authorization code grant" is selected + - **OpenID Connect scopes**: Select scopes your application needs (e.g., `openid`, `email`, `profile`) + + + For local development, you can use `http://localhost` URLs. For production, you must use HTTPS. + + + + + Navigate to **"Branding" โ†’ "Domain"** in the side navigation to find or configure Your AWS Cognito domain: + + **Option 1: Use Auto-Generated Domain** + - If AWS has already created a domain automatically, note the **domain prefix** (the part before `.auth.region.amazoncognito.com`) + - This prefix is what you'll use in your FastMCP configuration + + **Option 2: Create a Custom Domain Prefix** + - If no domain exists or you want a better name, delete the existing domain and create a new one using the **"Actions"** menu + - Under **"Domain"** โ†’ **"Cognito domain"** in the **"Create Cognito domain"** dialog, enter a meaningful prefix (e.g., `my-app`) that is available in the AWS region you are in + - Just note the **domain prefix** you entered (e.g., `my-fastmcp-app`) - this is what you'll use in your FastMCP configuration + + + The FastMCP AWS Cognito provider automatically constructs the full domain from your prefix and region, simplifying configuration. + + + + + After setup, you'll have: + + - **User Pool ID**: Format like `eu-central-1_XXXXXXXXX` + - **Client ID**: Your application's client identifier + - **Client Secret**: Generated client secret (keep secure) + - **Domain Prefix**: The prefix of Your AWS Cognito domain + - **AWS Region**: Where Your AWS Cognito user pool is located + + + Store these credentials securely. Never commit them to version control. Use environment variables or AWS Secrets Manager in production. + + + + +### Step 2: FastMCP Configuration + +Create your FastMCP server using the `AWSCognitoProvider`, which handles AWS Cognito's JWT tokens and user claims automatically: + +```python server.py +from fastmcp import FastMCP +from fastmcp.server.auth.providers.aws import AWSCognitoProvider +from fastmcp.server.dependencies import get_access_token + +# The AWSCognitoProvider handles JWT validation and user claims +auth_provider = AWSCognitoProvider( + user_pool_id="eu-central-1_XXXXXXXXX", # Your AWS Cognito user pool ID + aws_region="eu-central-1", # AWS region (defaults to eu-central-1) + client_id="your-app-client-id", # Your app client ID + client_secret="your-app-client-secret", # Your app client Secret + base_url="http://localhost:8000", # Must match your callback URL + # redirect_path="/auth/callback" # Default value, customize if needed +) + +mcp = FastMCP(name="AWS Cognito Secured App", auth=auth_provider) + +# Add a protected tool to test authentication +@mcp.tool +async def get_access_token_claims() -> dict: + """Get the authenticated user's access token claims.""" + token = get_access_token() + return { + "sub": token.claims.get("sub"), + "username": token.claims.get("username"), + "cognito:groups": token.claims.get("cognito:groups", []), + } +``` + +## Testing + +### Running the Server + +Start your FastMCP server with HTTP transport to enable OAuth flows: + +```bash +fastmcp run server.py --transport http --port 8000 +``` + +Your server is now running and protected by AWS Cognito OAuth authentication. + +### Testing with a Client + +Create a test client that authenticates with Your AWS Cognito-protected server: + +```python test_client.py +from fastmcp import Client +import asyncio + +async def main(): + # The client will automatically handle AWS Cognito OAuth + async with Client("http://localhost:8000/mcp/", auth="oauth") as client: + # First-time connection will open AWS Cognito login in your browser + print("โœ“ Authenticated with AWS Cognito!") + + # Test the protected tool + print("Calling protected tool: get_access_token_claims") + result = await client.call_tool("get_access_token_claims") + user_data = result.data + print("Available access token claims:") + print(f"- sub: {user_data.get('sub', 'N/A')}") + print(f"- username: {user_data.get('username', 'N/A')}") + print(f"- cognito:groups: {user_data.get('cognito:groups', [])}") + +if __name__ == "__main__": + asyncio.run(main()) +``` + +When you run the client for the first time: +1. Your browser will open to AWS Cognito's hosted UI login page +2. After you sign in (or sign up), you'll be redirected back to your MCP server +3. The client receives the JWT token and can make authenticated requests + + +The client caches tokens locally, so you won't need to re-authenticate for subsequent runs unless the token expires or you explicitly clear the cache. + + +## Environment Variables + +For production deployments, use environment variables instead of hardcoding credentials. + +### Provider Selection + +Setting this environment variable allows the AWS Cognito provider to be used automatically without explicitly instantiating it in code. + + + +Set to `fastmcp.server.auth.providers.aws.AWSCognitoProvider` to use AWS Cognito authentication. + + + +### AWS Cognito-Specific Configuration + +These environment variables provide default values for the AWS Cognito provider, whether it's instantiated manually or configured via `FASTMCP_SERVER_AUTH`. + + + +Your AWS Cognito user pool ID (e.g., `eu-central-1_XXXXXXXXX`) + + + +AWS region where your AWS Cognito user pool is located + + + +Your AWS Cognito app client ID + + + +Your AWS Cognito app client secret + + + +Public URL of your FastMCP server for OAuth callbacks + + + +One of the redirect paths configured in your AWS Cognito app client + + + +Comma-, space-, or JSON-separated list of required OAuth scopes (e.g., `openid email` or `["openid","email","profile"]`) + + + +Example `.env` file: +```bash +# Use the AWS Cognito provider +FASTMCP_SERVER_AUTH=fastmcp.server.auth.providers.aws.AWSCognitoProvider + +# AWS Cognito credentials +FASTMCP_SERVER_AUTH_AWS_COGNITO_USER_POOL_ID=eu-central-1_XXXXXXXXX +FASTMCP_SERVER_AUTH_AWS_COGNITO_AWS_REGION=eu-central-1 +FASTMCP_SERVER_AUTH_AWS_COGNITO_CLIENT_ID=your-app-client-id +FASTMCP_SERVER_AUTH_AWS_COGNITO_CLIENT_SECRET=your-app-client-secret +FASTMCP_SERVER_AUTH_AWS_COGNITO_BASE_URL=https://your-server.com +FASTMCP_SERVER_AUTH_AWS_COGNITO_REQUIRED_SCOPES=openid,email,profile +``` + +With environment variables set, your server code simplifies to: + +```python server.py +from fastmcp import FastMCP +from fastmcp.server.dependencies import get_access_token + +# Authentication is automatically configured from environment +mcp = FastMCP(name="AWS Cognito Secured App") + +@mcp.tool +async def get_access_token_claims() -> dict: + """Get the authenticated user's access token claims.""" + token = get_access_token() + return { + "sub": token.claims.get("sub"), + "username": token.claims.get("username"), + "cognito:groups": token.claims.get("cognito:groups", []), + } +``` + +## Features + +### JWT Token Validation + +The AWS Cognito provider includes robust JWT token validation: + +- **Signature Verification**: Validates tokens against AWS Cognito's public keys (JWKS) +- **Expiration Checking**: Automatically rejects expired tokens +- **Issuer Validation**: Ensures tokens come from your specific AWS Cognito user pool +- **Scope Enforcement**: Verifies required OAuth scopes are present + +### User Claims and Groups + +Access rich user information from AWS Cognito JWT tokens: + +```python +from fastmcp.server.dependencies import get_access_token + +@mcp.tool +async def admin_only_tool() -> str: + """A tool only available to admin users.""" + token = get_access_token() + user_groups = token.claims.get("cognito:groups", []) + + if "admin" not in user_groups: + raise ValueError("This tool requires admin access") + + return "Admin access granted!" +``` + +### Enterprise Integration + +Perfect for enterprise environments with: + +- **Single Sign-On (SSO)**: Integrate with corporate identity providers +- **Multi-Factor Authentication (MFA)**: Leverage AWS Cognito's built-in MFA +- **User Groups**: Role-based access control through AWS Cognito groups +- **Custom Attributes**: Access custom user attributes defined in your AWS Cognito user pool +- **Compliance**: Meet enterprise security and compliance requirements \ No newline at end of file diff --git a/docs/integrations/azure.mdx b/docs/integrations/azure.mdx new file mode 100644 index 000000000..2ceefe482 --- /dev/null +++ b/docs/integrations/azure.mdx @@ -0,0 +1,254 @@ +--- +title: Azure (Microsoft Entra) OAuth ๐Ÿค FastMCP +sidebarTitle: Azure +description: Secure your FastMCP server with Azure/Microsoft Entra OAuth +icon: microsoft +tag: NEW +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + + + +This guide shows you how to secure your FastMCP server using **Azure OAuth** (Microsoft Entra ID). Since Azure doesn't support Dynamic Client Registration, this integration uses the [**OAuth Proxy**](/servers/auth/oauth-proxy) pattern to bridge Azure's traditional OAuth with MCP's authentication requirements. + +## Configuration + +### Prerequisites + +Before you begin, you will need: +1. An **[Azure Account](https://portal.azure.com/)** with access to create App registrations +2. Your FastMCP server's URL (can be localhost for development, e.g., `http://localhost:8000`) +3. Your Azure tenant ID (found in Azure Portal under Microsoft Entra ID) + +### Step 1: Create an Azure App Registration + +Create an App registration in Azure Portal to get the credentials needed for authentication: + + + + Go to the [Azure Portal](https://portal.azure.com) and navigate to **Microsoft Entra ID โ†’ App registrations**. + + Click **"New registration"** to create a new application. + + + + Fill in the application details: + + - **Name**: Choose a name users will recognize (e.g., "My FastMCP Server") + - **Supported account types**: Choose based on your needs: + - **Single tenant**: Only users in your organization + - **Multitenant**: Users in any Microsoft Entra directory + - **Multitenant + personal accounts**: Any Microsoft account + - **Redirect URI**: Select "Web" and enter your server URL + `/auth/callback` (e.g., `http://localhost:8000/auth/callback`) + + + The redirect URI must match exactly. The default path is `/auth/callback`, but you can customize it using the `redirect_path` parameter. For local development, Azure allows `http://localhost` URLs. For production, you must use HTTPS. + + + + If you want to use a custom callback path (e.g., `/auth/azure/callback`), make sure to set the same path in both your Azure App registration and the `redirect_path` parameter when configuring the AzureProvider. + + + + + After registration, navigate to **Certificates & secrets** in your app's settings. + + - Click **"New client secret"** + - Add a description (e.g., "FastMCP Server") + - Choose an expiration period + - Click **"Add"** + + + Copy the secret value immediately - it won't be shown again! You'll need to create a new secret if you lose it. + + + + + From the **Overview** page of your app registration, note: + + - **Application (client) ID**: A UUID like `835f09b6-0f0f-40cc-85cb-f32c5829a149` + - **Directory (tenant) ID**: A UUID like `08541b6e-646d-43de-a0eb-834e6713d6d5` + - **Client Secret**: The value you copied in the previous step + + + Store these credentials securely. Never commit them to version control. Use environment variables or a secrets manager in production. + + + + +### Step 2: FastMCP Configuration + +Create your FastMCP server using the `AzureProvider`, which handles Azure's OAuth flow automatically: + +```python server.py +from fastmcp import FastMCP +from fastmcp.server.auth.providers.azure import AzureProvider + +# The AzureProvider handles Azure's token format and validation +auth_provider = AzureProvider( + client_id="835f09b6-0f0f-40cc-85cb-f32c5829a149", # Your Azure App Client ID + client_secret="your-client-secret", # Your Azure App Client Secret + tenant_id="08541b6e-646d-43de-a0eb-834e6713d6d5", # Your Azure Tenant ID (REQUIRED) + base_url="http://localhost:8000", # Must match your App registration + required_scopes=["User.Read", "email", "openid", "profile"], # Microsoft Graph permissions + # redirect_path="/auth/callback" # Default value, customize if needed +) + +mcp = FastMCP(name="Azure Secured App", auth=auth_provider) + +# Add a protected tool to test authentication +@mcp.tool +async def get_user_info() -> dict: + """Returns information about the authenticated Azure user.""" + from fastmcp.server.dependencies import get_access_token + + token = get_access_token() + # The AzureProvider stores user data in token claims + return { + "azure_id": token.claims.get("sub"), + "email": token.claims.get("email"), + "name": token.claims.get("name"), + "job_title": token.claims.get("job_title"), + "office_location": token.claims.get("office_location") + } +``` + + +**Important**: The `tenant_id` parameter is **REQUIRED**. Azure no longer supports using "common" for new applications due to security requirements. You must use one of: + +- **Your specific tenant ID**: Found in Azure Portal (e.g., `08541b6e-646d-43de-a0eb-834e6713d6d5`) +- **"organizations"**: For work and school accounts only +- **"consumers"**: For personal Microsoft accounts only + +Using your specific tenant ID is recommended for better security and control. + + +## Testing + +### Running the Server + +Start your FastMCP server with HTTP transport to enable OAuth flows: + +```bash +fastmcp run server.py --transport http --port 8000 +``` + +Your server is now running and protected by Azure OAuth authentication. + +### Testing with a Client + +Create a test client that authenticates with your Azure-protected server: + +```python test_client.py +from fastmcp import Client +import asyncio + +async def main(): + # The client will automatically handle Azure OAuth + async with Client("http://localhost:8000/mcp/", auth="oauth") as client: + # First-time connection will open Azure login in your browser + print("โœ“ Authenticated with Azure!") + + # Test the protected tool + result = await client.call_tool("get_user_info") + print(f"Azure user: {result['email']}") + print(f"Name: {result['name']}") + +if __name__ == "__main__": + asyncio.run(main()) +``` + +When you run the client for the first time: +1. Your browser will open to Microsoft's authorization page +2. Sign in with your Microsoft account (work, school, or personal based on your tenant configuration) +3. Grant the requested permissions +4. After authorization, you'll be redirected back +5. The client receives the token and can make authenticated requests + + +The client caches tokens locally, so you won't need to re-authenticate for subsequent runs unless the token expires or you explicitly clear the cache. + + +## Environment Variables + + + +For production deployments, use environment variables instead of hardcoding credentials. + +### Provider Selection + +Setting this environment variable allows the Azure provider to be used automatically without explicitly instantiating it in code. + + + +Set to `fastmcp.server.auth.providers.azure.AzureProvider` to use Azure authentication. + + + +### Azure-Specific Configuration + +These environment variables provide default values for the Azure provider, whether it's instantiated manually or configured via `FASTMCP_SERVER_AUTH`. + + + +Your Azure App registration Client ID (e.g., `835f09b6-0f0f-40cc-85cb-f32c5829a149`) + + + +Your Azure App registration Client Secret + + + +Your Azure tenant ID (specific ID, "organizations", or "consumers") + + +This is **REQUIRED**. Find your tenant ID in Azure Portal under Microsoft Entra ID โ†’ Overview. + + + + +Public URL of your FastMCP server for OAuth callbacks + + + +Redirect path configured in your Azure App registration + + + +Comma-, space-, or JSON-separated list of required Microsoft Graph scopes + + + +HTTP request timeout for Microsoft Graph API calls + + + +Example `.env` file: +```bash +# Use the Azure provider +FASTMCP_SERVER_AUTH=fastmcp.server.auth.providers.azure.AzureProvider + +# Azure OAuth credentials +FASTMCP_SERVER_AUTH_AZURE_CLIENT_ID=835f09b6-0f0f-40cc-85cb-f32c5829a149 +FASTMCP_SERVER_AUTH_AZURE_CLIENT_SECRET=your-client-secret-here +FASTMCP_SERVER_AUTH_AZURE_TENANT_ID=08541b6e-646d-43de-a0eb-834e6713d6d5 +FASTMCP_SERVER_AUTH_AZURE_BASE_URL=https://your-server.com +FASTMCP_SERVER_AUTH_AZURE_REQUIRED_SCOPES=User.Read,email,profile +``` + +With environment variables set, your server code simplifies to: + +```python server.py +from fastmcp import FastMCP + +# Authentication is automatically configured from environment +mcp = FastMCP(name="Azure Secured App") + +@mcp.tool +async def protected_tool(query: str) -> str: + """A tool that requires Azure authentication to access.""" + # Your tool implementation here + return f"Processing authenticated request: {query}" +``` + diff --git a/docs/integrations/chatgpt.mdx b/docs/integrations/chatgpt.mdx new file mode 100644 index 000000000..18028c69d --- /dev/null +++ b/docs/integrations/chatgpt.mdx @@ -0,0 +1,157 @@ +--- +title: ChatGPT ๐Ÿค FastMCP +sidebarTitle: ChatGPT +description: Connect FastMCP servers to ChatGPT in Chat and Deep Research modes +icon: message-smile +tag: NEW +--- + +ChatGPT supports MCP servers through remote HTTP connections in two modes: **Chat mode** for interactive conversations and **Deep Research mode** for comprehensive information retrieval. + + +**Developer Mode Required for Chat Mode**: To use MCP servers in regular ChatGPT conversations, you must first enable Developer Mode in your ChatGPT settings. This feature is available for ChatGPT Pro, Team, Enterprise, and Edu users. + + + +OpenAI's official MCP documentation and examples are built with **FastMCP v2**! Learn more from their [MCP documentation](https://platform.openai.com/docs/mcp) and [Developer Mode guide](https://platform.openai.com/docs/guides/developer-mode). + + +## Build a Server + +First, let's create a simple FastMCP server: + +```python server.py +from fastmcp import FastMCP +import random + +mcp = FastMCP("Demo Server") + +@mcp.tool +def roll_dice(sides: int = 6) -> int: + """Roll a dice with the specified number of sides.""" + return random.randint(1, sides) + +if __name__ == "__main__": + mcp.run(transport="http", port=8000) +``` + +### Deploy Your Server + +Your server must be accessible from the internet. For development, use `ngrok`: + + +```bash Terminal 1 +python server.py +``` + +```bash Terminal 2 +ngrok http 8000 +``` + + +Note your public URL (e.g., `https://abc123.ngrok.io`) for the next steps. + +## Chat Mode + +Chat mode lets you use MCP tools directly in ChatGPT conversations. See [OpenAI's Developer Mode guide](https://platform.openai.com/docs/guides/developer-mode) for the latest requirements. + +### Add to ChatGPT + +#### 1. Enable Developer Mode + +1. Open ChatGPT and go to **Settings** โ†’ **Connectors** +2. Under **Advanced**, toggle **Developer Mode** to enabled + +#### 2. Create Connector + +1. In **Settings** โ†’ **Connectors**, click **Create** +2. Enter: + - **Name**: Your server name + - **Server URL**: `https://your-server.ngrok.io/mcp/` +3. Check **I trust this provider** +4. Add authentication if needed +5. Click **Create** + + +**Without Developer Mode**: If you don't have search/fetch tools, ChatGPT will reject the server. With Developer Mode enabled, you don't need search/fetch tools for Chat mode. + + +#### 3. Use in Chat + +1. Start a new chat +2. Click the **+** button โ†’ **More** โ†’ **Developer Mode** +3. **Enable your MCP server connector** (required - the connector must be explicitly added to each chat) +4. Now you can use your tools: + +Example usage: +- "Roll a 20-sided dice" +- "Roll dice" (uses default 6 sides) + + +The connector must be explicitly enabled in each chat session through Developer Mode. Once added, it remains active for the entire conversation. + + +### Skip Confirmations + +Use `annotations={"readOnlyHint": True}` to skip confirmation prompts for read-only tools: + +```python +@mcp.tool(annotations={"readOnlyHint": True}) +def get_status() -> str: + """Check system status.""" + return "All systems operational" + +@mcp.tool() # No annotation - ChatGPT may ask for confirmation +def delete_item(id: str) -> str: + """Delete an item.""" + return f"Deleted {id}" +``` + +## Deep Research Mode + +Deep Research mode provides systematic information retrieval with citations. See [OpenAI's MCP documentation](https://platform.openai.com/docs/mcp) for the latest Deep Research specifications. + + +**Search and Fetch Required**: Without Developer Mode, ChatGPT will reject any server that doesn't have both `search` and `fetch` tools. Even in Developer Mode, Deep Research only uses these two tools. + + +### Tool Implementation + +Deep Research tools must follow this pattern: + +```python +@mcp.tool() +def search(query: str) -> dict: + """ + Search for records matching the query. + Must return {"ids": [list of string IDs]} + """ + # Your search logic + matching_ids = ["id1", "id2", "id3"] + return {"ids": matching_ids} + +@mcp.tool() +def fetch(id: str) -> dict: + """ + Fetch a complete record by ID. + Return the full record data for ChatGPT to analyze. + """ + # Your fetch logic + return { + "id": id, + "title": "Record Title", + "content": "Full record content...", + "metadata": {"author": "Jane Doe", "date": "2024"} + } +``` + +### Using Deep Research + +1. Ensure your server is added to ChatGPT's connectors (same as Chat mode) +2. Start a new chat +3. Click **+** โ†’ **Deep Research** +4. Select your MCP server as a source +5. Ask research questions + +ChatGPT will use your `search` and `fetch` tools to find and cite relevant information. + diff --git a/docs/integrations/claude-code.mdx b/docs/integrations/claude-code.mdx new file mode 100644 index 000000000..2b04c10a8 --- /dev/null +++ b/docs/integrations/claude-code.mdx @@ -0,0 +1,177 @@ +--- +title: Claude Code ๐Ÿค FastMCP +sidebarTitle: Claude Code +description: Install and use FastMCP servers in Claude Code +icon: message-smile +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" +import { LocalFocusTip } from "/snippets/local-focus.mdx" + + + +Claude Code supports MCP servers through multiple transport methods including STDIO, SSE, and HTTP, allowing you to extend Claude's capabilities with custom tools, resources, and prompts from your FastMCP servers. + +## Requirements + +This integration uses STDIO transport to run your FastMCP server locally. For remote deployments, you can run your FastMCP server with HTTP or SSE transport and configure it directly using Claude Code's built-in MCP management commands. + +## Create a Server + +The examples in this guide will use the following simple dice-rolling server, saved as `server.py`. + +```python server.py +import random +from fastmcp import FastMCP + +mcp = FastMCP(name="Dice Roller") + +@mcp.tool +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)] + +if __name__ == "__main__": + mcp.run() +``` + +## Install the Server + +### FastMCP CLI + + +The easiest way to install a FastMCP server in Claude Code is using the `fastmcp install claude-code` command. This automatically handles the configuration, dependency management, and calls Claude Code's built-in MCP management system. + +```bash +fastmcp install claude-code server.py +``` + +The install command supports the same `file.py:object` notation as the `run` command. If no object is specified, it will automatically look for a FastMCP server object named `mcp`, `server`, or `app` in your file: + +```bash +# These are equivalent if your server object is named 'mcp' +fastmcp install claude-code server.py +fastmcp install claude-code server.py:mcp + +# Use explicit object name if your server has a different name +fastmcp install claude-code server.py:my_custom_server +``` + +The command will automatically configure the server with Claude Code's `claude mcp add` command. + +#### Dependencies + +FastMCP provides flexible dependency management options for your Claude Code servers: + +**Individual packages**: Use the `--with` flag to specify packages your server needs. You can use this flag multiple times: + +```bash +fastmcp install claude-code server.py --with pandas --with requests +``` + +**Requirements file**: If you maintain a `requirements.txt` file with all your dependencies, use `--with-requirements` to install them: + +```bash +fastmcp install claude-code server.py --with-requirements requirements.txt +``` + +**Editable packages**: For local packages under development, use `--with-editable` to install them in editable mode: + +```bash +fastmcp install claude-code server.py --with-editable ./my-local-package +``` + +Alternatively, you can use a `fastmcp.json` configuration file (recommended): + +```json fastmcp.json +{ + "$schema": "https://gofastmcp.com/public/schemas/fastmcp.json/v1.json", + "source": { + "path": "server.py", + "entrypoint": "mcp" + }, + "environment": { + "dependencies": ["pandas", "requests"] + } +} +``` + + +#### Python Version and Project Configuration + +Control the Python environment for your server with these options: + +**Python version**: Use `--python` to specify which Python version your server requires. This ensures compatibility when your server needs specific Python features: + +```bash +fastmcp install claude-code server.py --python 3.11 +``` + +**Project directory**: Use `--project` to run your server within a specific project context. This tells `uv` to use the project's configuration files and virtual environment: + +```bash +fastmcp install claude-code server.py --project /path/to/my-project +``` + +#### Environment Variables + +If your server needs environment variables (like API keys), you must include them: + +```bash +fastmcp install claude-code server.py --server-name "Weather Server" \ + --env API_KEY=your-api-key \ + --env DEBUG=true +``` + +Or load them from a `.env` file: + +```bash +fastmcp install claude-code server.py --server-name "Weather Server" --env-file .env +``` + + +**Claude Code must be installed**. The integration looks for the Claude Code CLI at the default installation location (`~/.claude/local/claude`) and uses the `claude mcp add` command to register servers. + + +### Manual Configuration + +For more control over the configuration, you can manually use Claude Code's built-in MCP management commands. This gives you direct control over how your server is launched: + +```bash +# Add a server with custom configuration +claude mcp add dice-roller -- uv run --with fastmcp fastmcp run server.py + +# Add with environment variables +claude mcp add weather-server -e API_KEY=secret -e DEBUG=true -- uv run --with fastmcp fastmcp run server.py + +# Add with specific scope (local, user, or project) +claude mcp add my-server --scope user -- uv run --with fastmcp fastmcp run server.py +``` + +You can also manually specify Python versions and project directories in your Claude Code commands: + +```bash +# With specific Python version +claude mcp add ml-server -- uv run --python 3.11 --with fastmcp fastmcp run server.py + +# Within a project directory +claude mcp add project-server -- uv run --project /path/to/project --with fastmcp fastmcp run server.py +``` + +## Using the Server + +Once your server is installed, you can start using your FastMCP server with Claude Code. + +Try asking Claude something like: + +> "Roll some dice for me" + +Claude will automatically detect your `roll_dice` tool and use it to fulfill your request, returning something like: + +> I'll roll some dice for you! Here are your results: [4, 2, 6] +> +> You rolled three dice and got a 4, a 2, and a 6! + +Claude Code can now access all the tools, resources, and prompts you've defined in your FastMCP server. + +If your server provides resources, you can reference them with `@` mentions using the format `@server:protocol://resource/path`. If your server provides prompts, you can use them as slash commands with `/mcp__servername__promptname`. \ No newline at end of file diff --git a/docs/integrations/claude-desktop.mdx b/docs/integrations/claude-desktop.mdx new file mode 100644 index 000000000..3158a005a --- /dev/null +++ b/docs/integrations/claude-desktop.mdx @@ -0,0 +1,298 @@ +--- +title: Claude Desktop ๐Ÿค FastMCP +sidebarTitle: Claude Desktop +description: Connect FastMCP servers to Claude Desktop +icon: message-smile +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" +import { LocalFocusTip } from "/snippets/local-focus.mdx" + + + +Claude Desktop supports MCP servers through local STDIO connections and remote servers (beta), allowing you to extend Claude's capabilities with custom tools, resources, and prompts from your FastMCP servers. + + +Remote MCP server support is currently in beta and available for users on Claude Pro, Max, Team, and Enterprise plans (as of June 2025). Most users will still need to use local STDIO connections. + + + +This guide focuses specifically on using FastMCP servers with Claude Desktop. For general Claude Desktop MCP setup and official examples, see the [official Claude Desktop quickstart guide](https://modelcontextprotocol.io/quickstart/user). + + + +## Requirements + +Claude Desktop traditionally requires MCP servers to run locally using STDIO transport, where your server communicates with Claude through standard input/output rather than HTTP. However, users on certain plans now have access to remote server support as well. + + +If you don't have access to remote server support or need to connect to remote servers, you can create a **proxy server** that runs locally via STDIO and forwards requests to remote HTTP servers. See the [Proxy Servers](#proxy-servers) section below. + + +## Create a Server + +The examples in this guide will use the following simple dice-rolling server, saved as `server.py`. + +```python server.py +import random +from fastmcp import FastMCP + +mcp = FastMCP(name="Dice Roller") + +@mcp.tool +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)] + +if __name__ == "__main__": + mcp.run() +``` + +## Install the Server + +### FastMCP CLI + + +The easiest way to install a FastMCP server in Claude Desktop is using the `fastmcp install claude-desktop` command. This automatically handles the configuration and dependency management. + + +Prior to version 2.10.3, Claude Desktop could be managed by running `fastmcp install ` without specifying the client. + + +```bash +fastmcp install claude-desktop server.py +``` + +The install command supports the same `file.py:object` notation as the `run` command. If no object is specified, it will automatically look for a FastMCP server object named `mcp`, `server`, or `app` in your file: + +```bash +# These are equivalent if your server object is named 'mcp' +fastmcp install claude-desktop server.py +fastmcp install claude-desktop server.py:mcp + +# Use explicit object name if your server has a different name +fastmcp install claude-desktop server.py:my_custom_server +``` + +After installation, restart Claude Desktop completely. You should see a hammer icon (๐Ÿ”จ) in the bottom left of the input box, indicating that MCP tools are available. + +#### Dependencies + +FastMCP provides several ways to manage your server's dependencies when installing in Claude Desktop: + +**Individual packages**: Use the `--with` flag to specify packages your server needs. You can use this flag multiple times: + +```bash +fastmcp install claude-desktop server.py --with pandas --with requests +``` + +**Requirements file**: If you have a `requirements.txt` file listing all your dependencies, use `--with-requirements` to install them all at once: + +```bash +fastmcp install claude-desktop server.py --with-requirements requirements.txt +``` + +**Editable packages**: For local packages in development, use `--with-editable` to install them in editable mode: + +```bash +fastmcp install claude-desktop server.py --with-editable ./my-local-package +``` + +Alternatively, you can use a `fastmcp.json` configuration file (recommended): + +```json fastmcp.json +{ + "$schema": "https://gofastmcp.com/public/schemas/fastmcp.json/v1.json", + "source": { + "path": "server.py", + "entrypoint": "mcp" + }, + "environment": { + "dependencies": ["pandas", "requests"] + } +} +``` + + +#### Python Version and Project Directory + +FastMCP allows you to control the Python environment for your server: + +**Python version**: Use `--python` to specify which Python version your server should run with. This is particularly useful when your server requires a specific Python version: + +```bash +fastmcp install claude-desktop server.py --python 3.11 +``` + +**Project directory**: Use `--project` to run your server within a specific project directory. This ensures that `uv` will discover all `pyproject.toml`, `uv.toml`, and `.python-version` files from that project: + +```bash +fastmcp install claude-desktop server.py --project /path/to/my-project +``` + +When you specify a project directory, all relative paths in your server will be resolved from that directory, and the project's virtual environment will be used. + +#### Environment Variables + + +Claude Desktop runs servers in a completely isolated environment with no access to your shell environment or locally installed applications. You must explicitly pass any environment variables your server needs. + + +If your server needs environment variables (like API keys), you must include them: + +```bash +fastmcp install claude-desktop server.py --server-name "Weather Server" \ + --env API_KEY=your-api-key \ + --env DEBUG=true +``` + +Or load them from a `.env` file: + +```bash +fastmcp install claude-desktop server.py --server-name "Weather Server" --env-file .env +``` + +- **`uv` must be installed and available in your system PATH**. Claude Desktop runs in its own isolated environment and needs `uv` to manage dependencies. +- **On macOS, it is recommended to install `uv` globally with Homebrew** so that Claude Desktop will detect it: `brew install uv`. Installing `uv` with other methods may not make it accessible to Claude Desktop. + + + +### Manual Configuration + +For more control over the configuration, you can manually edit Claude Desktop's configuration file. You can open the configuration file from Claude's developer settings, or find it in the following locations: +- **macOS**: `~/Library/Application Support/Claude/claude_desktop_config.json` +- **Windows**: `%APPDATA%\Claude\claude_desktop_config.json` + +The configuration file is a JSON object with a `mcpServers` key, which contains the configuration for each MCP server. + +```json +{ + "mcpServers": { + "dice-roller": { + "command": "python", + "args": ["path/to/your/server.py"] + } + } +} +``` + +After updating the configuration file, restart Claude Desktop completely. Look for the hammer icon (๐Ÿ”จ) to confirm your server is loaded. + +#### Dependencies + +If your server has dependencies, you can use `uv` or another package manager to set up the environment. + + +When manually configuring dependencies, the recommended approach is to use `uv` with FastMCP. The configuration uses `uv run` to create an isolated environment with your specified packages: + +```json +{ + "mcpServers": { + "dice-roller": { + "command": "uv", + "args": [ + "run", + "--with", "fastmcp", + "--with", "pandas", + "--with", "requests", + "fastmcp", + "run", + "path/to/your/server.py" + ] + } + } +} +``` + +You can also manually specify Python versions and project directories in your configuration. Add `--python` to use a specific Python version, or `--project` to run within a project directory: + +```json +{ + "mcpServers": { + "dice-roller": { + "command": "uv", + "args": [ + "run", + "--python", "3.11", + "--project", "/path/to/project", + "--with", "fastmcp", + "fastmcp", + "run", + "path/to/your/server.py" + ] + } + } +} +``` + +The order of arguments matters: Python version and project settings come before package specifications, which come before the actual command to run. + + +- **`uv` must be installed and available in your system PATH**. Claude Desktop runs in its own isolated environment and needs `uv` to manage dependencies. +- **On macOS, it is recommended to install `uv` globally with Homebrew** so that Claude Desktop will detect it: `brew install uv`. Installing `uv` with other methods may not make it accessible to Claude Desktop. + + +#### Environment Variables + +You can also specify environment variables in the configuration: + +```json +{ + "mcpServers": { + "weather-server": { + "command": "python", + "args": ["path/to/weather_server.py"], + "env": { + "API_KEY": "your-api-key", + "DEBUG": "true" + } + } + } +} +``` + +Claude Desktop runs servers in a completely isolated environment with no access to your shell environment or locally installed applications. You must explicitly pass any environment variables your server needs. + + + +## Remote Servers + + +Users on Claude Pro, Max, Team, and Enterprise plans have first-class remote server support via integrations. For other users, or as an alternative approach, FastMCP can create a proxy server that forwards requests to a remote HTTP server. You can install the proxy server in Claude Desktop. + +Create a proxy server that connects to a remote HTTP server: + +```python proxy_server.py +from fastmcp import FastMCP + +# Create a proxy to a remote server +proxy = FastMCP.as_proxy( + "https://example.com/mcp/sse", + name="Remote Server Proxy" +) + +if __name__ == "__main__": + proxy.run() # Runs via STDIO for Claude Desktop +``` + +### Authentication + +For authenticated remote servers, create an authenticated client following the guidance in the [client auth documentation](/clients/auth/bearer) and pass it to the proxy: + +```python auth_proxy_server.py {7} +from fastmcp import FastMCP, Client +from fastmcp.client.auth import BearerAuth + +# Create authenticated client +client = Client( + "https://api.example.com/mcp/sse", + auth=BearerAuth(token="your-access-token") +) + +# Create proxy using the authenticated client +proxy = FastMCP.as_proxy(client, name="Authenticated Proxy") + +if __name__ == "__main__": + proxy.run() +``` + diff --git a/docs/integrations/cursor-install-mcp.png b/docs/integrations/cursor-install-mcp.png new file mode 100644 index 000000000..5681d70d7 Binary files /dev/null and b/docs/integrations/cursor-install-mcp.png differ diff --git a/docs/integrations/cursor.mdx b/docs/integrations/cursor.mdx new file mode 100644 index 000000000..5b98cbd1a --- /dev/null +++ b/docs/integrations/cursor.mdx @@ -0,0 +1,284 @@ +--- +title: Cursor ๐Ÿค FastMCP +sidebarTitle: Cursor +description: Install and use FastMCP servers in Cursor +icon: message-smile +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" +import { LocalFocusTip } from "/snippets/local-focus.mdx" + + + +Cursor supports MCP servers through multiple transport methods including STDIO, SSE, and Streamable HTTP, allowing you to extend Cursor's AI assistant with custom tools, resources, and prompts from your FastMCP servers. + +## Requirements + +This integration uses STDIO transport to run your FastMCP server locally. For remote deployments, you can run your FastMCP server with HTTP or SSE transport and configure it directly in Cursor's settings. + +## Create a Server + +The examples in this guide will use the following simple dice-rolling server, saved as `server.py`. + +```python server.py +import random +from fastmcp import FastMCP + +mcp = FastMCP(name="Dice Roller") + +@mcp.tool +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)] + +if __name__ == "__main__": + mcp.run() +``` + +## Install the Server + +### FastMCP CLI + + +The easiest way to install a FastMCP server in Cursor is using the `fastmcp install cursor` command. This automatically handles the configuration, dependency management, and opens Cursor with a deeplink to install the server. + +```bash +fastmcp install cursor server.py +``` + +#### Workspace Installation + + +By default, FastMCP installs servers globally for Cursor. You can also install servers to project-specific workspaces using the `--workspace` flag: + +```bash +# Install to current directory's .cursor/ folder +fastmcp install cursor server.py --workspace . + +# Install to specific workspace +fastmcp install cursor server.py --workspace /path/to/project +``` + +This creates a `.cursor/mcp.json` configuration file in the specified workspace directory, allowing different projects to have their own MCP server configurations. + +The install command supports the same `file.py:object` notation as the `run` command. If no object is specified, it will automatically look for a FastMCP server object named `mcp`, `server`, or `app` in your file: + +```bash +# These are equivalent if your server object is named 'mcp' +fastmcp install cursor server.py +fastmcp install cursor server.py:mcp + +# Use explicit object name if your server has a different name +fastmcp install cursor server.py:my_custom_server +``` + +After running the command, Cursor will open automatically and prompt you to install the server. The command will be `uv`, which is expected as this is a Python STDIO server. Click "Install" to confirm: + +![Cursor install prompt](./cursor-install-mcp.png) + +#### Dependencies + +FastMCP offers multiple ways to manage dependencies for your Cursor servers: + +**Individual packages**: Use the `--with` flag to specify packages your server needs. You can use this flag multiple times: + +```bash +fastmcp install cursor server.py --with pandas --with requests +``` + +**Requirements file**: For projects with a `requirements.txt` file, use `--with-requirements` to install all dependencies at once: + +```bash +fastmcp install cursor server.py --with-requirements requirements.txt +``` + +**Editable packages**: When developing local packages, use `--with-editable` to install them in editable mode: + +```bash +fastmcp install cursor server.py --with-editable ./my-local-package +``` + +Alternatively, you can use a `fastmcp.json` configuration file (recommended): + +```json fastmcp.json +{ + "$schema": "https://gofastmcp.com/public/schemas/fastmcp.json/v1.json", + "source": { + "path": "server.py", + "entrypoint": "mcp" + }, + "environment": { + "dependencies": ["pandas", "requests"] + } +} +``` + + +#### Python Version and Project Configuration + +Control your server's Python environment with these options: + +**Python version**: Use `--python` to specify which Python version your server should use. This is essential when your server requires specific Python features: + +```bash +fastmcp install cursor server.py --python 3.11 +``` + +**Project directory**: Use `--project` to run your server within a specific project context. This ensures `uv` discovers all project configuration files and uses the correct virtual environment: + +```bash +fastmcp install cursor server.py --project /path/to/my-project +``` + +#### Environment Variables + + +Cursor runs servers in a completely isolated environment with no access to your shell environment or locally installed applications. You must explicitly pass any environment variables your server needs. + + +If your server needs environment variables (like API keys), you must include them: + +```bash +fastmcp install cursor server.py --server-name "Weather Server" \ + --env API_KEY=your-api-key \ + --env DEBUG=true +``` + +Or load them from a `.env` file: + +```bash +fastmcp install cursor server.py --server-name "Weather Server" --env-file .env +``` + + +**`uv` must be installed and available in your system PATH**. Cursor runs in its own isolated environment and needs `uv` to manage dependencies. + + +### Generate MCP JSON + + +**Use the first-class integration above for the best experience.** The MCP JSON generation is useful for advanced use cases, manual configuration, or integration with other tools. + + +You can generate MCP JSON configuration for manual use: + +```bash +# Generate configuration and output to stdout +fastmcp install mcp-json server.py --server-name "Dice Roller" --with pandas + +# Copy configuration to clipboard for easy pasting +fastmcp install mcp-json server.py --server-name "Dice Roller" --copy +``` + +This generates the standard `mcpServers` configuration format that can be used with any MCP-compatible client. + +### Manual Configuration + +For more control over the configuration, you can manually edit Cursor's configuration file. The configuration file is located at: +- **All platforms**: `~/.cursor/mcp.json` + +The configuration file is a JSON object with a `mcpServers` key, which contains the configuration for each MCP server. + +```json +{ + "mcpServers": { + "dice-roller": { + "command": "python", + "args": ["path/to/your/server.py"] + } + } +} +``` + +After updating the configuration file, your server should be available in Cursor. + +#### Dependencies + +If your server has dependencies, you can use `uv` or another package manager to set up the environment. + +When manually configuring dependencies, the recommended approach is to use `uv` with FastMCP. The configuration should use `uv run` to create an isolated environment with your specified packages: + +```json +{ + "mcpServers": { + "dice-roller": { + "command": "uv", + "args": [ + "run", + "--with", "fastmcp", + "--with", "pandas", + "--with", "requests", + "fastmcp", + "run", + "path/to/your/server.py" + ] + } + } +} +``` + +You can also manually specify Python versions and project directories in your configuration: + +```json +{ + "mcpServers": { + "dice-roller": { + "command": "uv", + "args": [ + "run", + "--python", "3.11", + "--project", "/path/to/project", + "--with", "fastmcp", + "fastmcp", + "run", + "path/to/your/server.py" + ] + } + } +} +``` + +Note that the order of arguments is important: Python version and project settings should come before package specifications. + + +**`uv` must be installed and available in your system PATH**. Cursor runs in its own isolated environment and needs `uv` to manage dependencies. + + +#### Environment Variables + +You can also specify environment variables in the configuration: + +```json +{ + "mcpServers": { + "weather-server": { + "command": "python", + "args": ["path/to/weather_server.py"], + "env": { + "API_KEY": "your-api-key", + "DEBUG": "true" + } + } + } +} +``` + + +Cursor runs servers in a completely isolated environment with no access to your shell environment or locally installed applications. You must explicitly pass any environment variables your server needs. + + +## Using the Server + +Once your server is installed, you can start using your FastMCP server with Cursor's AI assistant. + +Try asking Cursor something like: + +> "Roll some dice for me" + +Cursor will automatically detect your `roll_dice` tool and use it to fulfill your request, returning something like: + +> ๐ŸŽฒ Here are your dice rolls: 4, 6, 4 +> +> You rolled 3 dice with a total of 14! The 6 was a nice high roll there! + +The AI assistant can now access all the tools, resources, and prompts you've defined in your FastMCP server. diff --git a/docs/integrations/descope.mdx b/docs/integrations/descope.mdx new file mode 100644 index 000000000..edca62b3a --- /dev/null +++ b/docs/integrations/descope.mdx @@ -0,0 +1,154 @@ +--- +title: Descope ๐Ÿค FastMCP +sidebarTitle: Descope +description: Secure your FastMCP server with Descope +icon: shield-check +tag: NEW +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + + + +This guide shows you how to secure your FastMCP server using [**Descope**](https://www.descope.com), a complete authentication and user management solution. This integration uses the [**Remote OAuth**](/servers/auth/remote-oauth) pattern, where Descope handles user login and your FastMCP server validates the tokens. + +## Configuration + +### Prerequisites + +Before you begin, you will need: +1. To [sign up](https://www.descope.com/sign-up) for a Free Forever Descope account +2. Your **Project ID** from the [Descope Console](https://app.descope.com/settings/project) +3. Your FastMCP server's URL (can be localhost for development, e.g., `http://localhost:3000`) + +### Step 1: Configure Descope + + + + 1. Go to the [Inbound Apps page](https://app.descope.com/apps/inbound) of the Descope Console + 2. Click **DCR Settings** + 3. Enable **Dynamic Client Registration (DCR)** + 4. Define allowed scopes + + + DCR is required for FastMCP clients to automatically register with your authentication server. + + + + + Save your Project ID from [Project Settings](https://app.descope.com/settings/project): + ``` + Project ID: P2abc...123 + ``` + + + +### Step 2: Environment Setup + +Create a `.env` file with your Descope configuration: + +```bash +DESCOPE_PROJECT_ID=P2abc...123 # Your Descope Project ID +DESCOPE_BASE_URL=https://api.descope.com # Descope API URL +SERVER_URL=http://localhost:3000 # Your server's base URL +``` + + +You can find your project's Descope Base URL in the [Multi-Region Support Guide](https://docs.descope.com/management/project-settings/multi-regional). + + +### Step 3: FastMCP Configuration + +Create your FastMCP server file and use the DescopeProvider to handle all the OAuth integration automatically: + +```python server.py +from fastmcp import FastMCP +from fastmcp.server.auth.providers.descope import DescopeProvider + +# The DescopeProvider automatically discovers Descope endpoints +# and configures JWT token validation +auth_provider = DescopeProvider( + project_id=DESCOPE_PROJECT_ID, # Your Descope Project ID + base_url=SERVER_URL, # Your server's public URL + descope_base_url=DESCOPE_BASE_URL, # Descope API base URL +) + +# Create FastMCP server with auth +mcp = FastMCP(name="My Descope Protected Server", auth=auth_provider) + +``` + +## Testing + +To test your server, you can use the `fastmcp` CLI to run it locally. Assuming you've saved the above code to `server.py` (after replacing the `project_id`, `base_url`, and `descope_base_url` with your actual values!), you can run the following command: + +```bash +fastmcp run server.py --transport http --port 8000 +``` + +Now, you can use a FastMCP client to test that you can reach your server after authenticating: + +```python +from fastmcp import Client +import asyncio + +async def main(): + async with Client("http://localhost:8000/mcp/", auth="oauth") as client: + assert await client.ping() + +if __name__ == "__main__": + asyncio.run(main()) +``` + +## Environment Variables + + +For production deployments, use environment variables instead of hardcoding credentials. + +### Provider Selection + +Setting this environment variable allows the Descope provider to be used automatically without explicitly instantiating it in code. + + + +Set to `fastmcp.server.auth.providers.descope.DescopeProvider` to use Descope authentication. + + + +### Descope-Specific Configuration + +These environment variables provide default values for the Descope provider, whether it's instantiated manually or configured via `FASTMCP_SERVER_AUTH`. + + + +Your Descope Project ID from the [Descope Console](https://app.descope.com/settings/project) + + + +Public URL of your FastMCP server (e.g., `https://your-server.com` or `http://localhost:8000` for development) + + + +Descope API base URL for your [region/environment](https://docs.descope.com/management/project-settings/multi-regional) + + + +Example `.env` file: +```bash +# Use the Descope provider +FASTMCP_SERVER_AUTH=fastmcp.server.auth.providers.descope.DescopeProvider + +# Descope configuration +FASTMCP_SERVER_AUTH_DESCOPEPROVIDER_PROJECT_ID=P2abc...123 +FASTMCP_SERVER_AUTH_DESCOPEPROVIDER_BASE_URL=https://your-server.com +FASTMCP_SERVER_AUTH_DESCOPEPROVIDER_DESCOPE_BASE_URL=https://api.descope.com +``` + +With environment variables set, your server code simplifies to: + +```python server.py +from fastmcp import FastMCP + +# Authentication is automatically configured from environment +mcp = FastMCP(name="My Descope Protected Server") +``` \ No newline at end of file diff --git a/docs/integrations/eunomia-authorization.mdx b/docs/integrations/eunomia-authorization.mdx new file mode 100644 index 000000000..bccfbccdb --- /dev/null +++ b/docs/integrations/eunomia-authorization.mdx @@ -0,0 +1,129 @@ +--- +title: Eunomia Authorization ๐Ÿค FastMCP +sidebarTitle: Eunomia Auth +description: Add policy-based authorization to your FastMCP servers with Eunomia +icon: shield-check +--- + +Add **policy-based authorization** to your FastMCP servers with one-line code addition with the **[Eunomia][eunomia-github] authorization middleware**. + +Control which tools, resources and prompts MCP clients can view and execute on your server. Define dynamic JSON-based policies and obtain a comprehensive audit log of all access attempts and violations. + +## How it Works + +Exploiting FastMCP's [Middleware][fastmcp-middleare], the Eunomia middleware intercepts all MCP requests to your server and automatically maps MCP methods to authorization checks. + +### Listing Operations + +The middleware behaves as a filter for listing operations (`tools/list`, `resources/list`, `prompts/list`), hiding to the client components that are not authorized by the defined policies. + +```mermaid +sequenceDiagram + participant MCPClient as MCP Client + participant EunomiaMiddleware as Eunomia Middleware + participant MCPServer as FastMCP Server + participant EunomiaServer as Eunomia Server + + MCPClient->>EunomiaMiddleware: MCP Listing Request (e.g., tools/list) + EunomiaMiddleware->>MCPServer: MCP Listing Request + MCPServer-->>EunomiaMiddleware: MCP Listing Response + EunomiaMiddleware->>EunomiaServer: Authorization Checks + EunomiaServer->>EunomiaMiddleware: Authorization Decisions + EunomiaMiddleware-->>MCPClient: Filtered MCP Listing Response +``` + +### Execution Operations + +The middleware behaves as a firewall for execution operations (`tools/call`, `resources/read`, `prompts/get`), blocking operations that are not authorized by the defined policies. + +```mermaid +sequenceDiagram + participant MCPClient as MCP Client + participant EunomiaMiddleware as Eunomia Middleware + participant MCPServer as FastMCP Server + participant EunomiaServer as Eunomia Server + + MCPClient->>EunomiaMiddleware: MCP Execution Request (e.g., tools/call) + EunomiaMiddleware->>EunomiaServer: Authorization Check + EunomiaServer->>EunomiaMiddleware: Authorization Decision + EunomiaMiddleware-->>MCPClient: MCP Unauthorized Error (if denied) + EunomiaMiddleware->>MCPServer: MCP Execution Request (if allowed) + MCPServer-->>EunomiaMiddleware: MCP Execution Response (if allowed) + EunomiaMiddleware-->>MCPClient: MCP Execution Response (if allowed) +``` + +## Add Authorization to Your Server + + +Eunomia is an AI-specific authorization server that handles policy decisions. The server runs embedded within your MCP server by default for a zero-effort configuration, but can alternatively be run remotely for centralized policy decisions. + + + +### Create a Server with Authorization + +First, install the `eunomia-mcp` package: + +```bash +pip install eunomia-mcp +``` + +Then create a FastMCP server and add the Eunomia middleware in one line: + +```python server.py +from fastmcp import FastMCP +from eunomia_mcp import create_eunomia_middleware + +# Create your FastMCP server +mcp = FastMCP("Secure MCP Server ๐Ÿ”’") + +@mcp.tool() +def add(a: int, b: int) -> int: + """Add two numbers""" + return a + b + +# Add middleware to your server +middleware = create_eunomia_middleware(policy_file="mcp_policies.json") +mcp.add_middleware(middleware) + +if __name__ == "__main__": + mcp.run() +``` + +### Configure Access Policies + +Use the `eunomia-mcp` CLI in your terminal to manage your authorization policies: + +```bash +# Create a default policy file +eunomia-mcp init + +# Or create a policy file customized for your FastMCP server +eunomia-mcp init --custom-mcp "app.server:mcp" +``` + +This creates `mcp_policies.json` file that you can further edit to your access control needs. + +```bash +# Once edited, validate your policy file +eunomia-mcp validate mcp_policies.json +``` + +### Run the Server + +Start your FastMCP server normally: + +```bash +python server.py +``` + +The middleware will now intercept all MCP requests and check them against your policies. Requests include agent identification through headers like `X-Agent-ID`, `X-User-ID`, `User-Agent`, or `Authorization` and an automatic mapping of MCP methods to authorization resources and actions. + + + For detailed policy configuration, custom authentication, and remote + deployments, visit the [Eunomia MCP Middleware + repository][eunomia-mcp-github]. + + +[eunomia-github]: https://github.com/whataboutyou-ai/eunomia +[eunomia-mcp-github]: https://github.com/whataboutyou-ai/eunomia/tree/main/pkgs/extensions/mcp +[fastmcp-middleare]: /servers/middleware diff --git a/docs/integrations/fastapi.mdx b/docs/integrations/fastapi.mdx new file mode 100644 index 000000000..53bd9be9c --- /dev/null +++ b/docs/integrations/fastapi.mdx @@ -0,0 +1,452 @@ +--- +title: FastAPI ๐Ÿค FastMCP +sidebarTitle: FastAPI +description: Integrate FastMCP with FastAPI applications +icon: bolt +--- + +import { VersionBadge } from '/snippets/version-badge.mdx' + + +**New in 2.11**: FastMCP is introducing a next-generation OpenAPI parser. The new parser has greatly improved performance and compatibility, and is also easier to maintain. To enable it, set the environment variable `FASTMCP_EXPERIMENTAL_ENABLE_NEW_OPENAPI_PARSER=true`. + +The new parser is largely API-compatible with the existing implementation and will become the default in a future version. We encourage all users to test it and report any issues before it becomes the default. + + +FastMCP provides two powerful ways to integrate with FastAPI applications: + +1. **[Generate an MCP server FROM your FastAPI app](#generating-an-mcp-server)** - Convert existing API endpoints into MCP tools +2. **[Mount an MCP server INTO your FastAPI app](#mounting-an-mcp-server)** - Add MCP functionality to your web application + + + +Generating MCP servers from OpenAPI is a great way to get started with FastMCP, but in practice LLMs achieve **significantly better performance** with well-designed and curated MCP servers than with auto-converted OpenAPI servers. This is especially true for complex APIs with many endpoints and parameters. + +We recommend using the FastAPI integration for bootstrapping and prototyping, not for mirroring your API to LLM clients. See the post [Stop Converting Your REST APIs to MCP](https://www.jlowin.dev/blog/stop-converting-rest-apis-to-mcp) for more details. + + + + +FastMCP does *not* include FastAPI as a dependency; you must install it separately to use this integration. + + +## Example FastAPI Application + +Throughout this guide, we'll use this e-commerce API as our example (click the `Copy` button to copy it for use with other code blocks): + +```python [expandable] +# Copy this FastAPI server into other code blocks in this guide + +from fastapi import FastAPI, HTTPException +from pydantic import BaseModel + +# Models +class Product(BaseModel): + name: str + price: float + category: str + description: str | None = None + +class ProductResponse(BaseModel): + id: int + name: str + price: float + category: str + description: str | None = None + +# Create FastAPI app +app = FastAPI(title="E-commerce API", version="1.0.0") + +# In-memory database +products_db = { + 1: ProductResponse( + id=1, name="Laptop", price=999.99, category="Electronics" + ), + 2: ProductResponse( + id=2, name="Mouse", price=29.99, category="Electronics" + ), + 3: ProductResponse( + id=3, name="Desk Chair", price=299.99, category="Furniture" + ), +} +next_id = 4 + +@app.get("/products", response_model=list[ProductResponse]) +def list_products( + category: str | None = None, + max_price: float | None = None, +) -> list[ProductResponse]: + """List all products with optional filtering.""" + products = list(products_db.values()) + if category: + products = [p for p in products if p.category == category] + if max_price: + products = [p for p in products if p.price <= max_price] + return products + +@app.get("/products/{product_id}", response_model=ProductResponse) +def get_product(product_id: int): + """Get a specific product by ID.""" + if product_id not in products_db: + raise HTTPException(status_code=404, detail="Product not found") + return products_db[product_id] + +@app.post("/products", response_model=ProductResponse) +def create_product(product: Product): + """Create a new product.""" + global next_id + product_response = ProductResponse(id=next_id, **product.model_dump()) + products_db[next_id] = product_response + next_id += 1 + return product_response + +@app.put("/products/{product_id}", response_model=ProductResponse) +def update_product(product_id: int, product: Product): + """Update an existing product.""" + if product_id not in products_db: + raise HTTPException(status_code=404, detail="Product not found") + products_db[product_id] = ProductResponse( + id=product_id, + **product.model_dump(), + ) + return products_db[product_id] + +@app.delete("/products/{product_id}") +def delete_product(product_id: int): + """Delete a product.""" + if product_id not in products_db: + raise HTTPException(status_code=404, detail="Product not found") + del products_db[product_id] + return {"message": "Product deleted"} +``` + + +All subsequent code examples in this guide assume you have the above FastAPI application code already defined. Each example builds upon this base application, `app`. + + +## Generating an MCP Server + + + +One of the most common ways to bootstrap an MCP server is to generate it from an existing FastAPI application. FastMCP will expose your FastAPI endpoints as MCP components (tools, by default) in order to expose your API to LLM clients. + + + +### Basic Conversion + +Convert the FastAPI app to an MCP server with a single line: + +```python {5} +# Assumes the FastAPI app from above is already defined +from fastmcp import FastMCP + +# Convert to MCP server +mcp = FastMCP.from_fastapi(app=app) + +if __name__ == "__main__": + mcp.run() +``` + +### Adding Components + +Your converted MCP server is a full FastMCP instance, meaning you can add new tools, resources, and other components to it just like you would with any other FastMCP instance. + +```python {8-11} +# Assumes the FastAPI app from above is already defined +from fastmcp import FastMCP + +# Convert to MCP server +mcp = FastMCP.from_fastapi(app=app) + +# Add a new tool +@mcp.tool +def get_product(product_id: int) -> ProductResponse: + """Get a product by ID.""" + return products_db[product_id] + +# Run the MCP server +if __name__ == "__main__": + mcp.run() +``` + + + + + +### Interacting with the MCP Server + +Once you've converted your FastAPI app to an MCP server, you can interact with it using the FastMCP client to test functionality before deploying it to an LLM-based application. + +```python {3, } +# Assumes the FastAPI app from above is already defined +from fastmcp import FastMCP +from fastmcp.client import Client +import asyncio + +# Convert to MCP server +mcp = FastMCP.from_fastapi(app=app) + +async def demo(): + async with Client(mcp) as client: + # List available tools + tools = await client.list_tools() + print(f"Available tools: {[t.name for t in tools]}") + + # Create a product + result = await client.call_tool( + "create_product_products_post", + { + "name": "Wireless Keyboard", + "price": 79.99, + "category": "Electronics", + "description": "Bluetooth mechanical keyboard" + } + ) + print(f"Created product: {result.data}") + + # List electronics under $100 + result = await client.call_tool( + "list_products_products_get", + {"category": "Electronics", "max_price": 100} + ) + print(f"Affordable electronics: {result.data}") + +if __name__ == "__main__": + asyncio.run(demo()) +``` + +### Custom Route Mapping + +Because FastMCP's FastAPI integration is based on its [OpenAPI integration](/integrations/openapi), you can customize how endpoints are converted to MCP components in exactly the same way. For example, here we use a `RouteMap` to map all GET requests to MCP resources, and all POST/PUT/DELETE requests to MCP tools: + +```python +# Assumes the FastAPI app from above is already defined +from fastmcp import FastMCP +from fastmcp.server.openapi import RouteMap, MCPType + +# If using experimental parser, import from experimental module: +# from fastmcp.experimental.server.openapi import RouteMap, MCPType + +# Custom mapping rules +mcp = FastMCP.from_fastapi( + app=app, + route_maps=[ + # GET with path params โ†’ ResourceTemplates + RouteMap( + methods=["GET"], + pattern=r".*\{.*\}.*", + mcp_type=MCPType.RESOURCE_TEMPLATE + ), + # Other GETs โ†’ Resources + RouteMap( + methods=["GET"], + pattern=r".*", + mcp_type=MCPType.RESOURCE + ), + # POST/PUT/DELETE โ†’ Tools (default) + ], +) + +# Now: +# - GET /products โ†’ Resource +# - GET /products/{id} โ†’ ResourceTemplate +# - POST/PUT/DELETE โ†’ Tools +``` + + +To learn more about customizing the conversion process, see the [OpenAPI Integration guide](/integrations/openapi). + + +### Authentication and Headers + +You can configure headers and other client options via the `httpx_client_kwargs` parameter. For example, to add authentication to your FastAPI app, you can pass a `headers` dictionary to the `httpx_client_kwargs` parameter: + +```python {27-31} +# Assumes the FastAPI app from above is already defined +from fastmcp import FastMCP + +# Add authentication to your FastAPI app +from fastapi import Depends, Header +from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials + +security = HTTPBearer() + +def verify_token(credentials: HTTPAuthorizationCredentials = Depends(security)): + if credentials.credentials != "secret-token": + raise HTTPException(status_code=401, detail="Invalid authentication") + return credentials.credentials + +# Add a protected endpoint +@app.get("/admin/stats", dependencies=[Depends(verify_token)]) +def get_admin_stats(): + return { + "total_products": len(products_db), + "categories": list(set(p.category for p in products_db.values())) + } + +# Create MCP server with authentication headers +mcp = FastMCP.from_fastapi( + app=app, + httpx_client_kwargs={ + "headers": { + "Authorization": "Bearer secret-token", + } + } +) +``` + +## Mounting an MCP Server + + + +In addition to generating servers, FastMCP can facilitate adding MCP servers to your existing FastAPI application. You can do this by mounting the MCP ASGI application. + +### Basic Mounting + +To mount an MCP server, you can use the `http_app` method on your FastMCP instance. This will return an ASGI application that can be mounted to your FastAPI application. + +```python {23-30} +from fastmcp import FastMCP +from fastapi import FastAPI + +# Create MCP server +mcp = FastMCP("Analytics Tools") + +@mcp.tool +def analyze_pricing(category: str) -> dict: + """Analyze pricing for a category.""" + products = [p for p in products_db.values() if p.category == category] + if not products: + return {"error": f"No products in {category}"} + + prices = [p.price for p in products] + return { + "category": category, + "avg_price": round(sum(prices) / len(prices), 2), + "min": min(prices), + "max": max(prices), + } + +# Create ASGI app from MCP server +mcp_app = mcp.http_app(path='/mcp') + +# Key: Pass lifespan to FastAPI +app = FastAPI(title="E-commerce API", lifespan=mcp_app.lifespan) + +# Mount the MCP server +app.mount("/analytics", mcp_app) + +# Now: API at /products/*, MCP at /analytics/mcp/ +``` + +## Offering an LLM-Friendly API + +A common pattern is to generate an MCP server from your FastAPI app and serve both interfaces from the same application. This provides an LLM-optimized interface alongside your regular API: + +```python +# Assumes the FastAPI app from above is already defined +from fastmcp import FastMCP +from fastapi import FastAPI + +# 1. Generate MCP server from your API +mcp = FastMCP.from_fastapi(app=app, name="E-commerce MCP") + +# 2. Create the MCP's ASGI app +mcp_app = mcp.http_app(path='/mcp') + +# 3. Create a new FastAPI app that combines both sets of routes +combined_app = FastAPI( + title="E-commerce API with MCP", + routes=[ + *mcp_app.routes, # MCP routes + *app.routes, # Original API routes + ], + lifespan=mcp_app.lifespan, +) + +# Now you have: +# - Regular API: http://localhost:8000/products +# - LLM-friendly MCP: http://localhost:8000/mcp/ +# Both served from the same FastAPI application! +``` + +This approach lets you maintain a single codebase while offering both traditional REST endpoints and MCP-compatible endpoints for LLM clients. + +## Key Considerations + +### Operation IDs + +FastAPI operation IDs become MCP component names. Always specify meaningful operation IDs: + +```python +# Good - explicit operation_id +@app.get("/users/{user_id}", operation_id="get_user_by_id") +def get_user(user_id: int): + return {"id": user_id} + +# Less ideal - auto-generated name +@app.get("/users/{user_id}") +def get_user(user_id: int): + return {"id": user_id} +``` + +### Lifespan Management + +When mounting MCP servers, always pass the lifespan context: + +```python +# Correct - lifespan passed +mcp_app = mcp.http_app(path='/mcp') +app = FastAPI(lifespan=mcp_app.lifespan) +app.mount("/mcp", mcp_app) + +# Incorrect - missing lifespan +app = FastAPI() +app.mount("/mcp", mcp.http_app()) # Session manager won't initialize +``` + +### Combining Lifespans + +If your FastAPI app already has a lifespan (for database connections, startup tasks, etc.), you can't simply replace it with the MCP lifespan. Instead, you need to create a new lifespan function that manages both contexts. This ensures that both your app's initialization logic and the MCP server's session manager run properly: + +```python +from contextlib import asynccontextmanager +from fastapi import FastAPI +from fastmcp import FastMCP + +# Your existing lifespan +@asynccontextmanager +async def app_lifespan(app: FastAPI): + # Startup + print("Starting up the app...") + # Initialize database, cache, etc. + yield + # Shutdown + print("Shutting down the app...") + +# Create MCP server +mcp = FastMCP("Tools") +mcp_app = mcp.http_app(path='/mcp') + +# Combine both lifespans +@asynccontextmanager +async def combined_lifespan(app: FastAPI): + # Run both lifespans + async with app_lifespan(app): + async with mcp_app.lifespan(app): + yield + +# Use the combined lifespan +app = FastAPI(lifespan=combined_lifespan) +app.mount("/mcp", mcp_app) +``` + +This pattern ensures both your app's initialization logic and the MCP server's session manager are properly managed. The key is using nested `async with` statements - the inner context (MCP) will be initialized after the outer context (your app), and cleaned up before it. This maintains the correct initialization and cleanup order for all your resources. + +### Performance Tips + +1. **Use in-memory transport for testing** - Pass MCP servers directly to clients +2. **Design purpose-built MCP tools** - Better than auto-converting complex APIs +3. **Keep tool parameters simple** - LLMs perform better with focused interfaces + +For more details on configuration options, see the [OpenAPI Integration guide](/integrations/openapi). \ No newline at end of file diff --git a/docs/integrations/gemini-cli.mdx b/docs/integrations/gemini-cli.mdx new file mode 100644 index 000000000..af63a5d32 --- /dev/null +++ b/docs/integrations/gemini-cli.mdx @@ -0,0 +1,174 @@ +--- +title: Gemini CLI ๐Ÿค FastMCP +sidebarTitle: Gemini CLI +description: Install and use FastMCP servers in Gemini CLI +icon: message-smile +tag: NEW +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" +import { LocalFocusTip } from "/snippets/local-focus.mdx" + + + +Gemini CLI supports MCP servers through multiple transport methods including STDIO, SSE, and HTTP, allowing you to extend Gemini's capabilities with custom tools, resources, and prompts from your FastMCP servers. + +## Requirements + +This integration uses STDIO transport to run your FastMCP server locally. For remote deployments, you can run your FastMCP server with HTTP or SSE transport and configure it directly using Gemini CLI's built-in MCP management commands. + +## Create a Server + +The examples in this guide will use the following simple dice-rolling server, saved as `server.py`. + +```python server.py +import random +from fastmcp import FastMCP + +mcp = FastMCP(name="Dice Roller") + +@mcp.tool +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)] + +if __name__ == "__main__": + mcp.run() +``` + +## Install the Server + +### FastMCP CLI + + +The easiest way to install a FastMCP server in Gemini CLI is using the `fastmcp install gemini-cli` command. This automatically handles the configuration, dependency management, and calls Gemini CLI's built-in MCP management system. + +```bash +fastmcp install gemini-cli server.py +``` + +The install command supports the same `file.py:object` notation as the `run` command. If no object is specified, it will automatically look for a FastMCP server object named `mcp`, `server`, or `app` in your file: + +```bash +# These are equivalent if your server object is named 'mcp' +fastmcp install gemini-cli server.py +fastmcp install gemini-cli server.py:mcp + +# Use explicit object name if your server has a different name +fastmcp install gemini-cli server.py:my_custom_server +``` + +The command will automatically configure the server with Gemini CLI's `gemini mcp add` command. + +#### Dependencies + +FastMCP provides flexible dependency management options for your Gemini CLI servers: + +**Individual packages**: Use the `--with` flag to specify packages your server needs. You can use this flag multiple times: + +```bash +fastmcp install gemini-cli server.py --with pandas --with requests +``` + +**Requirements file**: If you maintain a `requirements.txt` file with all your dependencies, use `--with-requirements` to install them: + +```bash +fastmcp install gemini-cli server.py --with-requirements requirements.txt +``` + +**Editable packages**: For local packages under development, use `--with-editable` to install them in editable mode: + +```bash +fastmcp install gemini-cli server.py --with-editable ./my-local-package +``` + +Alternatively, you can use a `fastmcp.json` configuration file (recommended): + +```json fastmcp.json +{ + "$schema": "https://gofastmcp.com/public/schemas/fastmcp.json/v1.json", + "source": { + "path": "server.py", + "entrypoint": "mcp" + }, + "environment": { + "dependencies": ["pandas", "requests"] + } +} +``` + + +#### Python Version and Project Configuration + +Control the Python environment for your server with these options: + +**Python version**: Use `--python` to specify which Python version your server requires. This ensures compatibility when your server needs specific Python features: + +```bash +fastmcp install gemini-cli server.py --python 3.11 +``` + +**Project directory**: Use `--project` to run your server within a specific project context. This tells `uv` to use the project's configuration files and virtual environment: + +```bash +fastmcp install gemini-cli server.py --project /path/to/my-project +``` + +#### Environment Variables + +If your server needs environment variables (like API keys), you must include them: + +```bash +fastmcp install gemini-cli server.py --server-name "Weather Server" \ + --env API_KEY=your-api-key \ + --env DEBUG=true +``` + +Or load them from a `.env` file: + +```bash +fastmcp install gemini-cli server.py --server-name "Weather Server" --env-file .env +``` + + +**Gemini CLI must be installed**. The integration looks for the Gemini CLI and uses the `gemini mcp add` command to register servers. + + +### Manual Configuration + +For more control over the configuration, you can manually use Gemini CLI's built-in MCP management commands. This gives you direct control over how your server is launched: + +```bash +# Add a server with custom configuration +gemini mcp add dice-roller uv -- run --with fastmcp fastmcp run server.py + +# Add with environment variables +gemini mcp add weather-server -e API_KEY=secret -e DEBUG=true uv -- run --with fastmcp fastmcp run server.py + +# Add with specific scope (user, or project) +gemini mcp add my-server --scope user uv -- run --with fastmcp fastmcp run server.py +``` + +You can also manually specify Python versions and project directories in your Gemini CLI commands: + +```bash +# With specific Python version +gemini mcp add ml-server uv -- run --python 3.11 --with fastmcp fastmcp run server.py + +# Within a project directory +gemini mcp add project-server uv -- run --project /path/to/project --with fastmcp fastmcp run server.py +``` + +## Using the Server + +Once your server is installed, you can start using your FastMCP server with Gemini CLI. + +Try asking Gemini something like: + +> "Roll some dice for me" + +Gemini will automatically detect your `roll_dice` tool and use it to fulfill your request. + +Gemini CLI can now access all the tools and prompts you've defined in your FastMCP server. + +If your server provides prompts, you can use them as slash commands with `/prompt_name`. diff --git a/docs/integrations/gemini.mdx b/docs/integrations/gemini.mdx new file mode 100644 index 000000000..159e662a5 --- /dev/null +++ b/docs/integrations/gemini.mdx @@ -0,0 +1,108 @@ +--- +title: Gemini SDK ๐Ÿค FastMCP +sidebarTitle: Gemini SDK +description: Connect FastMCP servers to the Google Gemini SDK +icon: message-code +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + +Google's Gemini API includes built-in support for MCP servers in their Python and JavaScript SDKs, allowing you to connect directly to MCP servers and use their tools seamlessly with Gemini models. + +## Gemini Python SDK + +Google's [Gemini Python SDK](https://ai.google.dev/gemini-api/docs) can use FastMCP clients directly. + + +Google's MCP integration is currently experimental and available in the Python and JavaScript SDKs. The API automatically calls MCP tools when needed and can connect to both local and remote MCP servers. + + + +Currently, Gemini's MCP support only accesses **tools** from MCP serversโ€”it queries the `list_tools` endpoint and exposes those functions to the AI. Other MCP features like resources and prompts are not currently supported. + + +### Create a Server + +First, create a FastMCP server with the tools you want to expose. For this example, we'll create a server with a single tool that rolls dice. + +```python server.py +import random +from fastmcp import FastMCP + +mcp = FastMCP(name="Dice Roller") + +@mcp.tool +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)] + +if __name__ == "__main__": + mcp.run() +``` + +### Call the Server + + +To use the Gemini API with MCP, you'll need to install the Google Generative AI SDK: + +```bash +pip install google-genai +``` + +You'll also need to authenticate with Google. You can do this by setting the `GEMINI_API_KEY` environment variable. Consult the Gemini SDK documentation for more information. + +```bash +export GEMINI_API_KEY="your-api-key" +``` + +Gemini's SDK interacts directly with the MCP client session. To call the server, you'll need to instantiate a FastMCP client, enter its connection context, and pass the client session to the Gemini SDK. + +```python {5, 9, 15} +from fastmcp import Client +from google import genai +import asyncio + +mcp_client = Client("server.py") +gemini_client = genai.Client() + +async def main(): + async with mcp_client: + response = await gemini_client.aio.models.generate_content( + model="gemini-2.0-flash", + contents="Roll 3 dice!", + config=genai.types.GenerateContentConfig( + temperature=0, + tools=[mcp_client.session], # Pass the FastMCP client session + ), + ) + print(response.text) + +if __name__ == "__main__": + asyncio.run(main()) +``` + +If you run this code, you'll see output like: + +```text +Okay, I rolled 3 dice and got a 5, 4, and 1. +``` + +### Remote & Authenticated Servers + +In the above example, we connected to our local server using `stdio` transport. Because we're using a FastMCP client, you can also connect to any local or remote MCP server, using any [transport](/clients/transports) or [auth](/clients/auth) method supported by FastMCP, simply by changing the client configuration. + +For example, to connect to a remote, authenticated server, you can use the following client: + +```python +from fastmcp import Client +from fastmcp.client.auth import BearerAuth + +mcp_client = Client( + "https://my-server.com/mcp/", + auth=BearerAuth(""), +) +``` + +The rest of the code remains the same. + + diff --git a/docs/integrations/github.mdx b/docs/integrations/github.mdx new file mode 100644 index 000000000..2414dd253 --- /dev/null +++ b/docs/integrations/github.mdx @@ -0,0 +1,209 @@ +--- +title: GitHub OAuth ๐Ÿค FastMCP +sidebarTitle: GitHub +description: Secure your FastMCP server with GitHub OAuth +icon: github +tag: NEW +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + + + +This guide shows you how to secure your FastMCP server using **GitHub OAuth**. Since GitHub doesn't support Dynamic Client Registration, this integration uses the [**OAuth Proxy**](/servers/auth/oauth-proxy) pattern to bridge GitHub's traditional OAuth with MCP's authentication requirements. + +## Configuration + +### Prerequisites + +Before you begin, you will need: +1. A **[GitHub Account](https://github.com/)** with access to create OAuth Apps +2. Your FastMCP server's URL (can be localhost for development, e.g., `http://localhost:8000`) + +### Step 1: Create a GitHub OAuth App + +Create an OAuth App in your GitHub settings to get the credentials needed for authentication: + + + + Go to **Settings โ†’ Developer settings โ†’ OAuth Apps** in your GitHub account, or visit [github.com/settings/developers](https://github.com/settings/developers). + + Click **"New OAuth App"** to create a new application. + + + + Fill in the application details: + + - **Application name**: Choose a name users will recognize (e.g., "My FastMCP Server") + - **Homepage URL**: Your application's homepage or documentation URL + - **Authorization callback URL**: Your server URL + `/auth/callback` (e.g., `http://localhost:8000/auth/callback`) + + + The callback URL must match exactly. The default path is `/auth/callback`, but you can customize it using the `redirect_path` parameter. For local development, GitHub allows `http://localhost` URLs. For production, you must use HTTPS. + + + + If you want to use a custom callback path (e.g., `/auth/github/callback`), make sure to set the same path in both your GitHub OAuth App settings and the `redirect_path` parameter when configuring the GitHubProvider. + + + + + After creating the app, you'll see: + + - **Client ID**: A public identifier like `Ov23liAbcDefGhiJkLmN` + - **Client Secret**: Click "Generate a new client secret" and save the value securely + + + Store these credentials securely. Never commit them to version control. Use environment variables or a secrets manager in production. + + + + +### Step 2: FastMCP Configuration + +Create your FastMCP server using the `GitHubProvider`, which handles GitHub's OAuth quirks automatically: + +```python server.py +from fastmcp import FastMCP +from fastmcp.server.auth.providers.github import GitHubProvider + +# The GitHubProvider handles GitHub's token format and validation +auth_provider = GitHubProvider( + client_id="Ov23liAbcDefGhiJkLmN", # Your GitHub OAuth App Client ID + client_secret="github_pat_...", # Your GitHub OAuth App Client Secret + base_url="http://localhost:8000", # Must match your OAuth App configuration + # redirect_path="/auth/callback" # Default value, customize if needed +) + +mcp = FastMCP(name="GitHub Secured App", auth=auth_provider) + +# Add a protected tool to test authentication +@mcp.tool +async def get_user_info() -> dict: + """Returns information about the authenticated GitHub user.""" + from fastmcp.server.dependencies import get_access_token + + token = get_access_token() + # The GitHubProvider stores user data in token claims + return { + "github_user": token.claims.get("login"), + "name": token.claims.get("name"), + "email": token.claims.get("email") + } +``` + +## Testing + +### Running the Server + +Start your FastMCP server with HTTP transport to enable OAuth flows: + +```bash +fastmcp run server.py --transport http --port 8000 +``` + +Your server is now running and protected by GitHub OAuth authentication. + +### Testing with a Client + +Create a test client that authenticates with your GitHub-protected server: + +```python test_client.py +from fastmcp import Client +import asyncio + +async def main(): + # The client will automatically handle GitHub OAuth + async with Client("http://localhost:8000/mcp/", auth="oauth") as client: + # First-time connection will open GitHub login in your browser + print("โœ“ Authenticated with GitHub!") + + # Test the protected tool + result = await client.call_tool("get_user_info") + print(f"GitHub user: {result['github_user']}") + +if __name__ == "__main__": + asyncio.run(main()) +``` + +When you run the client for the first time: +1. Your browser will open to GitHub's authorization page +2. After you authorize the app, you'll be redirected back +3. The client receives the token and can make authenticated requests + + +The client caches tokens locally, so you won't need to re-authenticate for subsequent runs unless the token expires or you explicitly clear the cache. + + +## Environment Variables + + + +For production deployments, use environment variables instead of hardcoding credentials. + +### Provider Selection + +Setting this environment variable allows the GitHub provider to be used automatically without explicitly instantiating it in code. + + + +Set to `fastmcp.server.auth.providers.github.GitHubProvider` to use GitHub authentication. + + + +### GitHub-Specific Configuration + +These environment variables provide default values for the GitHub provider, whether it's instantiated manually or configured via `FASTMCP_SERVER_AUTH`. + + + +Your GitHub OAuth App Client ID (e.g., `Ov23liAbcDefGhiJkLmN`) + + + +Your GitHub OAuth App Client Secret + + + +Public URL of your FastMCP server for OAuth callbacks + + + +Redirect path configured in your GitHub OAuth App + + + +Comma-, space-, or JSON-separated list of required GitHub scopes (e.g., `user repo` or `["user","repo"]`) + + + +HTTP request timeout for GitHub API calls + + + +Example `.env` file: +```bash +# Use the GitHub provider +FASTMCP_SERVER_AUTH=fastmcp.server.auth.providers.github.GitHubProvider + +# GitHub OAuth credentials +FASTMCP_SERVER_AUTH_GITHUB_CLIENT_ID=Ov23liAbcDefGhiJkLmN +FASTMCP_SERVER_AUTH_GITHUB_CLIENT_SECRET=github_pat_... +FASTMCP_SERVER_AUTH_GITHUB_BASE_URL=https://your-server.com +FASTMCP_SERVER_AUTH_GITHUB_REQUIRED_SCOPES=user,repo +``` + +With environment variables set, your server code simplifies to: + +```python server.py +from fastmcp import FastMCP + +# Authentication is automatically configured from environment +mcp = FastMCP(name="GitHub Secured App") + +@mcp.tool +async def list_repos() -> list[str]: + """List the authenticated user's repositories.""" + # Your tool implementation here + pass +``` diff --git a/docs/integrations/google.mdx b/docs/integrations/google.mdx new file mode 100644 index 000000000..9e3cf8bab --- /dev/null +++ b/docs/integrations/google.mdx @@ -0,0 +1,222 @@ +--- +title: Google OAuth ๐Ÿค FastMCP +sidebarTitle: Google +description: Secure your FastMCP server with Google OAuth +icon: google +tag: NEW +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + + + +This guide shows you how to secure your FastMCP server using **Google OAuth**. Since Google doesn't support Dynamic Client Registration, this integration uses the [**OAuth Proxy**](/servers/auth/oauth-proxy) pattern to bridge Google's traditional OAuth with MCP's authentication requirements. + +## Configuration + +### Prerequisites + +Before you begin, you will need: +1. A **[Google Cloud Account](https://console.cloud.google.com/)** with access to create OAuth 2.0 Client IDs +2. Your FastMCP server's URL (can be localhost for development, e.g., `http://localhost:8000`) + +### Step 1: Create a Google OAuth 2.0 Client ID + +Create an OAuth 2.0 Client ID in your Google Cloud Console to get the credentials needed for authentication: + + + + Go to the [Google Cloud Console](https://console.cloud.google.com/apis/credentials) and select your project (or create a new one). + + First, configure the OAuth consent screen by navigating to **APIs & Services โ†’ OAuth consent screen**. Choose "External" for testing or "Internal" for G Suite organizations. + + + + Navigate to **APIs & Services โ†’ Credentials** and click **"+ CREATE CREDENTIALS"** โ†’ **"OAuth client ID"**. + + Configure your OAuth client: + + - **Application type**: Web application + - **Name**: Choose a descriptive name (e.g., "FastMCP Server") + - **Authorized JavaScript origins**: Add your server's base URL (e.g., `http://localhost:8000`) + - **Authorized redirect URIs**: Add your server URL + `/auth/callback` (e.g., `http://localhost:8000/auth/callback`) + + + The redirect URI must match exactly. The default path is `/auth/callback`, but you can customize it using the `redirect_path` parameter. For local development, Google allows `http://localhost` URLs with various ports. For production, you must use HTTPS. + + + + If you want to use a custom callback path (e.g., `/auth/google/callback`), make sure to set the same path in both your Google OAuth Client settings and the `redirect_path` parameter when configuring the GoogleProvider. + + + + + After creating the client, you'll receive: + + - **Client ID**: A string ending in `.apps.googleusercontent.com` + - **Client Secret**: A string starting with `GOCSPX-` + + Download the JSON credentials or copy these values securely. + + + Store these credentials securely. Never commit them to version control. Use environment variables or a secrets manager in production. + + + + +### Step 2: FastMCP Configuration + +Create your FastMCP server using the `GoogleProvider`, which handles Google's OAuth flow automatically: + +```python server.py +from fastmcp import FastMCP +from fastmcp.server.auth.providers.google import GoogleProvider + +# The GoogleProvider handles Google's token format and validation +auth_provider = GoogleProvider( + client_id="123456789.apps.googleusercontent.com", # Your Google OAuth Client ID + client_secret="GOCSPX-abc123...", # Your Google OAuth Client Secret + base_url="http://localhost:8000", # Must match your OAuth configuration + required_scopes=[ # Request user information + "openid", + "https://www.googleapis.com/auth/userinfo.email", + ], + # redirect_path="/auth/callback" # Default value, customize if needed +) + +mcp = FastMCP(name="Google Secured App", auth=auth_provider) + +# Add a protected tool to test authentication +@mcp.tool +async def get_user_info() -> dict: + """Returns information about the authenticated Google user.""" + from fastmcp.server.dependencies import get_access_token + + token = get_access_token() + # The GoogleProvider stores user data in token claims + return { + "google_id": token.claims.get("sub"), + "email": token.claims.get("email"), + "name": token.claims.get("name"), + "picture": token.claims.get("picture"), + "locale": token.claims.get("locale") + } +``` + +## Testing + +### Running the Server + +Start your FastMCP server with HTTP transport to enable OAuth flows: + +```bash +fastmcp run server.py --transport http --port 8000 +``` + +Your server is now running and protected by Google OAuth authentication. + +### Testing with a Client + +Create a test client that authenticates with your Google-protected server: + +```python test_client.py +from fastmcp import Client +import asyncio + +async def main(): + # The client will automatically handle Google OAuth + async with Client("http://localhost:8000/mcp/", auth="oauth") as client: + # First-time connection will open Google login in your browser + print("โœ“ Authenticated with Google!") + + # Test the protected tool + result = await client.call_tool("get_user_info") + print(f"Google user: {result['email']}") + print(f"Name: {result['name']}") + +if __name__ == "__main__": + asyncio.run(main()) +``` + +When you run the client for the first time: +1. Your browser will open to Google's authorization page +2. Sign in with your Google account and grant the requested permissions +3. After authorization, you'll be redirected back +4. The client receives the token and can make authenticated requests + + +The client caches tokens locally, so you won't need to re-authenticate for subsequent runs unless the token expires or you explicitly clear the cache. + + +## Environment Variables + + + +For production deployments, use environment variables instead of hardcoding credentials. + +### Provider Selection + +Setting this environment variable allows the Google provider to be used automatically without explicitly instantiating it in code. + + + +Set to `fastmcp.server.auth.providers.google.GoogleProvider` to use Google authentication. + + + +### Google-Specific Configuration + +These environment variables provide default values for the Google provider, whether it's instantiated manually or configured via `FASTMCP_SERVER_AUTH`. + + + +Your Google OAuth 2.0 Client ID (e.g., `123456789.apps.googleusercontent.com`) + + + +Your Google OAuth 2.0 Client Secret (e.g., `GOCSPX-abc123...`) + + + +Public URL of your FastMCP server for OAuth callbacks + + + +Redirect path configured in your Google OAuth Client + + + +Comma-, space-, or JSON-separated list of required Google scopes (e.g., `"openid,https://www.googleapis.com/auth/userinfo.email"` or `["openid", "https://www.googleapis.com/auth/userinfo.email"]`) + + + +HTTP request timeout for Google API calls + + + +Example `.env` file: +```bash +# Use the Google provider +FASTMCP_SERVER_AUTH=fastmcp.server.auth.providers.google.GoogleProvider + +# Google OAuth credentials +FASTMCP_SERVER_AUTH_GOOGLE_CLIENT_ID=123456789.apps.googleusercontent.com +FASTMCP_SERVER_AUTH_GOOGLE_CLIENT_SECRET=GOCSPX-abc123... +FASTMCP_SERVER_AUTH_GOOGLE_BASE_URL=https://your-server.com +FASTMCP_SERVER_AUTH_GOOGLE_REQUIRED_SCOPES=openid,https://www.googleapis.com/auth/userinfo.email +``` + +With environment variables set, your server code simplifies to: + +```python server.py +from fastmcp import FastMCP + +# Authentication is automatically configured from environment +mcp = FastMCP(name="Google Secured App") + +@mcp.tool +async def protected_tool(query: str) -> str: + """A tool that requires Google authentication to access.""" + # Your tool implementation here + return f"Processing authenticated request: {query}" +``` \ No newline at end of file diff --git a/docs/integrations/images/authkit/enable_dcr.png b/docs/integrations/images/authkit/enable_dcr.png new file mode 100644 index 000000000..e5942f60e Binary files /dev/null and b/docs/integrations/images/authkit/enable_dcr.png differ diff --git a/docs/integrations/images/permit/abac_condition_example.png b/docs/integrations/images/permit/abac_condition_example.png new file mode 100644 index 000000000..a5592abca Binary files /dev/null and b/docs/integrations/images/permit/abac_condition_example.png differ diff --git a/docs/integrations/images/permit/abac_policy_example.png b/docs/integrations/images/permit/abac_policy_example.png new file mode 100644 index 000000000..bd4b5cf33 Binary files /dev/null and b/docs/integrations/images/permit/abac_policy_example.png differ diff --git a/docs/integrations/images/permit/policy_mapping.png b/docs/integrations/images/permit/policy_mapping.png new file mode 100644 index 000000000..d100b1eab Binary files /dev/null and b/docs/integrations/images/permit/policy_mapping.png differ diff --git a/docs/integrations/images/permit/role_assignement.png b/docs/integrations/images/permit/role_assignement.png new file mode 100644 index 000000000..c65e34181 Binary files /dev/null and b/docs/integrations/images/permit/role_assignement.png differ diff --git a/docs/integrations/mcp-json-configuration.mdx b/docs/integrations/mcp-json-configuration.mdx new file mode 100644 index 000000000..b44b15e76 --- /dev/null +++ b/docs/integrations/mcp-json-configuration.mdx @@ -0,0 +1,422 @@ +--- +title: MCP JSON Configuration ๐Ÿค FastMCP +sidebarTitle: MCP.json +description: Generate standard MCP configuration files for any compatible client +icon: brackets-curly +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + + + +FastMCP can generate standard MCP JSON configuration files that work with any MCP-compatible client including Claude Desktop, VS Code, Cursor, and other applications that support the Model Context Protocol. + +## MCP JSON Configuration Standard + +The MCP JSON configuration format is an **emergent standard** that has developed across the MCP ecosystem. This format defines how MCP clients should configure and launch MCP servers, providing a consistent way to specify server commands, arguments, and environment variables. + +### Configuration Structure + +The standard uses a `mcpServers` object where each key represents a server name and the value contains the server's configuration: + +```json +{ + "mcpServers": { + "server-name": { + "command": "executable", + "args": ["arg1", "arg2"], + "env": { + "VAR": "value" + } + } + } +} +``` + +### Server Configuration Fields + +#### `command` (required) +The executable command to run the MCP server. This should be an absolute path or a command available in the system PATH. + +```json +{ + "command": "python" +} +``` + +#### `args` (optional) +An array of command-line arguments passed to the server executable. Arguments are passed in order. + +```json +{ + "args": ["server.py", "--verbose", "--port", "8080"] +} +``` + +#### `env` (optional) +An object containing environment variables to set when launching the server. All values must be strings. + +```json +{ + "env": { + "API_KEY": "secret-key", + "DEBUG": "true", + "PORT": "8080" + } +} +``` + +### Client Adoption + +This format is widely adopted across the MCP ecosystem: + +- **Claude Desktop**: Uses `~/.claude/claude_desktop_config.json` +- **Cursor**: Uses `~/.cursor/mcp.json` +- **VS Code**: Uses workspace `.vscode/mcp.json` +- **Other clients**: Many MCP-compatible applications follow this standard + +## Overview + + +**For the best experience, use FastMCP's first-class integrations:** [`fastmcp install claude-code`](/integrations/claude-code), [`fastmcp install claude-desktop`](/integrations/claude-desktop), or [`fastmcp install cursor`](/integrations/cursor). Use MCP JSON generation for advanced use cases and unsupported clients. + + +The `fastmcp install mcp-json` command generates configuration in the standard `mcpServers` format used across the MCP ecosystem. This is useful when: + +- **Working with unsupported clients** - Any MCP client not directly integrated with FastMCP +- **CI/CD environments** - Automated configuration generation for deployments +- **Configuration sharing** - Easy distribution of server setups to team members +- **Custom tooling** - Integration with your own MCP management tools +- **Manual setup** - When you prefer to manually configure your MCP client + +## Basic Usage + +Generate configuration and output to stdout (useful for piping): + +```bash +fastmcp install mcp-json server.py +``` + +This outputs the server configuration JSON with the server name as the root key: + +```json +{ + "My Server": { + "command": "uv", + "args": [ + "run", + "--with", + "fastmcp", + "fastmcp", + "run", + "/absolute/path/to/server.py" + ] + } +} +``` + +To use this in a client configuration file, add it to the `mcpServers` object in your client's configuration: + +```json +{ + "mcpServers": { + "My Server": { + "command": "uv", + "args": [ + "run", + "--with", + "fastmcp", + "fastmcp", + "run", + "/absolute/path/to/server.py" + ] + } + } +} +``` + + +When using `--python`, `--project`, or `--with-requirements`, the generated configuration will include these options in the `uv run` command, ensuring your server runs with the correct Python version and dependencies. + + + +Different MCP clients may have specific configuration requirements or formatting needs. Always consult your client's documentation to ensure proper integration. + + +## Configuration Options + +### Server Naming + +```bash +# Use server's built-in name (from FastMCP constructor) +fastmcp install mcp-json server.py + +# Override with custom name +fastmcp install mcp-json server.py --name "Custom Server Name" +``` + +### Dependencies + +Add Python packages your server needs: + +```bash +# Single package +fastmcp install mcp-json server.py --with pandas + +# Multiple packages +fastmcp install mcp-json server.py --with pandas --with requests --with httpx + +# Editable local package +fastmcp install mcp-json server.py --with-editable ./my-package + +# From requirements file +fastmcp install mcp-json server.py --with-requirements requirements.txt +``` + +You can also use a `fastmcp.json` configuration file (recommended): + +```json fastmcp.json +{ + "$schema": "https://gofastmcp.com/public/schemas/fastmcp.json/v1.json", + "source": { + "path": "server.py", + "entrypoint": "mcp" + }, + "environment": { + "dependencies": ["pandas", "matplotlib", "seaborn"] + } +} +``` + +Then simply install with: +```bash +fastmcp install mcp-json fastmcp.json +``` + + +### Environment Variables + +```bash +# Individual environment variables +fastmcp install mcp-json server.py \ + --env API_KEY=your-secret-key \ + --env DEBUG=true + +# Load from .env file +fastmcp install mcp-json server.py --env-file .env +``` + +### Python Version and Project Directory + +Specify Python version or run within a specific project: + +```bash +# Use specific Python version +fastmcp install mcp-json server.py --python 3.11 + +# Run within a project directory +fastmcp install mcp-json server.py --project /path/to/project +``` + +### Server Object Selection + +Use the same `file.py:object` notation as other FastMCP commands: + +```bash +# Auto-detects server object (looks for 'mcp', 'server', or 'app') +fastmcp install mcp-json server.py + +# Explicit server object +fastmcp install mcp-json server.py:my_custom_server +``` + +## Clipboard Integration + +Copy configuration directly to your clipboard for easy pasting: + +```bash +fastmcp install mcp-json server.py --copy +``` + + +The `--copy` flag requires the `pyperclip` Python package. If not installed, you'll see an error message with installation instructions. + + +## Usage Examples + +### Basic Server + +```bash +fastmcp install mcp-json dice_server.py +``` + +Output: +```json +{ + "Dice Server": { + "command": "uv", + "args": [ + "run", + "--with", + "fastmcp", + "fastmcp", + "run", + "/home/user/dice_server.py" + ] + } +} +``` + +### Production Server with Dependencies + +```bash +fastmcp install mcp-json api_server.py \ + --name "Production API Server" \ + --with requests \ + --with python-dotenv \ + --env API_BASE_URL=https://api.example.com \ + --env TIMEOUT=30 +``` + +### Advanced Configuration + +```bash +fastmcp install mcp-json ml_server.py \ + --name "ML Analysis Server" \ + --python 3.11 \ + --with-requirements requirements.txt \ + --project /home/user/ml-project \ + --env GPU_DEVICE=0 +``` + +Output: +```json +{ + "Production API Server": { + "command": "uv", + "args": [ + "run", + "--with", + "fastmcp", + "--with", + "python-dotenv", + "--with", + "requests", + "fastmcp", + "run", + "/home/user/api_server.py" + ], + "env": { + "API_BASE_URL": "https://api.example.com", + "TIMEOUT": "30" + } + } +} +``` + +The advanced configuration example generates: +```json +{ + "ML Analysis Server": { + "command": "uv", + "args": [ + "run", + "--python", + "3.11", + "--project", + "/home/user/ml-project", + "--with", + "fastmcp", + "--with-requirements", + "requirements.txt", + "fastmcp", + "run", + "/home/user/ml_server.py" + ], + "env": { + "GPU_DEVICE": "0" + } + } +} +``` + +### Pipeline Usage + +Save configuration to file: + +```bash +fastmcp install mcp-json server.py > mcp-config.json +``` + +Use in shell scripts: + +```bash +#!/bin/bash +CONFIG=$(fastmcp install mcp-json server.py --name "CI Server") +echo "$CONFIG" | jq '."CI Server".command' +# Output: "uv" +``` + +## Integration with MCP Clients + +The generated configuration works with any MCP-compatible application: + +### Claude Desktop + +**Prefer [`fastmcp install claude-desktop`](/integrations/claude-desktop)** for automatic installation. Use MCP JSON for advanced configuration needs. + +Copy the `mcpServers` object into `~/.claude/claude_desktop_config.json` + +### Cursor + +**Prefer [`fastmcp install cursor`](/integrations/cursor)** for automatic installation. Use MCP JSON for advanced configuration needs. + +Add to `~/.cursor/mcp.json` + +### VS Code +Add to your workspace's `.vscode/mcp.json` file + +### Custom Applications +Use the JSON configuration with any application that supports the MCP protocol + +## Configuration Format + +The generated configuration outputs a server object with the server name as the root key: + +```json +{ + "": { + "command": "", + "args": ["", "", "..."], + "env": { + "": "" + } + } +} +``` + +To use this in an MCP client, add it to the client's `mcpServers` configuration object. + +**Fields:** +- `command`: The executable to run (always `uv` for FastMCP servers) +- `args`: Command-line arguments including dependencies and server path +- `env`: Environment variables (only included if specified) + + +**All file paths in the generated configuration are absolute paths**. This ensures the configuration works regardless of the working directory when the MCP client starts the server. + + +## Requirements + +- **uv**: Must be installed and available in your system PATH +- **pyperclip** (optional): Required only for `--copy` functionality + +Install uv if not already available: + +```bash +# macOS +brew install uv + +# Linux/Windows +curl -LsSf https://astral.sh/uv/install.sh | sh +``` diff --git a/docs/integrations/openai.mdx b/docs/integrations/openai.mdx new file mode 100644 index 000000000..6f88193f5 --- /dev/null +++ b/docs/integrations/openai.mdx @@ -0,0 +1,227 @@ +--- +title: OpenAI API ๐Ÿค FastMCP +sidebarTitle: OpenAI API +description: Connect FastMCP servers to the OpenAI API +icon: message-code +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + + +## Responses API + +OpenAI's [Responses API](https://platform.openai.com/docs/api-reference/responses) supports [MCP servers](https://platform.openai.com/docs/guides/tools-remote-mcp) as remote tool sources, allowing you to extend AI capabilities with custom functions. + + +The Responses API is a distinct API from OpenAI's Completions API or Assistants API. At this time, only the Responses API supports MCP. + + + +Currently, the Responses API only accesses **tools** from MCP serversโ€”it queries the `list_tools` endpoint and exposes those functions to the AI agent. Other MCP features like resources and prompts are not currently supported. + + + +### Create a Server + +First, create a FastMCP server with the tools you want to expose. For this example, we'll create a server with a single tool that rolls dice. + +```python server.py +import random +from fastmcp import FastMCP + +mcp = FastMCP(name="Dice Roller") + +@mcp.tool +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)] + +if __name__ == "__main__": + mcp.run(transport="http", port=8000) +``` + +### Deploy the Server + +Your server must be deployed to a public URL in order for OpenAI to access it. + +For development, you can use tools like `ngrok` to temporarily expose a locally-running server to the internet. We'll do that for this example (you may need to install `ngrok` and create a free account), but you can use any other method to deploy your server. + +Assuming you saved the above code as `server.py`, you can run the following two commands in two separate terminals to deploy your server and expose it to the internet: + + +```bash FastMCP server +python server.py +``` + +```bash ngrok +ngrok http 8000 +``` + + + +This exposes your unauthenticated server to the internet. Only run this command in a safe environment if you understand the risks. + + +### Call the Server + +To use the Responses API, you'll need to install the OpenAI Python SDK (not included with FastMCP): + +```bash +pip install openai +``` + +You'll also need to authenticate with OpenAI. You can do this by setting the `OPENAI_API_KEY` environment variable. Consult the OpenAI SDK documentation for more information. + +```bash +export OPENAI_API_KEY="your-api-key" +``` + +Here is an example of how to call your server from Python. Note that you'll need to replace `https://your-server-url.com` with the actual URL of your server. In addition, we use `/mcp/` as the endpoint because we deployed a streamable-HTTP server with the default path; you may need to use a different endpoint if you customized your server's deployment. + +```python {4, 11-16} +from openai import OpenAI + +# Your server URL (replace with your actual URL) +url = 'https://your-server-url.com' + +client = OpenAI() + +resp = client.responses.create( + model="gpt-4.1", + tools=[ + { + "type": "mcp", + "server_label": "dice_server", + "server_url": f"{url}/mcp/", + "require_approval": "never", + }, + ], + input="Roll a few dice!", +) + +print(resp.output_text) +``` +If you run this code, you'll see something like the following output: + +```text +You rolled 3 dice and got the following results: 6, 4, and 2! +``` + +### Authentication + + + +The Responses API can include headers to authenticate the request, which means you don't have to worry about your server being publicly accessible. + +#### Server Authentication + +The simplest way to add authentication to the server is to use a bearer token scheme. + +For this example, we'll quickly generate our own tokens with FastMCP's `RSAKeyPair` utility, but this may not be appropriate for production use. For more details, see the complete server-side [Token Verification](/servers/auth/token-verification) documentation. + +We'll start by creating an RSA key pair to sign and verify tokens. + +```python +from fastmcp.server.auth.providers.jwt import RSAKeyPair + +key_pair = RSAKeyPair.generate() +access_token = key_pair.create_token(audience="dice-server") +``` + + +FastMCP's `RSAKeyPair` utility is for development and testing only. + + +Next, we'll create a `JWTVerifier` to authenticate the server. + +```python +from fastmcp import FastMCP +from fastmcp.server.auth import JWTVerifier + +auth = JWTVerifier( + public_key=key_pair.public_key, + audience="dice-server", +) + +mcp = FastMCP(name="Dice Roller", auth=auth) +``` + +Here is a complete example that you can copy/paste. For simplicity and the purposes of this example only, it will print the token to the console. **Do NOT do this in production!** + +```python server.py [expandable] +from fastmcp import FastMCP +from fastmcp.server.auth import JWTVerifier +from fastmcp.server.auth.providers.jwt import RSAKeyPair +import random + +key_pair = RSAKeyPair.generate() +access_token = key_pair.create_token(audience="dice-server") + +auth = JWTVerifier( + public_key=key_pair.public_key, + audience="dice-server", +) + +mcp = FastMCP(name="Dice Roller", auth=auth) + +@mcp.tool +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)] + +if __name__ == "__main__": + print(f"\n---\n\n๐Ÿ”‘ Dice Roller access token:\n\n{access_token}\n\n---\n") + mcp.run(transport="http", port=8000) +``` + +#### Client Authentication + +If you try to call the authenticated server with the same OpenAI code we wrote earlier, you'll get an error like this: + +```python +pythonAPIStatusError: Error code: 424 - { + "error": { + "message": "Error retrieving tool list from MCP server: 'dice_server'. Http status code: 401 (Unauthorized)", + "type": "external_connector_error", + "param": "tools", + "code": "http_error" + } +} +``` + +As expected, the server is rejecting the request because it's not authenticated. + +To authenticate the client, you can pass the token in the `Authorization` header with the `Bearer` scheme: + + +```python {4, 7, 19-21} [expandable] +from openai import OpenAI + +# Your server URL (replace with your actual URL) +url = 'https://your-server-url.com' + +# Your access token (replace with your actual token) +access_token = 'your-access-token' + +client = OpenAI() + +resp = client.responses.create( + model="gpt-4.1", + tools=[ + { + "type": "mcp", + "server_label": "dice_server", + "server_url": f"{url}/mcp/", + "require_approval": "never", + "headers": { + "Authorization": f"Bearer {access_token}" + } + }, + ], + input="Roll a few dice!", +) + +print(resp.output_text) +``` + +You should now see the dice roll results in the output. \ No newline at end of file diff --git a/docs/integrations/openapi.mdx b/docs/integrations/openapi.mdx new file mode 100644 index 000000000..e023535a8 --- /dev/null +++ b/docs/integrations/openapi.mdx @@ -0,0 +1,473 @@ +--- +title: OpenAPI ๐Ÿค FastMCP +sidebarTitle: OpenAPI +description: Generate MCP servers from any OpenAPI specification +icon: list-tree +--- + +import { VersionBadge } from '/snippets/version-badge.mdx' + + + + +**New in 2.11**: FastMCP is introducing a next-generation OpenAPI parser. The new parser has greatly improved performance and compatibility, and is also easier to maintain. To enable it, set the environment variable `FASTMCP_EXPERIMENTAL_ENABLE_NEW_OPENAPI_PARSER=true`. + +The new parser is largely API-compatible with the existing implementation and will become the default in a future version. We encourage all users to test it and report any issues before it becomes the default. + + +FastMCP can automatically generate an MCP server from any OpenAPI specification, allowing AI models to interact with existing APIs through the MCP protocol. Instead of manually creating tools and resources, you provide an OpenAPI spec and FastMCP intelligently converts API endpoints into the appropriate MCP components. + + +Generating MCP servers from OpenAPI is a great way to get started with FastMCP, but in practice LLMs achieve **significantly better performance** with well-designed and curated MCP servers than with auto-converted OpenAPI servers. This is especially true for complex APIs with many endpoints and parameters. + +We recommend using the FastAPI integration for bootstrapping and prototyping, not for mirroring your API to LLM clients. See the post [Stop Converting Your REST APIs to MCP](https://www.jlowin.dev/blog/stop-converting-rest-apis-to-mcp) for more details. + + +## Create a Server + +To convert an OpenAPI specification to an MCP server, use the `FastMCP.from_openapi()` class method: + +```python server.py +import httpx +from fastmcp import FastMCP + +# Create an HTTP client for your API +client = httpx.AsyncClient(base_url="https://api.example.com") + +# Load your OpenAPI spec +openapi_spec = httpx.get("https://api.example.com/openapi.json").json() + +# Create the MCP server +mcp = FastMCP.from_openapi( + openapi_spec=openapi_spec, + client=client, + name="My API Server" +) + +if __name__ == "__main__": + mcp.run() +``` + +### Authentication + +If your API requires authentication, configure it on the HTTP client: + +```python +import httpx +from fastmcp import FastMCP + +# Bearer token authentication +api_client = httpx.AsyncClient( + base_url="https://api.example.com", + headers={"Authorization": "Bearer YOUR_TOKEN"} +) + +# Create MCP server with authenticated client +mcp = FastMCP.from_openapi( + openapi_spec=spec, + client=api_client, + timeout=30.0 # 30 second timeout for all requests +) +``` + +## Route Mapping + +By default, FastMCP converts **every endpoint** in your OpenAPI specification into an MCP **Tool**. This provides a simple, predictable starting point that ensures all your API's functionality is immediately available to the vast majority of LLM clients which only support MCP tools. + +While this is a pragmatic default for maximum compatibility, you can easily customize this behavior. Internally, FastMCP uses an ordered list of `RouteMap` objects to determine how to map OpenAPI routes to various MCP component types. + +Each `RouteMap` specifies a combination of methods, patterns, and tags, as well as a corresponding MCP component type. Each OpenAPI route is checked against each `RouteMap` in order, and the first one that matches every criteria is used to determine its converted MCP type. A special type, `EXCLUDE`, can be used to exclude routes from the MCP server entirely. + +- **Methods**: HTTP methods to match (e.g. `["GET", "POST"]` or `"*"` for all) +- **Pattern**: Regex pattern to match the route path (e.g. `r"^/users/.*"` or `r".*"` for all) +- **Tags**: A set of OpenAPI tags that must all be present. An empty set (`{}`) means no tag filtering, so the route matches regardless of its tags. +- **MCP type**: What MCP component type to create (`TOOL`, `RESOURCE`, `RESOURCE_TEMPLATE`, or `EXCLUDE`) +- **MCP tags**: A set of custom tags to add to components created from matching routes + +Here is FastMCP's default rule: + +```python +from fastmcp.server.openapi import RouteMap, MCPType + +DEFAULT_ROUTE_MAPPINGS = [ + # All routes become tools + RouteMap(mcp_type=MCPType.TOOL), +] +``` + + +**Experimental Parser**: If you're using the new parser (enabled via `FASTMCP_EXPERIMENTAL_ENABLE_NEW_OPENAPI_PARSER=true`), import from the experimental module instead: +```python +from fastmcp.experimental.server.openapi import RouteMap, MCPType +``` +The API is identical, but the implementation provides better performance and serverless compatibility. + + +### Custom Route Maps + +When creating your FastMCP server, you can customize routing behavior by providing your own list of `RouteMap` objects. Your custom maps are processed before the default route maps, and routes will be assigned to the first matching custom map. + +For example, prior to FastMCP 2.8.0, GET requests were automatically mapped to `Resource` and `ResourceTemplate` components based on whether they had path parameters. (This was changed solely for client compatibility reasons.) You can restore this behavior by providing custom route maps: + +```python +from fastmcp import FastMCP +from fastmcp.server.openapi import RouteMap, MCPType + +# Restore pre-2.8.0 semantic mapping +semantic_maps = [ + # GET requests with path parameters become ResourceTemplates + RouteMap(methods=["GET"], pattern=r".*\{.*\}.*", mcp_type=MCPType.RESOURCE_TEMPLATE), + # All other GET requests become Resources + RouteMap(methods=["GET"], pattern=r".*", mcp_type=MCPType.RESOURCE), +] + +mcp = FastMCP.from_openapi( + openapi_spec=spec, + client=client, + route_maps=semantic_maps, +) +``` + +With these maps, `GET` requests are handled semantically, and all other methods (`POST`, `PUT`, etc.) will fall through to the default rule and become `Tool`s. + +Here is a more complete example that uses custom route maps to convert all `GET` endpoints under `/analytics/` to tools while excluding all admin endpoints and all routes tagged "internal". All other routes will be handled by the default rules: + +```python +from fastmcp import FastMCP +from fastmcp.server.openapi import RouteMap, MCPType + +mcp = FastMCP.from_openapi( + openapi_spec=spec, + client=client, + route_maps=[ + # Analytics `GET` endpoints are tools + RouteMap( + methods=["GET"], + pattern=r"^/analytics/.*", + mcp_type=MCPType.TOOL, + ), + + # Exclude all admin endpoints + RouteMap( + pattern=r"^/admin/.*", + mcp_type=MCPType.EXCLUDE, + ), + + # Exclude all routes tagged "internal" + RouteMap( + tags={"internal"}, + mcp_type=MCPType.EXCLUDE, + ), + ], +) +``` + + +The default route maps are always applied after your custom maps, so you do not have to create route maps for every possible route. + + +### Excluding Routes + +To exclude routes from the MCP server, use a route map to assign them to `MCPType.EXCLUDE`. + +You can use this to remove sensitive or internal routes by targeting them specifically: + +```python +from fastmcp import FastMCP +from fastmcp.server.openapi import RouteMap, MCPType + +mcp = FastMCP.from_openapi( + openapi_spec=spec, + client=client, + route_maps=[ + RouteMap(pattern=r"^/admin/.*", mcp_type=MCPType.EXCLUDE), + RouteMap(tags={"internal"}, mcp_type=MCPType.EXCLUDE), + ], +) +``` + +Or you can use a catch-all rule to exclude everything that your maps don't handle explicitly: + +```python +from fastmcp import FastMCP +from fastmcp.server.openapi import RouteMap, MCPType + +mcp = FastMCP.from_openapi( + openapi_spec=spec, + client=client, + route_maps=[ + # custom mapping logic goes here + # ... your specific route maps ... + # exclude all remaining routes + RouteMap(mcp_type=MCPType.EXCLUDE), + ], +) +``` + + +Using a catch-all exclusion rule will prevent the default route mappings from being applied, since it will match every remaining route. This is useful if you want to explicitly allow-list certain routes. + + +### Advanced Route Mapping + + + +For advanced use cases that require more complex logic, you can provide a `route_map_fn` callable. After the route map logic is applied, this function is called on each matched route and its assigned MCP component type. It can optionally return a different component type to override the mapped assignment. If it returns `None`, the assigned type is used. + +In addition to more precise targeting of methods, patterns, and tags, this function can access any additional OpenAPI metadata about the route. + + +The `route_map_fn` is called on all routes, even those that matched `MCPType.EXCLUDE` in your custom maps. This gives you an opportunity to customize the mapping or even override an exclusion. + + +```python +from fastmcp import FastMCP +from fastmcp.server.openapi import RouteMap, MCPType, HTTPRoute + +def custom_route_mapper(route: HTTPRoute, mcp_type: MCPType) -> MCPType | None: + """Advanced route type mapping.""" + # Convert all admin routes to tools regardless of HTTP method + if "/admin/" in route.path: + return MCPType.TOOL + + elif "internal" in route.tags: + return MCPType.EXCLUDE + + # Convert user detail routes to templates even if they're POST + elif route.path.startswith("/users/") and route.method == "POST": + return MCPType.RESOURCE_TEMPLATE + + # Use defaults for all other routes + return None + +mcp = FastMCP.from_openapi( + openapi_spec=spec, + client=client, + route_map_fn=custom_route_mapper, +) +``` + +## Customization + +### Component Names + + + +FastMCP automatically generates names for MCP components based on the OpenAPI specification. By default, it uses the `operationId` from your OpenAPI spec, up to the first double underscore (`__`). + +All component names are automatically: +- **Slugified**: Spaces and special characters are converted to underscores or removed +- **Truncated**: Limited to 56 characters maximum to ensure compatibility +- **Unique**: If multiple components have the same name, a number is automatically appended to make them unique + +For more control over component names, you can provide an `mcp_names` dictionary that maps `operationId` values to your desired names. The `operationId` must be exactly as it appears in the OpenAPI spec. The provided name will always be slugified and truncated. + +```python +mcp = FastMCP.from_openapi( + openapi_spec=spec, + client=client, + mcp_names={ + "list_users__with_pagination": "user_list", + "create_user__admin_required": "create_user", + "get_user_details__admin_required": "user_detail", + } +) +``` + +Any `operationId` not found in `mcp_names` will use the default strategy (operationId up to the first `__`). + +### Tags + + + +FastMCP provides several ways to add tags to your MCP components, allowing you to categorize and organize them for better discoverability and filtering. Tags are combined from multiple sources to create the final set of tags on each component. + +#### RouteMap Tags + +You can add custom tags to components created from specific routes using the `mcp_tags` parameter in `RouteMap`. These tags will be applied to all components created from routes that match that particular route map. + +```python +from fastmcp.server.openapi import RouteMap, MCPType + +mcp = FastMCP.from_openapi( + openapi_spec=spec, + client=client, + route_maps=[ + # Add custom tags to all POST endpoints + RouteMap( + methods=["POST"], + pattern=r".*", + mcp_type=MCPType.TOOL, + mcp_tags={"write-operation", "api-mutation"} + ), + + # Add different tags to detail view endpoints + RouteMap( + methods=["GET"], + pattern=r".*\{.*\}.*", + mcp_type=MCPType.RESOURCE_TEMPLATE, + mcp_tags={"detail-view", "parameterized"} + ), + + # Add tags to list endpoints + RouteMap( + methods=["GET"], + pattern=r".*", + mcp_type=MCPType.RESOURCE, + mcp_tags={"list-data", "collection"} + ), + ], +) +``` + +#### Global Tags + +You can add tags to **all** components by providing a `tags` parameter when creating your MCP server. These global tags will be applied to every component created from your OpenAPI specification. + +```python +mcp = FastMCP.from_openapi( + openapi_spec=spec, + client=client, + tags={"api-v2", "production", "external"} +) +``` + +#### OpenAPI Tags in Client Meta + +FastMCP automatically includes OpenAPI tags from your specification in the component's metadata. These tags are available to MCP clients through the `_meta._fastmcp.tags` field, allowing clients to filter and organize components based on the original OpenAPI tagging: + + +```json {5} OpenAPI spec with tags +{ + "paths": { + "/users": { + "get": { + "tags": ["users", "public"], + "operationId": "list_users", + "summary": "List all users" + } + } + } +} +``` +```python {6-9} Access OpenAPI tags in MCP client +async with client: + tools = await client.list_tools() + for tool in tools: + if hasattr(tool, '_meta') and tool._meta: + # OpenAPI tags are now available in _fastmcp namespace! + fastmcp_meta = tool._meta.get('_fastmcp', {}) + openapi_tags = fastmcp_meta.get('tags', []) + if 'users' in openapi_tags: + print(f"Found user-related tool: {tool.name}") +``` + + +This makes it easy for clients to understand and organize API endpoints based on their original OpenAPI categorization. + +### Advanced Customization + + + +By default, FastMCP creates MCP components using a variety of metadata from the OpenAPI spec, such as incorporating the OpenAPI description into the MCP component description. + +At times you may want to modify those MCP components in a variety of ways, such as adding LLM-specific instructions or tags. For fine-grained customization, you can provide a `mcp_component_fn` when creating the MCP server. After each MCP component has been created, this function is called on it and has the opportunity to modify it in-place. + + +Your `mcp_component_fn` is expected to modify the component in-place, not to return a new component. The result of the function is ignored. + + +```python +from fastmcp.server.openapi import ( + HTTPRoute, + OpenAPITool, + OpenAPIResource, + OpenAPIResourceTemplate, +) + +# If using experimental parser, import from experimental module: +# from fastmcp.experimental.server.openapi import ( +# HTTPRoute, +# OpenAPITool, +# OpenAPIResource, +# OpenAPIResourceTemplate, +# ) + +def customize_components( + route: HTTPRoute, + component: OpenAPITool | OpenAPIResource | OpenAPIResourceTemplate, +) -> None: + # Add custom tags to all components + component.tags.add("openapi") + + # Customize based on component type + if isinstance(component, OpenAPITool): + component.description = f"๐Ÿ”ง {component.description} (via API)" + + if isinstance(component, OpenAPIResource): + component.description = f"๐Ÿ“Š {component.description}" + component.tags.add("data") + +mcp = FastMCP.from_openapi( + openapi_spec=spec, + client=client, + mcp_component_fn=customize_components, +) +``` + +## Request Parameter Handling + +FastMCP intelligently handles different types of parameters in OpenAPI requests: + +### Query Parameters + +By default, FastMCP only includes query parameters that have non-empty values. Parameters with `None` values or empty strings are automatically filtered out. + +```python +# When calling this tool... +await client.call_tool("search_products", { + "category": "electronics", # โœ… Included + "min_price": 100, # โœ… Included + "max_price": None, # โŒ Excluded + "brand": "", # โŒ Excluded +}) + +# The HTTP request will be: GET /products?category=electronics&min_price=100 +``` + +### Path Parameters + +Path parameters are typically required by REST APIs. FastMCP: +- Filters out `None` values +- Validates that all required path parameters are provided +- Raises clear errors for missing required parameters + +```python +# โœ… This works +await client.call_tool("get_user", {"user_id": 123}) + +# โŒ This raises: "Missing required path parameters: {'user_id'}" +await client.call_tool("get_user", {"user_id": None}) +``` + +### Array Parameters + +FastMCP handles array parameters according to OpenAPI specifications: + +- **Query arrays**: Serialized based on the `explode` parameter (default: `True`) +- **Path arrays**: Serialized as comma-separated values (OpenAPI 'simple' style) + +```python +# Query array with explode=true (default) +# ?tags=red&tags=blue&tags=green + +# Query array with explode=false +# ?tags=red,blue,green + +# Path array (always comma-separated) +# /items/red,blue,green +``` + +### Headers + +Header parameters are automatically converted to strings and included in the HTTP request. \ No newline at end of file diff --git a/docs/integrations/permit.mdx b/docs/integrations/permit.mdx new file mode 100644 index 000000000..066f5b1ea --- /dev/null +++ b/docs/integrations/permit.mdx @@ -0,0 +1,352 @@ +--- +title: Permit.io Authorization ๐Ÿค FastMCP +sidebarTitle: Permit.io +description: Add fine-grained authorization to your FastMCP servers with Permit.io +icon: shield-check +--- + +Add **policy-based authorization** to your FastMCP servers with one-line code addition with the **[Permit.io][permit-github] authorization middleware**. + +Control which tools, resources and prompts MCP clients can view and execute on your server. Define dynamic policies using Permit.io's powerful RBAC, ABAC, and REBAC capabilities, and obtain comprehensive audit logs of all access attempts and violations. + +## How it Works + +Leveraging FastMCP's [Middleware][fastmcp-middleware], the Permit.io middleware intercepts all MCP requests to your server and automatically maps MCP methods to authorization checks against your Permit.io policies; covering both server methods and tool execution. + +### Policy Mapping + +The middleware automatically maps MCP methods to Permit.io resources and actions: + +- **MCP server methods** (e.g., `tools/list`, `resources/read`): + - **Resource**: `{server_name}_{component}` (e.g., `myserver_tools`) + - **Action**: The method verb (e.g., `list`, `read`) +- **Tool execution** (method `tools/call`): + - **Resource**: `{server_name}` (e.g., `myserver`) + - **Action**: The tool name (e.g., `greet`) + +![Permit.io Policy Mapping Example](./images/permit/policy_mapping.png) + +*Example: In Permit.io, the 'Admin' role is granted permissions on resources and actions as mapped by the middleware. For example, 'greet', 'greet-jwt', and 'login' are actions on the 'mcp_server' resource, and 'list' is an action on the 'mcp_server_tools' resource.* + +> **Note:** +> Don't forget to assign the relevant role (e.g., Admin, User) to the user authenticating to your MCP server (such as the user in the JWT) in the Permit.io Directory. Without the correct role assignment, users will not have access to the resources and actions you've configured in your policies. +> +> ![Permit.io Directory Role Assignment Example](./images/permit/role_assignement.png) +> +> *Example: In Permit.io Directory, both 'client' and 'admin' users are assigned the 'Admin' role, granting them the permissions defined in your policy mapping.* + +For detailed policy mapping examples and configuration, see [Detailed Policy Mapping](https://github.com/permitio/permit-fastmcp/blob/main/docs/policy-mapping.md). + +### Listing Operations + +The middleware behaves as a filter for listing operations (`tools/list`, `resources/list`, `prompts/list`), hiding to the client components that are not authorized by the defined policies. + +```mermaid +sequenceDiagram + participant MCPClient as MCP Client + participant PermitMiddleware as Permit.io Middleware + participant MCPServer as FastMCP Server + participant PermitPDP as Permit.io PDP + + MCPClient->>PermitMiddleware: MCP Listing Request (e.g., tools/list) + PermitMiddleware->>MCPServer: MCP Listing Request + MCPServer-->>PermitMiddleware: MCP Listing Response + PermitMiddleware->>PermitPDP: Authorization Checks + PermitPDP->>PermitMiddleware: Authorization Decisions + PermitMiddleware-->>MCPClient: Filtered MCP Listing Response +``` + +### Execution Operations + +The middleware behaves as an enforcement point for execution operations (`tools/call`, `resources/read`, `prompts/get`), blocking operations that are not authorized by the defined policies. + +```mermaid +sequenceDiagram + participant MCPClient as MCP Client + participant PermitMiddleware as Permit.io Middleware + participant MCPServer as FastMCP Server + participant PermitPDP as Permit.io PDP + + MCPClient->>PermitMiddleware: MCP Execution Request (e.g., tools/call) + PermitMiddleware->>PermitPDP: Authorization Check + PermitPDP->>PermitMiddleware: Authorization Decision + PermitMiddleware-->>MCPClient: MCP Unauthorized Error (if denied) + PermitMiddleware->>MCPServer: MCP Execution Request (if allowed) + MCPServer-->>PermitMiddleware: MCP Execution Response (if allowed) + PermitMiddleware-->>MCPClient: MCP Execution Response (if allowed) +``` + +## Add Authorization to Your Server + + +Permit.io is a cloud-native authorization service. You need a Permit.io account and a running Policy Decision Point (PDP) for the middleware to function. You can run the PDP locally with Docker or use Permit.io's cloud PDP. + + +### Prerequisites + +1. **Permit.io Account**: Sign up at [permit.io](https://permit.io) +2. **PDP Setup**: Run the Permit.io PDP locally or use the cloud PDP (RBAC only) +3. **API Key**: Get your Permit.io API key from the dashboard + +### Run the Permit.io PDP + +Run the PDP locally with Docker: + +```bash +docker run -p 7766:7766 permitio/pdp:latest +``` + +Or use the cloud PDP URL: `https://cloudpdp.api.permit.io` + +### Create a Server with Authorization + +First, install the `permit-fastmcp` package: + +```bash +# Using UV (recommended) +uv add permit-fastmcp + +# Using pip +pip install permit-fastmcp +``` + +Then create a FastMCP server and add the Permit.io middleware: + +```python server.py +from fastmcp import FastMCP +from permit_fastmcp.middleware.middleware import PermitMcpMiddleware + +mcp = FastMCP("Secure FastMCP Server ๐Ÿ”’") + +@mcp.tool +def greet(name: str) -> str: + """Greet a user by name""" + return f"Hello, {name}!" + +@mcp.tool +def add(a: int, b: int) -> int: + """Add two numbers""" + return a + b + +# Add Permit.io authorization middleware +mcp.add_middleware(PermitMcpMiddleware( + permit_pdp_url="http://localhost:7766", + permit_api_key="your-permit-api-key" +)) + +if __name__ == "__main__": + mcp.run(transport="http") +``` + +### Configure Access Policies + +Create your authorization policies in the Permit.io dashboard: + +1. **Create Resources**: Define resources like `mcp_server` and `mcp_server_tools` +2. **Define Actions**: Add actions like `greet`, `add`, `list`, `read` +3. **Create Roles**: Define roles like `Admin`, `User`, `Guest` +4. **Assign Permissions**: Grant roles access to specific resources and actions +5. **Assign Users**: Assign roles to users in the Permit.io Directory + +For step-by-step setup instructions and troubleshooting, see [Getting Started & FAQ](https://github.com/permitio/permit-fastmcp/blob/main/docs/getting-started.md). + +#### Example Policy Configuration + +Policies are defined in the Permit.io dashboard, but you can also use the [Permit.io Terraform provider](https://github.com/permitio/terraform-provider-permitio) to define policies in code. + + +```terraform +# Resources +resource "permitio_resource" "mcp_server" { + name = "mcp_server" + key = "mcp_server" + + actions = { + "greet" = { name = "greet" } + "add" = { name = "add" } + } +} + +resource "permitio_resource" "mcp_server_tools" { + name = "mcp_server_tools" + key = "mcp_server_tools" + + actions = { + "list" = { name = "list" } + } +} + +# Roles +resource "permitio_role" "Admin" { + key = "Admin" + name = "Admin" + permissions = [ + "mcp_server:greet", + "mcp_server:add", + "mcp_server_tools:list" + ] +} +``` + +You can also use the [Permit.io CLI](https://github.com/permitio/permit-cli), [API](https://api.permit.io/scalar) or [SDKs](https://github.com/permitio/permit-python) to manage policies, as well as writing policies directly in REGO (Open Policy Agent's policy language). + +For complete policy examples including ABAC and RBAC configurations, see [Example Policies](https://github.com/permitio/permit-fastmcp/tree/main/docs/example_policies). + +### Identity Management + +The middleware supports multiple identity extraction modes: + +- **Fixed Identity**: Use a fixed identity for all requests +- **Header-based**: Extract identity from HTTP headers +- **JWT-based**: Extract and verify JWT tokens +- **Source-based**: Use the MCP context source field + +For detailed identity mode configuration and environment variables, see [Identity Modes & Environment Variables](https://github.com/permitio/permit-fastmcp/blob/main/docs/identity-modes.md). + +#### JWT Authentication Example + +```python +import os + +# Configure JWT identity extraction +os.environ["PERMIT_MCP_IDENTITY_MODE"] = "jwt" +os.environ["PERMIT_MCP_IDENTITY_JWT_SECRET"] = "your-jwt-secret" + +mcp.add_middleware(PermitMcpMiddleware( + permit_pdp_url="http://localhost:7766", + permit_api_key="your-permit-api-key" +)) +``` + +### ABAC Policies with Tool Arguments + +The middleware supports Attribute-Based Access Control (ABAC) policies that can evaluate tool arguments as attributes. Tool arguments are automatically flattened as individual attributes (e.g., `arg_name`, `arg_number`) for granular policy conditions. + +![ABAC Condition Example](./images/permit/abac_condition_example.png) + +*Example: Create dynamic resources with conditions like `resource.arg_number greater-than 10` to allow the `conditional-greet` tool only when the number argument exceeds 10.* + +#### Example: Conditional Access + +Create a dynamic resource with conditions like `resource.arg_number greater-than 10` to allow the `conditional-greet` tool only when the number argument exceeds 10. + +```python +@mcp.tool +def conditional_greet(name: str, number: int) -> str: + """Greet a user only if number > 10""" + return f"Hello, {name}! Your number is {number}" +``` + +![ABAC Policy Example](./images/permit/abac_policy_example.png) + +*Example: The Admin role is granted access to the "conditional-greet" action on the "Big-greets" dynamic resource, while other tools like "greet", "greet-jwt", and "login" are granted on the base "mcp_server" resource.* + +For comprehensive ABAC configuration and advanced policy examples, see [ABAC Policies with Tool Arguments](https://github.com/permitio/permit-fastmcp/blob/main/docs/policy-mapping.md#abac-policies-with-tool-arguments). + +### Run the Server + +Start your FastMCP server normally: + +```bash +python server.py +``` + +The middleware will now intercept all MCP requests and check them against your Permit.io policies. Requests include user identification through the configured identity mode and automatic mapping of MCP methods to authorization resources and actions. + +## Advanced Configuration + +### Environment Variables + +Configure the middleware using environment variables: + +```bash +# Permit.io configuration +export PERMIT_MCP_PERMIT_PDP_URL="http://localhost:7766" +export PERMIT_MCP_PERMIT_API_KEY="your-api-key" + +# Identity configuration +export PERMIT_MCP_IDENTITY_MODE="jwt" +export PERMIT_MCP_IDENTITY_JWT_SECRET="your-jwt-secret" + +# Method configuration +export PERMIT_MCP_KNOWN_METHODS='["tools/list","tools/call"]' +export PERMIT_MCP_BYPASSED_METHODS='["initialize","ping"]' + +# Logging configuration +export PERMIT_MCP_ENABLE_AUDIT_LOGGING="true" +``` + +For a complete list of all configuration options and environment variables, see [Configuration Reference](https://github.com/permitio/permit-fastmcp/blob/main/docs/configuration-reference.md). + +### Custom Middleware Configuration + +```python +from permit_fastmcp.middleware.middleware import PermitMcpMiddleware + +middleware = PermitMcpMiddleware( + permit_pdp_url="http://localhost:7766", + permit_api_key="your-api-key", + enable_audit_logging=True, + bypass_methods=["initialize", "ping", "health/*"] +) + +mcp.add_middleware(middleware) +``` + +For advanced configuration options and custom middleware extensions, see [Advanced Configuration](https://github.com/permitio/permit-fastmcp/blob/main/docs/advanced-configuration.md). + +## Example: Complete JWT Authentication Server + +See the [example server](https://github.com/permitio/permit-fastmcp/blob/main/permit_fastmcp/example_server/example.py) for a full implementation with JWT-based authentication. For additional examples and usage patterns, see [Example Server](https://github.com/permitio/permit-fastmcp/blob/main/permit_fastmcp/example_server/): + +```python +from fastmcp import FastMCP, Context +from permit_fastmcp.middleware.middleware import PermitMcpMiddleware +import jwt +import datetime + +# Configure JWT identity extraction +os.environ["PERMIT_MCP_IDENTITY_MODE"] = "jwt" +os.environ["PERMIT_MCP_IDENTITY_JWT_SECRET"] = "mysecretkey" + +mcp = FastMCP("My MCP Server") + +@mcp.tool +def login(username: str, password: str) -> str: + """Login to get a JWT token""" + if username == "admin" and password == "password": + token = jwt.encode( + {"sub": username, "exp": datetime.datetime.utcnow() + datetime.timedelta(hours=1)}, + "mysecretkey", + algorithm="HS256" + ) + return f"Bearer {token}" + raise Exception("Invalid credentials") + +@mcp.tool +def greet_jwt(ctx: Context) -> str: + """Greet a user by extracting their name from JWT""" + # JWT extraction handled by middleware + return "Hello, authenticated user!" + +mcp.add_middleware(PermitMcpMiddleware( + permit_pdp_url="http://localhost:7766", + permit_api_key="your-permit-api-key" +)) + +if __name__ == "__main__": + mcp.run(transport="http") +``` + + + For detailed policy configuration, custom authentication, and advanced + deployment patterns, visit the [Permit.io FastMCP Middleware + repository][permit-fastmcp-github]. For troubleshooting common issues, see [Troubleshooting](https://github.com/permitio/permit-fastmcp/blob/main/docs/troubleshooting.md). + + + +[permit.io]: https://www.permit.io +[permit-github]: https://github.com/permitio +[permit-fastmcp-github]: https://github.com/permitio/permit-fastmcp +[Agent.Security]: https://agent.security +[fastmcp-middleware]: /servers/middleware diff --git a/docs/integrations/scalekit.mdx b/docs/integrations/scalekit.mdx new file mode 100644 index 000000000..89de64ef1 --- /dev/null +++ b/docs/integrations/scalekit.mdx @@ -0,0 +1,187 @@ +--- +title: Scalekit ๐Ÿค FastMCP +sidebarTitle: Scalekit +description: Secure your FastMCP server with Scalekit +icon: shield-check +tag: NEW +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + + + + +Install auth stack to your FastMCP server with [Scalekit](https://scalekit.com) using the [Remote OAuth](/servers/auth/remote-oauth) pattern: Scalekit handles user authentication, and the MCP server validates issued tokens. + +## Configuration + +### Prerequisites + +Before you begin + +1. Get a [Scalekit account](https://app.scalekit.com/) and grab API credentials such as **Client ID**, **Client Secret** and **Environment URL** from _Dashboard > Developers > Settings_. +2. Have your FastMCP server's endpoint ready (can be localhost for development, e.g., `http://localhost:8000/mcp`) + +### Step 1: Configure MCP server in Scalekit environment + + + + +In your Scalekit dashboard: + 1. Open the **MCP Servers** section, then select **Create new server** + 2. Enter server details: a name, a resource identifier, and the desired MCP client authentication settings + 3. Save, then copy the **Resource ID** (for example, res_92015146095) + +In your FastMCP project's `.env`: + +```sh +SCALEKIT_ENVIRONMENT_URL= +SCALEKIT_CLIENT_ID= # skc_7008EXAMPLE46 +SCALEKIT_RESOURCE_ID= # res_926EXAMPLE5878 +MCP_URL=http://localhost:8000/mcp +``` + + + + +### Step 2: Add auth to FastMCP server + +Create your FastMCP server file and use the ScalekitProvider to handle all the OAuth integration automatically: + +```python server.py +from fastmcp import FastMCP +from fastmcp.server.auth.providers.scalekit import ScalekitProvider + +# Discovers Scalekit endpoints and set up JWT token validation +auth_provider = ScalekitProvider( + environment_url=SCALEKIT_ENVIRONMENT_URL, # Scalekit environment URL + client_id=SCALEKIT_CLIENT_ID, # OAuth client ID + resource_id=SCALEKIT_RESOURCE_ID, # Resource server ID + mcp_url=SERVER_URL, # Is also aud claim +) + +# Create FastMCP server with auth +mcp = FastMCP(name="My Scalekit Protected Server", auth=auth_provider) + +@mcp.tool +def auth_status() -> dict: + """Show Scalekit authentication status.""" + # Extract user claims from the JWT + return { + "message": "This tool requires authentication via Scalekit", + "authenticated": True, + "provider": "Scalekit" + } + +``` + +## Testing + +### Start the MCP server + +```sh +uv run python server.py +``` + +Use any MCP client (for example, mcp-inspector, Claude, VS Code, or Windsurf) to connect to the running serve. Verify that authentication succeeds and requests are authorized as expected. + +### Provider selection + +Setting this environment variable allows the Scalekit provider to be used automatically without explicitly instantiating it in code. + + + +Set to `fastmcp.server.auth.providers.scalekit.ScalekitProvider` to use Scalekit authentication. + + + +### Scalekit-specific configuration + +These environment variables provide default values for the Scalekit provider, whether it's instantiated manually or configured via `FASTMCP_SERVER_AUTH`. + + + +Your Scalekit environment URL from the Admin Portal (e.g., `https://your-env.scalekit.com`) + + + +Your Scalekit OAuth application client ID from the Applications section + + + +Your Scalekit resource server ID from the Resources section + + + +Public URL of your FastMCP server (e.g., `https://your-server.com` or `http://localhost:8000/mcp` for development) + + + +Example `.env`: + +```bash +# Use the Scalekit provider +FASTMCP_SERVER_AUTH=fastmcp.server.auth.providers.scalekit.ScalekitProvider + +# Scalekit configuration +FASTMCP_SERVER_AUTH_SCALEKITPROVIDER_ENVIRONMENT_URL=https://your-env.scalekit.com +FASTMCP_SERVER_AUTH_SCALEKITPROVIDER_CLIENT_ID=skc_123 +FASTMCP_SERVER_AUTH_SCALEKITPROVIDER_RESOURCE_ID=res_456 +FASTMCP_SERVER_AUTH_SCALEKITPROVIDER_MCP_URL=https://your-server.com/mcp +``` + +With environment variables set, your server code simplifies to: + +```python server.py +from fastmcp import FastMCP + +# Authentication is automatically configured from environment +mcp = FastMCP(name="My Scalekit Protected Server") + +@mcp.tool +def protected_action() -> str: + """A tool that requires authentication.""" + return "Access granted via Scalekit!" +``` + +## Capabilities + +Scalekit supports OAuth 2.1 with Dynamic Client Registration for MCP clients and enterprise SSO, and provides builtโ€‘in JWT validation and security controls. + +**OAuth 2.1/DCR**: clients selfโ€‘register, use PKCE, and work with the Remote OAuth pattern without preโ€‘provisioned credentials. + +**Validation and SSO**: tokens are verified (keys, RS256, issuer, audience, expiry), and SAML, OIDC, OAuth 2.0, ADFS, Azure AD, and Google Workspace are supported; use HTTPS in production and review auth logs as needed. + +## Debugging + +Enable detailed logging to troubleshoot authentication issues: + +```python +import logging +logging.basicConfig(level=logging.DEBUG) +``` + +### Token inspection + +You can inspect JWT tokens in your tools to understand the user context: + +```python +from fastmcp.server.context import request_ctx +import jwt + +@mcp.tool +def inspect_token() -> dict: + """Inspect the current JWT token claims.""" + context = request_ctx.get() + + # Extract token from Authorization header + if hasattr(context, 'request') and hasattr(context.request, 'headers'): + auth_header = context.request.headers.get('authorization', '') + if auth_header.startswith('Bearer '): + token = auth_header[7:] + # Decode without verification (already verified by provider) + claims = jwt.decode(token, options={"verify_signature": False}) + return claims + + return {"error": "No token found"} +``` diff --git a/docs/integrations/workos.mdx b/docs/integrations/workos.mdx new file mode 100644 index 000000000..98aa6999e --- /dev/null +++ b/docs/integrations/workos.mdx @@ -0,0 +1,242 @@ +--- +title: WorkOS ๐Ÿค FastMCP +sidebarTitle: WorkOS +description: Authenticate FastMCP servers with WorkOS Connect +icon: shield-check +tag: NEW +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + + + +Secure your FastMCP server with WorkOS Connect authentication. This integration uses the OAuth Proxy pattern to handle authentication through WorkOS Connect while maintaining compatibility with MCP clients. + + +This guide covers WorkOS Connect applications. For Dynamic Client Registration (DCR) with AuthKit, see the [AuthKit integration](/integrations/authkit) instead. + + +## Configuration + +### Prerequisites + +Before you begin, you will need: +1. A **[WorkOS Account](https://workos.com/)** with access to create OAuth Apps +2. Your FastMCP server's URL (can be localhost for development, e.g., `http://localhost:8000`) + +### Step 1: Create a WorkOS OAuth App + +Create an OAuth App in your WorkOS dashboard to get the credentials needed for authentication: + + + +In your WorkOS dashboard: +1. Navigate to **Applications** +2. Click **Create Application** +3. Select **OAuth Application** +4. Name your application + + + +In your OAuth application settings: +1. Copy your **Client ID** (starts with `client_`) +2. Click **Generate Client Secret** and save it securely +3. Copy your **AuthKit Domain** (e.g., `https://your-app.authkit.app`) + + + +In the **Redirect URIs** section: +- Add: `http://localhost:8000/auth/callback` (for development) +- For production, add your server's public URL + `/auth/callback` + + +The callback URL must match exactly. The default path is `/auth/callback`, but you can customize it using the `redirect_path` parameter. + + + + +### Step 2: FastMCP Configuration + +Create your FastMCP server using the `WorkOSProvider`: + +```python server.py +from fastmcp import FastMCP +from fastmcp.server.auth.providers.workos import WorkOSProvider + +# Configure WorkOS OAuth +auth = WorkOSProvider( + client_id="client_YOUR_CLIENT_ID", + client_secret="YOUR_CLIENT_SECRET", + authkit_domain="https://your-app.authkit.app", + base_url="http://localhost:8000", + required_scopes=["openid", "profile", "email"] +) + +mcp = FastMCP("WorkOS Protected Server", auth=auth) + +@mcp.tool +def protected_tool(message: str) -> str: + """This tool requires authentication.""" + return f"Authenticated user says: {message}" + +if __name__ == "__main__": + mcp.run(transport="http", port=8000) +``` + +## Testing + +### Running the Server + +Start your FastMCP server with HTTP transport to enable OAuth flows: + +```bash +fastmcp run server.py --transport http --port 8000 +``` + +Your server is now running and protected by WorkOS OAuth authentication. + +### Testing with a Client + +Create a test client that authenticates with your WorkOS-protected server: + +```python client.py +from fastmcp import Client +import asyncio + +async def main(): + # The client will automatically handle WorkOS OAuth + async with Client("http://localhost:8000/mcp", auth="oauth") as client: + # First-time connection will open WorkOS login in your browser + print("โœ“ Authenticated with WorkOS!") + + # Test the protected tool + result = await client.call_tool("protected_tool", {"message": "Hello!"}) + print(result) + +if __name__ == "__main__": + asyncio.run(main()) +``` + +When you run the client for the first time: +1. Your browser will open to WorkOS's authorization page +2. After you authorize the app, you'll be redirected back +3. The client receives the token and can make authenticated requests + + +The client caches tokens locally, so you won't need to re-authenticate for subsequent runs unless the token expires or you explicitly clear the cache. + + +## Environment Variables + + + +For production deployments, use environment variables instead of hardcoding credentials. + +### Provider Selection + +Setting this environment variable allows the WorkOS provider to be used automatically without explicitly instantiating it in code. + + + +Set to `fastmcp.server.auth.providers.workos.WorkOSProvider` to use WorkOS authentication. + + + +### WorkOS-Specific Configuration + +These environment variables provide default values for the WorkOS provider, whether it's instantiated manually or configured via `FASTMCP_SERVER_AUTH`. + + + +Your WorkOS OAuth App Client ID (e.g., `client_01K33Y6GGS7T3AWMPJWKW42Y3Q`) + + + +Your WorkOS OAuth App Client Secret + + + +Your WorkOS AuthKit domain (e.g., `https://your-app.authkit.app`) + + + +Public URL of your FastMCP server for OAuth callbacks + + + +Redirect path configured in your WorkOS OAuth App + + + +Comma-, space-, or JSON-separated list of required OAuth scopes (e.g., `openid profile email` or `["openid","profile","email"]`) + + + +HTTP request timeout for WorkOS API calls + + + +Example `.env` file: +```bash +# WorkOS OAuth credentials (always used as defaults) +FASTMCP_SERVER_AUTH_WORKOS_CLIENT_ID=client_01K33Y6GGS7T3AWMPJWKW42Y3Q +FASTMCP_SERVER_AUTH_WORKOS_CLIENT_SECRET=your_client_secret +FASTMCP_SERVER_AUTH_WORKOS_AUTHKIT_DOMAIN=https://your-app.authkit.app +FASTMCP_SERVER_AUTH_WORKOS_BASE_URL=https://your-server.com +FASTMCP_SERVER_AUTH_WORKOS_REQUIRED_SCOPES=["openid","profile","email"] + +# Optional: Automatically provision WorkOS auth for all servers +FASTMCP_SERVER_AUTH=fastmcp.server.auth.providers.workos.WorkOSProvider +``` + +With environment variables set, you can either: + +**Option 1: Manual instantiation (env vars provide defaults)** +```python server.py +from fastmcp import FastMCP +from fastmcp.server.auth.providers.workos import WorkOSProvider + +# Env vars provide default values for WorkOSProvider() +auth = WorkOSProvider() # Uses env var defaults +mcp = FastMCP(name="WorkOS Protected Server", auth=auth) +``` + +**Option 2: Automatic provisioning (requires FASTMCP_SERVER_AUTH=fastmcp.server.auth.providers.workos.WorkOSProvider)** +```python server.py +from fastmcp import FastMCP + +# Auth is automatically provisioned from FASTMCP_SERVER_AUTH +mcp = FastMCP(name="WorkOS Protected Server") +``` + +## Configuration Options + + + +WorkOS OAuth application client ID + + + +WorkOS OAuth application client secret + + + +Your WorkOS AuthKit domain URL (e.g., `https://your-app.authkit.app`) + + + +Your FastMCP server's public URL + + + +OAuth scopes to request + + + +OAuth callback path + + + +API request timeout + + \ No newline at end of file diff --git a/docs/patterns/cli.mdx b/docs/patterns/cli.mdx new file mode 100644 index 000000000..66aeac978 --- /dev/null +++ b/docs/patterns/cli.mdx @@ -0,0 +1,577 @@ +--- +title: FastMCP CLI +sidebarTitle: CLI +description: Learn how to use the FastMCP command-line interface +icon: terminal +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + + +FastMCP provides a command-line interface (CLI) that makes it easy to run, develop, and install your MCP servers. The CLI is automatically installed when you install FastMCP. + +```bash +fastmcp --help +``` + +## Commands Overview + +| Command | Purpose | Dependency Management | +| ------- | ------- | --------------------- | +| `run` | Run a FastMCP server directly | **Supports:** Local files, factory functions, URLs, fastmcp.json configs, MCP configs. **Deps:** Uses your local environment directly. With `--python`, `--with`, `--project`, or `--with-requirements`: Runs via `uv run` subprocess. With fastmcp.json: Automatically manages dependencies based on configuration | +| `dev` | Run a server with the MCP Inspector for testing | **Supports:** Local files and fastmcp.json configs. **Deps:** Always runs via `uv run` subprocess (never uses your local environment); dependencies must be specified or available in a uv-managed project. With fastmcp.json: Uses configured dependencies | +| `install` | Install a server in MCP client applications | **Supports:** Local files and fastmcp.json configs. **Deps:** Creates an isolated environment; dependencies must be explicitly specified with `--with` and/or `--with-editable`. With fastmcp.json: Uses configured dependencies | +| `inspect` | Generate a JSON report about a FastMCP server | **Supports:** Local files and fastmcp.json configs. **Deps:** Uses your current environment; you are responsible for ensuring all dependencies are available | +| `project prepare` | Create a persistent uv project from fastmcp.json environment config | **Supports:** fastmcp.json configs only. **Deps:** Creates a uv project directory with all dependencies pre-installed for reuse with `--project` flag | +| `version` | Display version information | N/A | + +## `fastmcp run` + +Run a FastMCP server directly or proxy a remote server. + +```bash +fastmcp run server.py +``` + + +By default, this command runs the server directly in your current Python environment. You are responsible for ensuring all dependencies are available. When using `--python`, `--with`, `--project`, or `--with-requirements` options, it runs the server via `uv run` subprocess instead. + + +### Options + +| Option | Flag | Description | +| ------ | ---- | ----------- | +| Transport | `--transport`, `-t` | Transport protocol to use (`stdio`, `http`, or `sse`) | +| Host | `--host` | Host to bind to when using http transport (default: 127.0.0.1) | +| Port | `--port`, `-p` | Port to bind to when using http transport (default: 8000) | +| Path | `--path` | Path to bind to when using http transport (default: `/mcp/` or `/sse/` for SSE) | +| Log Level | `--log-level`, `-l` | Log level (DEBUG, INFO, WARNING, ERROR, CRITICAL) | +| No Banner | `--no-banner` | Disable the startup banner display | +| No Environment | `--skip-env` | Skip environment setup with uv (use when already in a uv environment) | +| Python Version | `--python` | Python version to use (e.g., 3.10, 3.11) | +| Additional Packages | `--with` | Additional packages to install (can be used multiple times) | +| Project Directory | `--project` | Run the command within the given project directory | +| Requirements File | `--with-requirements` | Requirements file to install dependencies from | + + +### Entrypoints + + +The `fastmcp run` command supports the following entrypoints: + +1. **[Inferred server instance](#inferred-server-instance)**: `server.py` - imports the module and looks for a FastMCP server instance named `mcp`, `server`, or `app`. Errors if no such object is found. +2. **[Explicit server entrypoint](#explicit-server-entrypoint)**: `server.py:custom_name` - imports and uses the specified server entrypoint +3. **[Factory function](#factory-function)**: `server.py:create_server` - calls the specified function (sync or async) to create a server instance +4. **[Remote server proxy](#remote-server-proxy)**: `https://example.com/mcp-server` - connects to a remote server and creates a **local proxy server** +5. **[FastMCP configuration file](#fastmcp-configuration)**: `fastmcp.json` - runs servers using FastMCP's declarative configuration format (auto-detects files in current directory) +6. **MCP configuration file**: `mcp.json` - runs servers defined in a standard MCP configuration file + + +Note: When using `fastmcp run` with a local file, it **completely ignores** the `if __name__ == "__main__"` block. This means: +- Any setup code in `__main__` will NOT run +- Server configuration in `__main__` is bypassed +- `fastmcp run` finds your server entrypoint/factory and runs it with its own transport settings + +If you need setup code to run, use the **factory pattern** instead. + + +#### Inferred Server Instance + +If you provide a path to a file, `fastmcp run` will load the file and look for a FastMCP server instance stored as a variable named `mcp`, `server`, or `app`. If no such object is found, it will raise an error. + +For example, if you have a file called `server.py` with the following content: + +```python server.py +from fastmcp import FastMCP + +mcp = FastMCP("MyServer") +``` + +You can run it with: + +```bash +fastmcp run server.py +``` + +#### Explicit Server Entrypoint + +If your server is stored as a variable with a custom name, or you want to be explicit about which server to run, you can use the following syntax to load a specific server entrypoint: + +```bash +fastmcp run server.py:custom_name +``` + +For example, if you have a file called `server.py` with the following content: + +```python +from fastmcp import FastMCP + +my_server = FastMCP("CustomServer") + +@my_server.tool +def hello() -> str: + return "Hello from custom server!" +``` + +You can run it with: + +```bash +fastmcp run server.py:custom_name +``` + +#### Factory Function + + +Since `fastmcp run` ignores the `if __name__ == "__main__"` block, you can use a factory function to run setup code before your server starts. Factory functions are called without any arguments and must return a FastMCP server instance. Both sync and async factory functions are supported. + +The syntax for using a factory function is the same as for an explicit server entrypoint: `fastmcp run server.py:factory_fn`. FastMCP will automatically detect that you have identified a function rather than a server Instance + +For example, if you have a file called `server.py` with the following content: + +```python +from fastmcp import FastMCP + +async def create_server() -> FastMCP: + mcp = FastMCP("MyServer") + + @mcp.tool + def add(x: int, y: int) -> int: + return x + y + + # Setup that runs with fastmcp run + tool = await mcp.get_tool("add") + tool.disable() + + return mcp +``` + +You can run it with: + +```bash +fastmcp run server.py:create_server +``` + +#### Remote Server Proxy + +FastMCP run can also start a local proxy server that connects to a remote server. This is useful when you want to run a remote server locally for testing or development purposes, or to use with a client that doesn't support direct connections to remote servers. + +To start a local proxy, you can use the following syntax: + +```bash +fastmcp run https://example.com/mcp +``` + +#### FastMCP Configuration + + +FastMCP supports declarative configuration through `fastmcp.json` files. When you run `fastmcp run` without arguments, it automatically looks for a `fastmcp.json` file in the current directory: + +```bash +# Auto-detect fastmcp.json in current directory +fastmcp run + +# Or explicitly specify a configuration file +fastmcp run my-config.fastmcp.json +``` + +The configuration file handles dependencies, environment variables, and transport settings. Command-line arguments override configuration file values: + +```bash +# Override port from config file +fastmcp run fastmcp.json --port 8080 + +# Skip environment setup when already in a uv environment +fastmcp run fastmcp.json --skip-env +``` + + +The `--skip-env` flag is useful when: +- You're already in an activated virtual environment +- You're inside a Docker container with pre-installed dependencies +- You're in a uv-managed environment (prevents infinite recursion) +- You want to test the server without environment setup + + +See [Server Configuration](/deployment/server-configuration) for detailed documentation on fastmcp.json. + +#### MCP Configuration + +FastMCP can also run servers defined in a standard MCP configuration file. This is useful when you want to run multiple servers from a single file, or when you want to use a client that doesn't support direct connections to remote servers. + +To run a MCP configuration file, you can use the following syntax: + +```bash +fastmcp run mcp.json +``` + +This will run all the servers defined in the file. + +## `fastmcp dev` + +Run a MCP server with the [MCP Inspector](https://github.com/modelcontextprotocol/inspector) for testing. + +```bash +fastmcp dev server.py +``` + + +This command always runs your server via `uv run` subprocess (never your local environment) to work with the MCP Inspector. Dependencies can be: +- Specified using `--with` and/or `--with-editable` options +- Defined in a `fastmcp.json` configuration file +- Available in a uv-managed project + +When using `fastmcp.json`, the dev command automatically uses the configured dependencies. + + + +The `dev` command is a shortcut for testing a server over STDIO only. When the Inspector launches, you may need to: +1. Select "STDIO" from the transport dropdown +2. Connect manually + +This command does not support HTTP testing. To test a server over Streamable HTTP or SSE: +1. Start your server manually with the appropriate transport using either the command line: + ```bash + fastmcp run server.py --transport http + ``` + or by setting the transport in your code: + ```bash + python server.py # Assuming your __main__ block sets Streamable HTTP transport + ``` +2. Open the MCP Inspector separately and connect to your running server + + +### Options + +| Option | Flag | Description | +| ------ | ---- | ----------- | +| Editable Package | `--with-editable`, `-e` | Directory containing pyproject.toml to install in editable mode | +| Additional Packages | `--with` | Additional packages to install (can be used multiple times) | +| Inspector Version | `--inspector-version` | Version of the MCP Inspector to use | +| UI Port | `--ui-port` | Port for the MCP Inspector UI | +| Server Port | `--server-port` | Port for the MCP Inspector Proxy server | +| Python Version | `--python` | Python version to use (e.g., 3.10, 3.11) | +| Project Directory | `--project` | Run the command within the given project directory | +| Requirements File | `--with-requirements` | Requirements file to install dependencies from | + +### Entrypoints + +The `dev` command supports local FastMCP server files and configuration: + +1. **Inferred server instance**: `server.py` - imports the module and looks for a FastMCP server instance named `mcp`, `server`, or `app`. Errors if no such object is found. +2. **Explicit server entrypoint**: `server.py:custom_name` - imports and uses the specified server entrypoint +3. **Factory function**: `server.py:create_server` - calls the specified function (sync or async) to create a server instance +4. **FastMCP configuration**: `fastmcp.json` - uses FastMCP's declarative configuration (auto-detects in current directory) + + +The `dev` command **only supports local files and fastmcp.json** - no URLs, remote servers, or standard MCP configuration files. + + +**Examples** + +```bash +# Run dev server with editable mode and additional packages +fastmcp dev server.py -e . --with pandas --with matplotlib + +# Run dev server with fastmcp.json configuration (auto-detects) +fastmcp dev + +# Run dev server with explicit fastmcp.json file +fastmcp dev dev.fastmcp.json + +# Run dev server with specific Python version +fastmcp dev server.py --python 3.11 + +# Run dev server with requirements file +fastmcp dev server.py --with-requirements requirements.txt + +# Run dev server within a specific project directory +fastmcp dev server.py --project /path/to/project +``` + +## `fastmcp install` + + +Install a MCP server in MCP client applications. FastMCP currently supports the following clients: + +- **Claude Code** - Installs via Claude Code's built-in MCP management system +- **Claude Desktop** - Installs via direct configuration file modification +- **Cursor** - Installs via deeplink that opens Cursor for user confirmation +- **MCP JSON** - Generates standard MCP JSON configuration for manual use + +```bash +fastmcp install claude-code server.py +fastmcp install claude-desktop server.py +fastmcp install cursor server.py +fastmcp install mcp-json server.py +``` + +Note that for security reasons, MCP clients usually run every server in a completely isolated environment. Therefore, all dependencies must be explicitly specified using the `--with` and/or `--with-editable` options (following `uv` conventions) or by attaching them to your server in code via the `dependencies` parameter. You should not assume that the MCP server will have access to your local environment. + + +**`uv` must be installed and available in your system PATH**. Both Claude Desktop and Cursor run in isolated environments and need `uv` to manage dependencies. On macOS, install `uv` globally with Homebrew for Claude Desktop compatibility: `brew install uv`. + + + +**Python Version Considerations**: The install commands now support the `--python` option to specify a Python version directly. You can also use `--project` to run within a specific project directory or `--with-requirements` to install dependencies from a requirements file. + + + +**FastMCP `install` commands focus on local server files with STDIO transport.** For remote servers running with HTTP or SSE transport, use your client's native configuration - FastMCP's value is simplifying the complex local setup with dependencies and `uv` commands. + + +### Options + +| Option | Flag | Description | +| ------ | ---- | ----------- | +| Server Name | `--server-name`, `-n` | Custom name for the server (defaults to server's name attribute or file name) | +| Editable Package | `--with-editable`, `-e` | Directory containing pyproject.toml to install in editable mode | +| Additional Packages | `--with` | Additional packages to install (can be used multiple times) | +| Environment Variables | `--env` | Environment variables in KEY=VALUE format (can be used multiple times) | +| Environment File | `--env-file`, `-f` | Load environment variables from a .env file | +| Python Version | `--python` | Python version to use (e.g., 3.10, 3.11) | +| Project Directory | `--project` | Run the command within the given project directory | +| Requirements File | `--with-requirements` | Requirements file to install dependencies from | + +### Entrypoints + +The `install` command supports local FastMCP server files and configuration: + +1. **Inferred server instance**: `server.py` - imports the module and looks for a FastMCP server instance named `mcp`, `server`, or `app`. Errors if no such object is found. +2. **Explicit server entrypoint**: `server.py:custom_name` - imports and uses the specified server entrypoint +3. **Factory function**: `server.py:create_server` - calls the specified function (sync or async) to create a server instance +4. **FastMCP configuration**: `fastmcp.json` - uses FastMCP's declarative configuration with dependencies and settings + + +Factory functions are particularly useful for install commands since they allow setup code to run that would otherwise be ignored when the MCP client runs your server. When using fastmcp.json, dependencies are automatically handled. + + + +The `install` command **only supports local files and fastmcp.json** - no URLs, remote servers, or standard MCP configuration files. For remote servers, use your MCP client's native configuration. + + +**Examples** + +```bash +# Auto-detects server entrypoint (looks for 'mcp', 'server', or 'app') +fastmcp install claude-desktop server.py + +# Install with fastmcp.json configuration (auto-detects) +fastmcp install claude-desktop + +# Install with explicit fastmcp.json file +fastmcp install claude-desktop my-config.fastmcp.json + +# Uses specific server entrypoint +fastmcp install claude-desktop server.py:my_server + +# With custom name and dependencies +fastmcp install claude-desktop server.py:my_server --server-name "My Analysis Server" --with pandas + +# Install in Claude Code with environment variables +fastmcp install claude-code server.py --env API_KEY=secret --env DEBUG=true + +# Install in Cursor with environment variables +fastmcp install cursor server.py --env API_KEY=secret --env DEBUG=true + +# Install with environment file +fastmcp install cursor server.py --env-file .env + +# Install with specific Python version +fastmcp install claude-desktop server.py --python 3.11 + +# Install with requirements file +fastmcp install claude-code server.py --with-requirements requirements.txt + +# Install within a project directory +fastmcp install cursor server.py --project /path/to/project + +# Generate MCP JSON configuration +fastmcp install mcp-json server.py --name "My Server" --with pandas + +# Copy JSON configuration to clipboard +fastmcp install mcp-json server.py --copy +``` + +### MCP JSON Generation + +The `mcp-json` subcommand generates standard MCP JSON configuration that can be used with any MCP-compatible client. This is useful when: + +- Working with MCP clients not directly supported by FastMCP +- Creating configuration for CI/CD environments +- Sharing server configurations with others +- Integration with custom tooling + +The generated JSON follows the standard MCP server configuration format used by Claude Desktop, VS Code, Cursor, and other MCP clients, with the server name as the root key: + +```json +{ + "server-name": { + "command": "uv", + "args": [ + "run", + "--with", + "fastmcp", + "fastmcp", + "run", + "/path/to/server.py" + ], + "env": { + "API_KEY": "value" + } + } +} +``` + + +To use this configuration with your MCP client, you'll typically need to add it to the client's `mcpServers` object. Consult your client's documentation for any specific configuration requirements or formatting needs. + + +**Options specific to mcp-json:** + +| Option | Flag | Description | +| ------ | ---- | ----------- | +| Copy to Clipboard | `--copy` | Copy configuration to clipboard instead of printing to stdout | + +## `fastmcp inspect` + + + +Inspect a FastMCP server to view summary information or generate a detailed JSON report. + +```bash +# Show text summary +fastmcp inspect server.py + +# Output FastMCP JSON to stdout +fastmcp inspect server.py --format fastmcp + +# Save MCP JSON to file (format required with -o) +fastmcp inspect server.py --format mcp -o manifest.json +``` + +### Options + +| Option | Flag | Description | +| ------ | ---- | ----------- | +| Format | `--format`, `-f` | Output format: `fastmcp` (FastMCP-specific) or `mcp` (MCP protocol). Required when using `-o` | +| Output File | `--output`, `-o` | Save JSON report to file instead of stdout. Requires `--format` | + +### Output Formats + +#### FastMCP Format (`--format fastmcp`) +The default and most comprehensive format, includes all FastMCP-specific metadata: +- Server name, instructions, and version +- FastMCP version and MCP version +- Tool tags and enabled status +- Output schemas for tools +- Annotations and custom metadata +- Uses snake_case field names +- **Use this for**: Complete server introspection and debugging FastMCP servers + +#### MCP Protocol Format (`--format mcp`) +Shows exactly what MCP clients will see via the protocol: +- Only includes standard MCP protocol fields +- Matches output from `client.list_tools()`, `client.list_prompts()`, etc. +- Uses camelCase field names (e.g., `inputSchema`) +- Excludes FastMCP-specific fields like tags and enabled status +- **Use this for**: Debugging client visibility and ensuring MCP compatibility + +### Entrypoints + +The `inspect` command supports local FastMCP server files and configuration: + +1. **Inferred server instance**: `server.py` - imports the module and looks for a FastMCP server instance named `mcp`, `server`, or `app`. Errors if no such object is found. +2. **Explicit server entrypoint**: `server.py:custom_name` - imports and uses the specified server entrypoint +3. **Factory function**: `server.py:create_server` - calls the specified function (sync or async) to create a server instance +4. **FastMCP configuration**: `fastmcp.json` - inspects servers defined with FastMCP's declarative configuration + + +The `inspect` command **only supports local files and fastmcp.json** - no URLs, remote servers, or standard MCP configuration files. + + +### Examples + +```bash +# Show text summary (no JSON output) +fastmcp inspect server.py +# Output: +# Server: MyServer +# Instructions: A helpful MCP server +# Version: 1.0.0 +# +# Components: +# Tools: 5 +# Prompts: 2 +# Resources: 3 +# Templates: 1 +# +# Environment: +# FastMCP: 2.0.0 +# MCP: 1.0.0 +# +# Use --format [fastmcp|mcp] for complete JSON output + +# Output FastMCP format to stdout +fastmcp inspect server.py --format fastmcp + +# Specify server entrypoint +fastmcp inspect server.py:my_server + +# Output MCP protocol format to stdout +fastmcp inspect server.py --format mcp + +# Save to file (format required) +fastmcp inspect server.py --format fastmcp -o server-manifest.json + +# Save MCP format with custom server object +fastmcp inspect server.py:my_server --format mcp -o mcp-manifest.json + +# Error: format required with output file +fastmcp inspect server.py -o output.json +# Error: --format is required when using -o/--output +``` + +## `fastmcp project prepare` + +Create a persistent uv project directory from a fastmcp.json file's environment configuration. This allows you to pre-install all dependencies once and reuse them with the `--project` flag. + +```bash +fastmcp project prepare fastmcp.json --output-dir ./env +``` + +### Options + +| Option | Flag | Description | +| ------ | ---- | ----------- | +| Output Directory | `--output-dir` | **Required.** Directory where the persistent uv project will be created | + +### Usage Pattern + +```bash +# Step 1: Prepare the environment (installs dependencies) +fastmcp project prepare fastmcp.json --output-dir ./my-env + +# Step 2: Run using the prepared environment (fast, no dependency installation) +fastmcp run fastmcp.json --project ./my-env +``` + +The prepare command creates a uv project with: +- A `pyproject.toml` containing all dependencies from the fastmcp.json +- A `.venv` with all packages pre-installed +- A `uv.lock` file for reproducible environments + +This is useful when you want to separate environment setup from server execution, such as in deployment scenarios where dependencies are installed once and the server is run multiple times. + +## `fastmcp version` + +Display version information about FastMCP and related components. + +```bash +fastmcp version +``` + +### Options + +| Option | Flag | Description | +| ------ | ---- | ----------- | +| Copy to Clipboard | `--copy` | Copy version information to clipboard | diff --git a/docs/patterns/contrib.mdx b/docs/patterns/contrib.mdx new file mode 100644 index 000000000..2f71c0b61 --- /dev/null +++ b/docs/patterns/contrib.mdx @@ -0,0 +1,45 @@ +--- +title: "Contrib Modules" +description: "Community-contributed modules extending FastMCP" +icon: "cubes" +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + + + +FastMCP includes a `contrib` package that holds community-contributed modules. These modules extend FastMCP's functionality but aren't officially maintained by the core team. + +Contrib modules provide additional features, integrations, or patterns that complement the core FastMCP library. They offer a way for the community to share useful extensions while keeping the core library focused and maintainable. + +The available modules can be viewed in the [contrib directory](https://github.com/jlowin/fastmcp/tree/main/src/fastmcp/contrib). + +## Usage + +To use a contrib module, import it from the `fastmcp.contrib` package: + +```python +from fastmcp.contrib import my_module +``` + +## Important Considerations + +- **Stability**: Modules in `contrib` may have different testing requirements or stability guarantees compared to the core library. +- **Compatibility**: Changes to core FastMCP might break modules in `contrib` without explicit warnings in the main changelog. +- **Dependencies**: Contrib modules may have additional dependencies not required by the core library. These dependencies are typically documented in the module's README or separate requirements files. + +## Contributing + +We welcome contributions to the `contrib` package! If you have a module that extends FastMCP in a useful way, consider contributing it: + +1. Create a new directory in `src/fastmcp/contrib/` for your module +3. Add proper tests for your module in `tests/contrib/` +2. Include comprehensive documentation in a README.md file, including usage and examples, as well as any additional dependencies or installation instructions +5. Submit a pull request + +The ideal contrib module: +- Solves a specific use case or integration need +- Follows FastMCP coding standards +- Includes thorough documentation and examples +- Has comprehensive tests +- Specifies any additional dependencies diff --git a/docs/patterns/decorating-methods.mdx b/docs/patterns/decorating-methods.mdx new file mode 100644 index 000000000..2b48aa4d9 --- /dev/null +++ b/docs/patterns/decorating-methods.mdx @@ -0,0 +1,225 @@ +--- +title: Decorating Methods +sidebarTitle: Decorating Methods +description: Properly use instance methods, class methods, and static methods with FastMCP decorators. +icon: 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`). + +## 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: + +1. For instance methods: The decorator gets the unbound method before any instance exists +2. For class methods: The decorator gets the function before it's bound to the class + +This means directly decorating these methods doesn't work as expected. In practice, the LLM would see parameters like `self` or `cls` that it cannot provide values for. + +Additionally, **FastMCP decorators return objects (Tool, Resource, or Prompt instances) rather than the original function**. This means that when you decorate a method directly, the method becomes the returned object and is no longer callable by your code: + + +**Don't do this!** + +The method will no longer be callable from Python, and the tool won't be callable by LLMs. + +```python + +from fastmcp import FastMCP +mcp = FastMCP() + +class MyClass: + @mcp.tool + def my_method(self, x: int) -> int: + return x * 2 + +obj = MyClass() +obj.my_method(5) # Fails - my_method is a Tool, not a function +``` + + +This is another important reason to register methods functionally after defining the class. + +## Recommended Patterns + +### Instance Methods + + +**Don't do this!** + +```python +from fastmcp import FastMCP + +mcp = FastMCP() + +class MyClass: + @mcp.tool # This won't work correctly + def add(self, x, y): + return x + y +``` + +When the decorator is applied this way, it captures the unbound method. When the LLM later tries to use this component, it will see `self` as a required parameter, but it won't know what to provide for it, causing errors or unexpected behavior. + + +**Do this instead**: + +```python +from fastmcp import FastMCP + +mcp = FastMCP() + +class MyClass: + def add(self, x, y): + return x + y + +# Create an instance first, then register the bound methods +obj = MyClass() +mcp.tool(obj.add) + +# Now you can call it without 'self' showing up as a parameter +await mcp._mcp_call_tool('add', {'x': 1, 'y': 2}) # Returns 3 +``` + + +This approach works because: +1. You first create an instance of the class (`obj`) +2. When you access the method through the instance (`obj.add`), Python creates a bound method where `self` is already set to that instance +3. When you register this bound method, the system sees a callable that only expects the appropriate parameters, not `self` + +### Class Methods + +The behavior of decorating class methods depends on the order of decorators: + + +**Don't do this** (decorator order matters): + +```python +from fastmcp import FastMCP + +mcp = FastMCP() + +class MyClass: + @classmethod + @mcp.tool # This won't work but won't raise an error + def from_string_v1(cls, s): + return cls(s) + + @mcp.tool + @classmethod # This will raise a helpful ValueError + def from_string_v2(cls, s): + return cls(s) +``` + + +- 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 + + +**Do this instead**: + +```python +from fastmcp import FastMCP + +mcp = FastMCP() + +class MyClass: + @classmethod + def from_string(cls, s): + return cls(s) + +# Register the class method after the class is defined +mcp.tool(MyClass.from_string) +``` + + +This works because: +1. The `@classmethod` decorator is applied properly during class definition +2. When you access `MyClass.from_string`, Python provides a special method object that automatically binds the class to the `cls` parameter +3. When registered, only the appropriate parameters are exposed to the LLM, hiding the implementation detail of the `cls` parameter + +### Static Methods + +Static methods "work" with FastMCP decorators, but this is not recommended because the FastMCP decorator will not return a callable method. Therefore, you should register static methods the same way as other methods. + + +**This is not recommended, though it will work.** + +```python +from fastmcp import FastMCP + +mcp = FastMCP() + +class MyClass: + @mcp.tool + @staticmethod + def utility(x, y): + return x + y +``` + + +This works because `@staticmethod` converts the method to a regular function, which the FastMCP decorator can then properly process. However, this is not recommended because the FastMCP decorator will not return a callable staticmethod. Therefore, you should register static methods the same way as other methods. + + +**Prefer this pattern:** + +```python +from fastmcp import FastMCP + +mcp = FastMCP() + +class MyClass: + @staticmethod + def utility(x, y): + return x + y + +# This also works +mcp.tool(MyClass.utility) +``` + + +## Additional Patterns + +### Creating Components at Class Initialization + +You can automatically register instance methods when creating an object: + +```python +from fastmcp import FastMCP + +mcp = FastMCP() + +class ComponentProvider: + def __init__(self, mcp_instance): + # Register methods + mcp_instance.tool(self.tool_method) + mcp_instance.resource("resource://data")(self.resource_method) + + def tool_method(self, x): + return x * 2 + + def resource_method(self): + return "Resource data" + +# The methods are automatically registered when creating the instance +provider = ComponentProvider(mcp) +``` + +This pattern is useful when: +- You want to encapsulate registration logic within the class itself +- You have multiple related components that should be registered together +- You want to ensure that methods are always properly registered when creating an instance + +The class automatically registers its methods during initialization, ensuring they're properly bound to the instance before registration. + +## Summary + +The current behavior of FastMCP decorators with methods is: + +- **Static methods**: Can be decorated directly and work perfectly with all FastMCP decorators +- **Class methods**: Cannot be decorated directly and will raise a helpful `ValueError` with guidance +- **Instance methods**: Should be registered after creating an instance using the decorator calls + +For class and instance methods, you should register them after creating the instance or class to ensure proper method binding. This ensures that the methods are properly bound before being registered. + + +Understanding these patterns allows you to effectively organize your components into classes while maintaining proper method binding, giving you the benefits of object-oriented design without sacrificing the simplicity of FastMCP's decorator system. diff --git a/docs/patterns/tool-transformation.mdx b/docs/patterns/tool-transformation.mdx new file mode 100644 index 000000000..5f274fd75 --- /dev/null +++ b/docs/patterns/tool-transformation.mdx @@ -0,0 +1,576 @@ +--- +title: Tool Transformation +sidebarTitle: Tool Transformation +description: Create enhanced tool variants with modified schemas, argument mappings, and custom behavior. +icon: wand-magic-sparkles +--- + +import { VersionBadge } from '/snippets/version-badge.mdx' + + + +Tool transformation allows you to create new, enhanced tools from existing ones. This powerful feature enables you to adapt tools for different contexts, simplify complex interfaces, or add custom logic without duplicating code. + +## Why Transform Tools? + +Often, an existing tool is *almost* perfect for your use case, but it might have: +- A confusing description (or no description at all). +- Argument names or descriptions that are not intuitive for an LLM (e.g., `q` instead of `query`). +- Unnecessary parameters that you want to hide from the LLM. +- A need for input validation before the original tool is called. +- A need to modify or format the tool's output. + +Instead of rewriting the tool from scratch, you can **transform** it to fit your needs. + +## Basic Transformation + +The primary way to create a transformed tool is with the `Tool.from_tool()` class method. At its simplest, you can use it to change a tool's top-level metadata like its `name`, `description`, or `tags`. + +In the following simple example, we take a generic `search` tool and adjust its name and description to help an LLM client better understand its purpose. + +```python {13-21} +from fastmcp import FastMCP +from fastmcp.tools import Tool + +mcp = FastMCP() + +# The original, generic tool +@mcp.tool +def search(query: str, category: str = "all") -> list[dict]: + """Searches for items in the database.""" + return database.search(query, category) + +# Create a more domain-specific version by changing its metadata +product_search_tool = Tool.from_tool( + search, + name="find_products", + description=""" + Search for products in the e-commerce catalog. + Use this when customers ask about finding specific items, + checking availability, or browsing product categories. + """, +) + +mcp.add_tool(product_search_tool) +``` + + +When you transform a tool, the original tool remains registered on the server. To avoid confusing an LLM with two similar tools, you can disable the original one: + +```python +from fastmcp import FastMCP +from fastmcp.tools import Tool + +mcp = FastMCP() + +# The original, generic tool +@mcp.tool +def search(query: str, category: str = "all") -> list[dict]: + ... + +# Create a more domain-specific version +product_search_tool = Tool.from_tool(search, ...) +mcp.add_tool(product_search_tool) + +# Disable the original tool +search.disable() +``` + + +Now, clients see a tool named `find_products` with a clear, domain-specific purpose and relevant tags, even though it still uses the original generic `search` function's logic. + +### Parameters + +The `Tool.from_tool()` class method is the primary way to create a transformed tool. It takes the following parameters: + +- `tool`: The tool to transform. This is the only required argument. +- `name`: An optional name for the new tool. +- `description`: An optional description for the new tool. +- `transform_args`: A dictionary of `ArgTransform` objects, one for each argument you want to modify. +- `transform_fn`: An optional function that will be called instead of the parent tool's logic. +- `output_schema`: Control output schema and structured outputs (see [Output Schema Control](#output-schema-control)). +- `tags`: An optional set of tags for the new tool. +- `annotations`: An optional set of `ToolAnnotations` for the new tool. +- `serializer`: An optional function that will be called to serialize the result of the new tool. +- `meta`: Control meta information for the tool. Use `None` to remove meta, any dict to set meta, or leave unset to inherit from parent. + +The result is a new `TransformedTool` object that wraps the parent tool and applies the transformations you specify. You can add this tool to your MCP server using its `add_tool()` method. + + + +## Modifying Arguments + +To modify a tool's parameters, provide a dictionary of `ArgTransform` objects to the `transform_args` parameter of `Tool.from_tool()`. Each key is the name of the *original* argument you want to modify. + + +You only need to provide a `transform_args` entry for arguments you want to modify. All other arguments will be passed through unchanged. + + +### The ArgTransform Class + +To modify an argument, you need to create an `ArgTransform` object. This object has the following parameters: + +- `name`: The new name for the argument. +- `description`: The new description for the argument. +- `default`: The new default value for the argument. +- `default_factory`: A function that will be called to generate a default value for the argument. This is useful for arguments that need to be generated for each tool call, such as timestamps or unique IDs. +- `hide`: Whether to hide the argument from the LLM. +- `required`: Whether the argument is required, usually used to make an optional argument be required instead. +- `type`: The new type for the argument. + + +Certain combinations of parameters are not allowed. For example, you can only use `default_factory` with `hide=True`, because dynamic defaults cannot be represented in a JSON schema for the client. You can only set required=True for arguments that do not declare a default value. + + + +### Descriptions + +By far the most common reason to transform a tool, after its own description, is to improve its argument descriptions. A good description is crucial for helping an LLM understand how to use a parameter correctly. This is especially important when wrapping tools from external APIs, whose argument descriptions may be missing or written for developers, not LLMs. + +In this example, we add a helpful description to the `user_id` argument: + +```python {16-19} +from fastmcp import FastMCP +from fastmcp.tools import Tool +from fastmcp.tools.tool_transform import ArgTransform + +mcp = FastMCP() + +@mcp.tool +def find_user(user_id: str): + """Finds a user by their ID.""" + ... + +new_tool = Tool.from_tool( + find_user, + transform_args={ + "user_id": ArgTransform( + description=( + "The unique identifier for the user, " + "usually in the format 'usr-xxxxxxxx'." + ) + ) + } +) +``` + +### Names + +At times, you may want to rename an argument to make it more intuitive for an LLM. + +For example, in the following example, we take a generic `q` argument and expand it to `search_query`: + +```python {15} +from fastmcp import FastMCP +from fastmcp.tools import Tool +from fastmcp.tools.tool_transform import ArgTransform + +mcp = FastMCP() + +@mcp.tool +def search(q: str): + """Searches for items in the database.""" + return database.search(q) + +new_tool = Tool.from_tool( + search, + transform_args={ + "q": ArgTransform(name="search_query") + } +) +``` + +### Default Values + +You can update the default value for any argument using the `default` parameter. Here, we change the default value of the `y` argument to 10: + +```python{15} +from fastmcp import FastMCP +from fastmcp.tools import Tool +from fastmcp.tools.tool_transform import ArgTransform + +mcp = FastMCP() + +@mcp.tool +def add(x: int, y: int) -> int: + """Adds two numbers.""" + return x + y + +new_tool = Tool.from_tool( + add, + transform_args={ + "y": ArgTransform(default=10) + } +) +``` + +Default values are especially useful in combination with hidden arguments. + +### Hiding Arguments + +Sometimes a tool requires arguments that shouldn't be exposed to the LLM, such as API keys, configuration flags, or internal IDs. You can hide these parameters using `hide=True`. Note that you can only hide arguments that have a default value (or for which you provide a new default), because the LLM can't provide a value at call time. + + +To pass a constant value to the parent tool, combine `hide=True` with `default=`. + + +```python {19-20} +import os +from fastmcp import FastMCP +from fastmcp.tools import Tool +from fastmcp.tools.tool_transform import ArgTransform + +mcp = FastMCP() + +@mcp.tool +def send_email(to: str, subject: str, body: str, api_key: str): + """Sends an email.""" + ... + +# Create a simplified version that hides the API key +new_tool = Tool.from_tool( + send_email, + name="send_notification", + transform_args={ + "api_key": ArgTransform( + hide=True, + default=os.environ.get("EMAIL_API_KEY"), + ) + } +) +``` +The LLM now only sees the `to`, `subject`, and `body` parameters. The `api_key` is supplied automatically from an environment variable. + +For values that must be generated for each tool call (like timestamps or unique IDs), use `default_factory`, which is called with no arguments every time the tool is called. For example, + +```python {3-4} +transform_args = { + 'timestamp': ArgTransform( + hide=True, + default_factory=lambda: datetime.now(), + ) +} +``` + + +`default_factory` can only be used with `hide=True`. This is because visible parameters need static defaults that can be represented in a JSON schema for the client. + + +### Meta Information + + + +You can control meta information on transformed tools using the `meta` parameter. Meta information is additional data about the tool that doesn't affect its functionality but can be used by clients for categorization, routing, or other purposes. + +```python {15-17} +from fastmcp import FastMCP +from fastmcp.tools import Tool + +mcp = FastMCP() + +@mcp.tool +def analyze_data(data: str) -> dict: + """Analyzes the provided data.""" + return {"result": f"Analysis of {data}"} + +# Add custom meta information +enhanced_tool = Tool.from_tool( + analyze_data, + name="enhanced_analyzer", + meta={ + "category": "analytics", + "priority": "high", + "requires_auth": True + } +) + +mcp.add_tool(enhanced_tool) +``` + +You can also remove meta information entirely: + +```python {6} +# Remove meta information from parent tool +simplified_tool = Tool.from_tool( + analyze_data, + name="simple_analyzer", + meta=None # Removes any meta information +) +``` + +If you don't specify the `meta` parameter, the transformed tool inherits the parent tool's meta information. + +### Required Values + +In rare cases where you want to make an optional argument required, you can set `required=True`. This has no effect if the argument was already required. + +```python {3} +transform_args = { + 'user_id': ArgTransform( + required=True, + ) +} +``` + +## Modifying Tool Behavior + + +With great power comes great responsibility. Modifying tool behavior is a very advanced feature. + + +In addition to changing a tool's schema, advanced users can also modify its behavior. This is useful for adding validation logic, or for post-processing the tool's output. + +The `from_tool()` method takes a `transform_fn` parameter, which is an async function that replaces the parent tool's logic and gives you complete control over the tool's execution. + +### The Transform Function + +The `transform_fn` is an async function that **completely replaces** the parent tool's logic. + +Critically, the transform function's arguments are used to determine the new tool's final schema. Any arguments that are not already present in the parent tool schema OR the `transform_args` will be added to the new tool's schema. Note that when `transform_args` and your function have the same argument name, the `transform_args` metadata will take precedence, if provided. + +```python +async def my_custom_logic(user_input: str, max_length: int = 100) -> str: + # Your custom logic here - this completely replaces the parent tool + return f"Custom result for: {user_input[:max_length]}" + +Tool.from_tool(transform_fn=my_custom_logic) +``` + + +The name / docstring of the `transform_fn` are ignored. Only its arguments are used to determine the final schema. + + +### Calling the Parent Tool + +Most of the time, you don't want to completely replace the parent tool's behavior. Instead, you want to add validation, modify inputs, or post-process outputs while still leveraging the parent tool's core functionality. For this, FastMCP provides the special `forward()` and `forward_raw()` functions. + +Both `forward()` and `forward_raw()` are async functions that let you call the parent tool from within your `transform_fn`: + +- **`forward()`** (recommended): Automatically handles argument mapping based on your `ArgTransform` configurations. Call it with the transformed argument names. +- **`forward_raw()`**: Bypasses all transformation and calls the parent tool directly with its original argument names. This is rarely needed unless you're doing complex argument manipulation, perhaps without `arg_transforms`. + +The most common transformation pattern is to validate (potentially renamed) arguments before calling the parent tool. Here's an example that validates that `x` and `y` are positive before calling the parent tool: + + + +In the simplest case, your parent tool and your transform function have the same arguments. You can call `forward()` with the same argument names as the parent tool: + +```python {15} +from fastmcp import FastMCP +from fastmcp.tools import Tool +from fastmcp.tools.tool_transform import forward + +mcp = FastMCP() + +@mcp.tool +def add(x: int, y: int) -> int: + """Adds two numbers.""" + return x + y + +async def ensure_positive(x: int, y: int) -> int: + if x <= 0 or y <= 0: + raise ValueError("x and y must be positive") + return await forward(x=x, y=y) + +new_tool = Tool.from_tool( + add, + transform_fn=ensure_positive, +) + +mcp.add_tool(new_tool) +``` + + + +When your transformed tool has different argument names than the parent tool, you can call `forward()` with the renamed arguments and it will automatically map the arguments to the parent tool's arguments: + +```python {15, 20-23} +from fastmcp import FastMCP +from fastmcp.tools import Tool +from fastmcp.tools.tool_transform import forward + +mcp = FastMCP() + +@mcp.tool +def add(x: int, y: int) -> int: + """Adds two numbers.""" + return x + y + +async def ensure_positive(a: int, b: int) -> int: + if a <= 0 or b <= 0: + raise ValueError("a and b must be positive") + return await forward(a=a, b=b) + +new_tool = Tool.from_tool( + add, + transform_fn=ensure_positive, + transform_args={ + "x": ArgTransform(name="a"), + "y": ArgTransform(name="b"), + } +) + +mcp.add_tool(new_tool) +``` + + +Finally, you can use `forward_raw()` to bypass all argument mapping and call the parent tool directly with its original argument names. + +```python {15, 20-23} +from fastmcp import FastMCP +from fastmcp.tools import Tool +from fastmcp.tools.tool_transform import forward + +mcp = FastMCP() + +@mcp.tool +def add(x: int, y: int) -> int: + """Adds two numbers.""" + return x + y + +async def ensure_positive(a: int, b: int) -> int: + if a <= 0 or b <= 0: + raise ValueError("a and b must be positive") + return await forward_raw(x=a, y=b) + +new_tool = Tool.from_tool( + add, + transform_fn=ensure_positive, + transform_args={ + "x": ArgTransform(name="a"), + "y": ArgTransform(name="b"), + } +) + +mcp.add_tool(new_tool) +``` + + + +### Passing Arguments with **kwargs + +If your `transform_fn` includes `**kwargs` in its signature, it will receive **all arguments from the parent tool after `ArgTransform` configurations have been applied**. This is powerful for creating flexible validation functions that don't require you to add every argument to the function signature. + +In the following example, we wrap a parent tool that accepts two arguments `x` and `y`. These are renamed to `a` and `b` in the transformed tool, and the transform only validates `a`, passing the other argument through as `**kwargs`. + +```python {12, 15} +from fastmcp import FastMCP +from fastmcp.tools import Tool +from fastmcp.tools.tool_transform import forward + +mcp = FastMCP() + +@mcp.tool +def add(x: int, y: int) -> int: + """Adds two numbers.""" + return x + y + +async def ensure_a_positive(a: int, **kwargs) -> int: + if a <= 0: + raise ValueError("a must be positive") + return await forward(a=a, **kwargs) + +new_tool = Tool.from_tool( + add, + transform_fn=ensure_a_positive, + transform_args={ + "x": ArgTransform(name="a"), + "y": ArgTransform(name="b"), + } +) + +mcp.add_tool(new_tool) +``` + + +In the above example, `**kwargs` receives the renamed argument `b`, not the original argument `y`. It is therefore recommended to use with `forward()`, not `forward_raw()`. + + +## Modifying MCP Tools with MCPConfig + +When running MCP Servers under FastMCP with `MCPConfig`, you can also apply a subset of tool transformations +directly in the MCPConfig json file. + +```json +{ + "mcpServers": { + "weather": { + "url": "https://weather.example.com/mcp", + "transport": "http", + "tools": { + "weather_get_forecast": { + "name": "miami_weather", + "description": "Get the weather for Miami", + "meta": { + "category": "weather", + "location": "miami" + }, + "arguments": { + "city": { + "name": "city", + "default": "Miami", + "hide": True, + } + } + } + } + } + } +} +``` + +The `tools` section is a dictionary of tool names to tool configurations. Each tool configuration is a +dictionary of tool properties. + +See the [MCPConfigTransport](/clients/transports#tool-transformation-with-fastmcp-and-mcpconfig) documentation for more details. + + +## Output Schema Control + + + +Transformed tools inherit output schemas from their parent by default, but you can control this behavior: + +**Inherit from Parent (Default)** +```python +Tool.from_tool(parent_tool, name="renamed_tool") +``` +The transformed tool automatically uses the parent tool's output schema and structured output behavior. + +**Custom Output Schema** +```python +Tool.from_tool(parent_tool, output_schema={ + "type": "object", + "properties": {"status": {"type": "string"}} +}) +``` +Provide your own schema that differs from the parent. The tool must return data matching this schema. + +**Remove Output Schema** +```python +Tool.from_tool(parent_tool, output_schema=False) +``` +Removes the output schema declaration. Automatic structured content still works for object-like returns (dict, dataclass, Pydantic models) but primitive types won't be structured. + +**Full Control with Transform Functions** +```python +async def custom_output(**kwargs) -> ToolResult: + result = await forward(**kwargs) + return ToolResult(content=[...], structured_content={...}) + +Tool.from_tool(parent_tool, transform_fn=custom_output) +``` +Use a transform function returning `ToolResult` for complete control over both content blocks and structured outputs. + +## Common Patterns + +Tool transformation is a flexible feature that supports many powerful patterns. Here are a few common use cases to give you ideas. + +### Adapting Remote or Generated Tools +This is one of the most common reasons to use tool transformation. Tools from remote servers (via a [proxy](/servers/proxy)) or generated from an [OpenAPI spec](/integrations/openapi) are often too generic for direct use by an LLM. You can use transformation to create a simpler, more intuitive version for your specific needs. + +### Chaining Transformations +You can chain transformations by using an already transformed tool as the parent for a new transformation. This lets you build up complex behaviors in layers, for example, first renaming arguments, and then adding validation logic to the renamed tool. + +### Context-Aware Tool Factories +You can write functions that act as "factories," generating specialized versions of a tool for different contexts. For example, you could create a `get_my_data` tool that is specific to the currently logged-in user by hiding the `user_id` parameter and providing it automatically. diff --git a/docs/public/schemas/fastmcp.json/latest.json b/docs/public/schemas/fastmcp.json/latest.json new file mode 100644 index 000000000..aa1f59ce4 --- /dev/null +++ b/docs/public/schemas/fastmcp.json/latest.json @@ -0,0 +1,365 @@ +{ + "$defs": { + "Deployment": { + "description": "Configuration for server deployment and runtime settings.", + "properties": { + "transport": { + "anyOf": [ + { + "enum": [ + "stdio", + "http", + "sse", + "streamable-http" + ], + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Transport protocol to use", + "title": "Transport" + }, + "host": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Host to bind to when using HTTP transport", + "examples": [ + "127.0.0.1", + "0.0.0.0", + "localhost" + ], + "title": "Host" + }, + "port": { + "anyOf": [ + { + "type": "integer" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Port to bind to when using HTTP transport", + "examples": [ + 8000, + 3000, + 5000 + ], + "title": "Port" + }, + "path": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "URL path for the server endpoint", + "examples": [ + "/mcp/", + "/api/mcp/", + "/sse/" + ], + "title": "Path" + }, + "log_level": { + "anyOf": [ + { + "enum": [ + "DEBUG", + "INFO", + "WARNING", + "ERROR", + "CRITICAL" + ], + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Log level for the server", + "title": "Log Level" + }, + "cwd": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Working directory for the server process", + "examples": [ + ".", + "./src", + "/app" + ], + "title": "Cwd" + }, + "env": { + "anyOf": [ + { + "additionalProperties": { + "type": "string" + }, + "type": "object" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Environment variables to set when running the server", + "examples": [ + { + "API_KEY": "secret", + "DEBUG": "true" + } + ], + "title": "Env" + }, + "args": { + "anyOf": [ + { + "items": { + "type": "string" + }, + "type": "array" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Arguments to pass to the server (after --)", + "examples": [ + [ + "--config", + "config.json", + "--debug" + ] + ], + "title": "Args" + } + }, + "title": "Deployment", + "type": "object" + }, + "FileSystemSource": { + "description": "Source for local Python files.", + "properties": { + "type": { + "const": "filesystem", + "default": "filesystem", + "title": "Type", + "type": "string" + }, + "path": { + "description": "Path to Python file containing the server", + "title": "Path", + "type": "string" + }, + "entrypoint": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Name of server instance or factory function (a no-arg function that returns a FastMCP server)", + "title": "Entrypoint" + } + }, + "required": [ + "path" + ], + "title": "FileSystemSource", + "type": "object" + }, + "UVEnvironment": { + "description": "Configuration for Python environment setup.", + "properties": { + "type": { + "const": "uv", + "default": "uv", + "title": "Type", + "type": "string" + }, + "python": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Python version constraint", + "examples": [ + "3.10", + "3.11", + "3.12" + ], + "title": "Python" + }, + "dependencies": { + "anyOf": [ + { + "items": { + "type": "string" + }, + "type": "array" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Python packages to install with PEP 508 specifiers", + "examples": [ + [ + "fastmcp>=2.0,<3", + "httpx", + "pandas>=2.0" + ] + ], + "title": "Dependencies" + }, + "requirements": { + "anyOf": [ + { + "format": "path", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Path to requirements.txt file", + "examples": [ + "requirements.txt", + "../requirements/prod.txt" + ], + "title": "Requirements" + }, + "project": { + "anyOf": [ + { + "format": "path", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Path to project directory containing pyproject.toml", + "examples": [ + ".", + "../my-project" + ], + "title": "Project" + }, + "editable": { + "anyOf": [ + { + "items": { + "format": "path", + "type": "string" + }, + "type": "array" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Directories to install in editable mode", + "examples": [ + [ + ".", + "../my-package" + ], + [ + "/path/to/package" + ] + ], + "title": "Editable" + } + }, + "title": "UVEnvironment", + "type": "object" + } + }, + "description": "Configuration file for FastMCP servers", + "properties": { + "$schema": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": "https://gofastmcp.com/public/schemas/fastmcp.json/v1.json", + "description": "JSON schema for IDE support and validation", + "title": "$Schema" + }, + "source": { + "$ref": "#/$defs/FileSystemSource", + "description": "Source configuration for the server", + "examples": [ + { + "path": "server.py" + }, + { + "entrypoint": "app", + "path": "server.py" + }, + { + "entrypoint": "mcp", + "path": "src/server.py", + "type": "filesystem" + } + ] + }, + "environment": { + "$ref": "#/$defs/UVEnvironment", + "description": "Python environment setup configuration" + }, + "deployment": { + "$ref": "#/$defs/Deployment", + "description": "Server deployment and runtime settings" + } + }, + "required": [ + "source" + ], + "title": "FastMCP Configuration", + "type": "object", + "$id": "https://gofastmcp.com/public/schemas/fastmcp.json/v1.json" +} diff --git a/docs/public/schemas/fastmcp.json/v1.json b/docs/public/schemas/fastmcp.json/v1.json new file mode 100644 index 000000000..aa1f59ce4 --- /dev/null +++ b/docs/public/schemas/fastmcp.json/v1.json @@ -0,0 +1,365 @@ +{ + "$defs": { + "Deployment": { + "description": "Configuration for server deployment and runtime settings.", + "properties": { + "transport": { + "anyOf": [ + { + "enum": [ + "stdio", + "http", + "sse", + "streamable-http" + ], + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Transport protocol to use", + "title": "Transport" + }, + "host": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Host to bind to when using HTTP transport", + "examples": [ + "127.0.0.1", + "0.0.0.0", + "localhost" + ], + "title": "Host" + }, + "port": { + "anyOf": [ + { + "type": "integer" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Port to bind to when using HTTP transport", + "examples": [ + 8000, + 3000, + 5000 + ], + "title": "Port" + }, + "path": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "URL path for the server endpoint", + "examples": [ + "/mcp/", + "/api/mcp/", + "/sse/" + ], + "title": "Path" + }, + "log_level": { + "anyOf": [ + { + "enum": [ + "DEBUG", + "INFO", + "WARNING", + "ERROR", + "CRITICAL" + ], + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Log level for the server", + "title": "Log Level" + }, + "cwd": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Working directory for the server process", + "examples": [ + ".", + "./src", + "/app" + ], + "title": "Cwd" + }, + "env": { + "anyOf": [ + { + "additionalProperties": { + "type": "string" + }, + "type": "object" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Environment variables to set when running the server", + "examples": [ + { + "API_KEY": "secret", + "DEBUG": "true" + } + ], + "title": "Env" + }, + "args": { + "anyOf": [ + { + "items": { + "type": "string" + }, + "type": "array" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Arguments to pass to the server (after --)", + "examples": [ + [ + "--config", + "config.json", + "--debug" + ] + ], + "title": "Args" + } + }, + "title": "Deployment", + "type": "object" + }, + "FileSystemSource": { + "description": "Source for local Python files.", + "properties": { + "type": { + "const": "filesystem", + "default": "filesystem", + "title": "Type", + "type": "string" + }, + "path": { + "description": "Path to Python file containing the server", + "title": "Path", + "type": "string" + }, + "entrypoint": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Name of server instance or factory function (a no-arg function that returns a FastMCP server)", + "title": "Entrypoint" + } + }, + "required": [ + "path" + ], + "title": "FileSystemSource", + "type": "object" + }, + "UVEnvironment": { + "description": "Configuration for Python environment setup.", + "properties": { + "type": { + "const": "uv", + "default": "uv", + "title": "Type", + "type": "string" + }, + "python": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Python version constraint", + "examples": [ + "3.10", + "3.11", + "3.12" + ], + "title": "Python" + }, + "dependencies": { + "anyOf": [ + { + "items": { + "type": "string" + }, + "type": "array" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Python packages to install with PEP 508 specifiers", + "examples": [ + [ + "fastmcp>=2.0,<3", + "httpx", + "pandas>=2.0" + ] + ], + "title": "Dependencies" + }, + "requirements": { + "anyOf": [ + { + "format": "path", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Path to requirements.txt file", + "examples": [ + "requirements.txt", + "../requirements/prod.txt" + ], + "title": "Requirements" + }, + "project": { + "anyOf": [ + { + "format": "path", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Path to project directory containing pyproject.toml", + "examples": [ + ".", + "../my-project" + ], + "title": "Project" + }, + "editable": { + "anyOf": [ + { + "items": { + "format": "path", + "type": "string" + }, + "type": "array" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Directories to install in editable mode", + "examples": [ + [ + ".", + "../my-package" + ], + [ + "/path/to/package" + ] + ], + "title": "Editable" + } + }, + "title": "UVEnvironment", + "type": "object" + } + }, + "description": "Configuration file for FastMCP servers", + "properties": { + "$schema": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": "https://gofastmcp.com/public/schemas/fastmcp.json/v1.json", + "description": "JSON schema for IDE support and validation", + "title": "$Schema" + }, + "source": { + "$ref": "#/$defs/FileSystemSource", + "description": "Source configuration for the server", + "examples": [ + { + "path": "server.py" + }, + { + "entrypoint": "app", + "path": "server.py" + }, + { + "entrypoint": "mcp", + "path": "src/server.py", + "type": "filesystem" + } + ] + }, + "environment": { + "$ref": "#/$defs/UVEnvironment", + "description": "Python environment setup configuration" + }, + "deployment": { + "$ref": "#/$defs/Deployment", + "description": "Server deployment and runtime settings" + } + }, + "required": [ + "source" + ], + "title": "FastMCP Configuration", + "type": "object", + "$id": "https://gofastmcp.com/public/schemas/fastmcp.json/v1.json" +} diff --git a/docs/python-sdk/fastmcp-cli-__init__.mdx b/docs/python-sdk/fastmcp-cli-__init__.mdx new file mode 100644 index 000000000..d2873740a --- /dev/null +++ b/docs/python-sdk/fastmcp-cli-__init__.mdx @@ -0,0 +1,9 @@ +--- +title: __init__ +sidebarTitle: __init__ +--- + +# `fastmcp.cli` + + +FastMCP CLI package. diff --git a/docs/python-sdk/fastmcp-cli-claude.mdx b/docs/python-sdk/fastmcp-cli-claude.mdx new file mode 100644 index 000000000..82eb6e881 --- /dev/null +++ b/docs/python-sdk/fastmcp-cli-claude.mdx @@ -0,0 +1,43 @@ +--- +title: claude +sidebarTitle: claude +--- + +# `fastmcp.cli.claude` + + +Claude app integration utilities. + +## Functions + +### `get_claude_config_path` + +```python +get_claude_config_path() -> Path | None +``` + + +Get the Claude config directory based on platform. + + +### `update_claude_config` + +```python +update_claude_config(file_spec: str, server_name: str) -> bool +``` + + +Add or update a FastMCP server in Claude's configuration. + +**Args:** +- `file_spec`: Path to the server file, optionally with \:object suffix +- `server_name`: Name for the server in Claude's config +- `with_editable`: Optional list of directories to install in editable mode +- `with_packages`: Optional list of additional packages to install +- `env_vars`: Optional dictionary of environment variables. These are merged with +any existing variables, with new values taking precedence. + +**Raises:** +- `RuntimeError`: If Claude Desktop's config directory is not found, indicating +Claude Desktop may not be installed or properly set up. + diff --git a/docs/python-sdk/fastmcp-cli-cli.mdx b/docs/python-sdk/fastmcp-cli-cli.mdx new file mode 100644 index 000000000..a6f4ee13b --- /dev/null +++ b/docs/python-sdk/fastmcp-cli-cli.mdx @@ -0,0 +1,129 @@ +--- +title: cli +sidebarTitle: cli +--- + +# `fastmcp.cli.cli` + + +FastMCP CLI tools using Cyclopts. + +## Functions + +### `with_argv` + +```python +with_argv(args: list[str] | None) +``` + + +Temporarily replace sys.argv if args provided. + +This context manager is used at the CLI boundary to inject +server arguments when needed, without mutating sys.argv deep +in the source loading logic. + +Args are provided without the script name, so we preserve sys.argv[0] +and replace the rest. + + +### `version` + +```python +version() +``` + + +Display version information and platform details. + + +### `dev` + +```python +dev(server_spec: str | None = None) -> None +``` + + +Run an MCP server with the MCP Inspector for development. + +**Args:** +- `server_spec`: Python file to run, optionally with \:object suffix, or None to auto-detect fastmcp.json + + +### `run` + +```python +run(server_spec: str | None = None, *server_args: str) -> None +``` + + +Run an MCP server or connect to a remote one. + +The server can be specified in several ways: +1. Module approach: "server.py" - runs the module directly, looking for an object named 'mcp', 'server', or 'app' +2. Import approach: "server.py:app" - imports and runs the specified server object +3. URL approach: "http://server-url" - connects to a remote server and creates a proxy +4. MCPConfig file: "mcp.json" - runs as a proxy server for the MCP Servers in the MCPConfig file +5. FastMCP config: "fastmcp.json" - runs server using FastMCP configuration +6. No argument: looks for fastmcp.json in current directory + +Server arguments can be passed after -- : +fastmcp run server.py -- --config config.json --debug + +**Args:** +- `server_spec`: Python file, object specification (file\:obj), config file, URL, or None to auto-detect + + +### `inspect` + +```python +inspect(server_spec: str | None = None) -> None +``` + + +Inspect an MCP server and display information or generate a JSON report. + +This command analyzes an MCP server. Without flags, it displays a text summary. +Use --format to output complete JSON data. + +**Examples:** + +# Show text summary +fastmcp inspect server.py + +# Output FastMCP format JSON to stdout +fastmcp inspect server.py --format fastmcp + +# Save MCP protocol format to file (format required with -o) +fastmcp inspect server.py --format mcp -o manifest.json + +# Inspect from fastmcp.json configuration +fastmcp inspect fastmcp.json +fastmcp inspect # auto-detect fastmcp.json + +**Args:** +- `server_spec`: Python file to inspect, optionally with \:object suffix, or fastmcp.json + + +### `prepare` + +```python +prepare(config_path: Annotated[str | None, cyclopts.Parameter(help='Path to fastmcp.json configuration file')] = None, output_dir: Annotated[str | None, cyclopts.Parameter(help='Directory to create the persistent environment in')] = None, skip_source: Annotated[bool, cyclopts.Parameter(help='Skip source preparation (e.g., git clone)')] = False) -> None +``` + + +Prepare a FastMCP project by creating a persistent uv environment. + +This command creates a persistent uv project with all dependencies installed: +- Creates a pyproject.toml with dependencies from the config +- Installs all Python packages into a .venv +- Prepares the source (git clone, download, etc.) unless --skip-source + +After running this command, you can use: +fastmcp run <config> --project <output-dir> + +This is useful for: +- CI/CD pipelines with separate build and run stages +- Docker images where you prepare during build +- Production deployments where you want fast startup times + diff --git a/docs/python-sdk/fastmcp-cli-install-__init__.mdx b/docs/python-sdk/fastmcp-cli-install-__init__.mdx new file mode 100644 index 000000000..3909565f2 --- /dev/null +++ b/docs/python-sdk/fastmcp-cli-install-__init__.mdx @@ -0,0 +1,9 @@ +--- +title: __init__ +sidebarTitle: __init__ +--- + +# `fastmcp.cli.install` + + +Install subcommands for FastMCP CLI using Cyclopts. diff --git a/docs/python-sdk/fastmcp-cli-install-claude_code.mdx b/docs/python-sdk/fastmcp-cli-install-claude_code.mdx new file mode 100644 index 000000000..e1bf560ca --- /dev/null +++ b/docs/python-sdk/fastmcp-cli-install-claude_code.mdx @@ -0,0 +1,71 @@ +--- +title: claude_code +sidebarTitle: claude_code +--- + +# `fastmcp.cli.install.claude_code` + + +Claude Code integration for FastMCP install using Cyclopts. + +## Functions + +### `find_claude_command` + +```python +find_claude_command() -> str | None +``` + + +Find the Claude Code CLI command. + +Checks common installation locations since 'claude' is often a shell alias +that doesn't work with subprocess calls. + + +### `check_claude_code_available` + +```python +check_claude_code_available() -> bool +``` + + +Check if Claude Code CLI is available. + + +### `install_claude_code` + +```python +install_claude_code(file: Path, server_object: str | None, name: str) -> bool +``` + + +Install FastMCP server in Claude Code. + +**Args:** +- `file`: Path to the server file +- `server_object`: Optional server object name (for \:object suffix) +- `name`: Name for the server in Claude Code +- `with_editable`: Optional list of directories to install in editable mode +- `with_packages`: Optional list of additional packages to install +- `env_vars`: Optional dictionary of environment variables +- `python_version`: Optional Python version to use +- `with_requirements`: Optional requirements file to install from +- `project`: Optional project directory to run within + +**Returns:** +- True if installation was successful, False otherwise + + +### `claude_code_command` + +```python +claude_code_command(server_spec: str) -> None +``` + + +Install an MCP server in Claude Code. + +**Args:** +- `server_spec`: Python file to install, optionally with \:object suffix + diff --git a/docs/python-sdk/fastmcp-cli-install-claude_desktop.mdx b/docs/python-sdk/fastmcp-cli-install-claude_desktop.mdx new file mode 100644 index 000000000..1b36835e4 --- /dev/null +++ b/docs/python-sdk/fastmcp-cli-install-claude_desktop.mdx @@ -0,0 +1,58 @@ +--- +title: claude_desktop +sidebarTitle: claude_desktop +--- + +# `fastmcp.cli.install.claude_desktop` + + +Claude Desktop integration for FastMCP install using Cyclopts. + +## Functions + +### `get_claude_config_path` + +```python +get_claude_config_path() -> Path | None +``` + + +Get the Claude config directory based on platform. + + +### `install_claude_desktop` + +```python +install_claude_desktop(file: Path, server_object: str | None, name: str) -> bool +``` + + +Install FastMCP server in Claude Desktop. + +**Args:** +- `file`: Path to the server file +- `server_object`: Optional server object name (for \:object suffix) +- `name`: Name for the server in Claude's config +- `with_editable`: Optional list of directories to install in editable mode +- `with_packages`: Optional list of additional packages to install +- `env_vars`: Optional dictionary of environment variables +- `python_version`: Optional Python version to use +- `with_requirements`: Optional requirements file to install from +- `project`: Optional project directory to run within + +**Returns:** +- True if installation was successful, False otherwise + + +### `claude_desktop_command` + +```python +claude_desktop_command(server_spec: str) -> None +``` + + +Install an MCP server in Claude Desktop. + +**Args:** +- `server_spec`: Python file to install, optionally with \:object suffix + diff --git a/docs/python-sdk/fastmcp-cli-install-cursor.mdx b/docs/python-sdk/fastmcp-cli-install-cursor.mdx new file mode 100644 index 000000000..0dc106747 --- /dev/null +++ b/docs/python-sdk/fastmcp-cli-install-cursor.mdx @@ -0,0 +1,107 @@ +--- +title: cursor +sidebarTitle: cursor +--- + +# `fastmcp.cli.install.cursor` + + +Cursor integration for FastMCP install using Cyclopts. + +## Functions + +### `generate_cursor_deeplink` + +```python +generate_cursor_deeplink(server_name: str, server_config: StdioMCPServer) -> str +``` + + +Generate a Cursor deeplink for installing the MCP server. + +**Args:** +- `server_name`: Name of the server +- `server_config`: Server configuration + +**Returns:** +- Deeplink URL that can be clicked to install the server + + +### `open_deeplink` + +```python +open_deeplink(deeplink: str) -> bool +``` + + +Attempt to open a deeplink URL using the system's default handler. + +**Args:** +- `deeplink`: The deeplink URL to open + +**Returns:** +- True if the command succeeded, False otherwise + + +### `install_cursor_workspace` + +```python +install_cursor_workspace(file: Path, server_object: str | None, name: str, workspace_path: Path) -> bool +``` + + +Install FastMCP server to workspace-specific Cursor configuration. + +**Args:** +- `file`: Path to the server file +- `server_object`: Optional server object name (for \:object suffix) +- `name`: Name for the server in Cursor +- `workspace_path`: Path to the workspace directory +- `with_editable`: Optional list of directories to install in editable mode +- `with_packages`: Optional list of additional packages to install +- `env_vars`: Optional dictionary of environment variables +- `python_version`: Optional Python version to use +- `with_requirements`: Optional requirements file to install from +- `project`: Optional project directory to run within + +**Returns:** +- True if installation was successful, False otherwise + + +### `install_cursor` + +```python +install_cursor(file: Path, server_object: str | None, name: str) -> bool +``` + + +Install FastMCP server in Cursor. + +**Args:** +- `file`: Path to the server file +- `server_object`: Optional server object name (for \:object suffix) +- `name`: Name for the server in Cursor +- `with_editable`: Optional list of directories to install in editable mode +- `with_packages`: Optional list of additional packages to install +- `env_vars`: Optional dictionary of environment variables +- `python_version`: Optional Python version to use +- `with_requirements`: Optional requirements file to install from +- `project`: Optional project directory to run within +- `workspace`: Optional workspace directory for project-specific installation + +**Returns:** +- True if installation was successful, False otherwise + + +### `cursor_command` + +```python +cursor_command(server_spec: str) -> None +``` + + +Install an MCP server in Cursor. + +**Args:** +- `server_spec`: Python file to install, optionally with \:object suffix + diff --git a/docs/python-sdk/fastmcp-cli-install-gemini_cli.mdx b/docs/python-sdk/fastmcp-cli-install-gemini_cli.mdx new file mode 100644 index 000000000..7cbf60153 --- /dev/null +++ b/docs/python-sdk/fastmcp-cli-install-gemini_cli.mdx @@ -0,0 +1,68 @@ +--- +title: gemini_cli +sidebarTitle: gemini_cli +--- + +# `fastmcp.cli.install.gemini_cli` + + +Gemini CLI integration for FastMCP install using Cyclopts. + +## Functions + +### `find_gemini_command` + +```python +find_gemini_command() -> str | None +``` + + +Find the Gemini CLI command. + + +### `check_gemini_cli_available` + +```python +check_gemini_cli_available() -> bool +``` + + +Check if Gemini CLI is available. + + +### `install_gemini_cli` + +```python +install_gemini_cli(file: Path, server_object: str | None, name: str) -> bool +``` + + +Install FastMCP server in Gemini CLI. + +**Args:** +- `file`: Path to the server file +- `server_object`: Optional server object name (for \:object suffix) +- `name`: Name for the server in Gemini CLI +- `with_editable`: Optional list of directories to install in editable mode +- `with_packages`: Optional list of additional packages to install +- `env_vars`: Optional dictionary of environment variables +- `python_version`: Optional Python version to use +- `with_requirements`: Optional requirements file to install from +- `project`: Optional project directory to run within + +**Returns:** +- True if installation was successful, False otherwise + + +### `gemini_cli_command` + +```python +gemini_cli_command(server_spec: str) -> None +``` + + +Install an MCP server in Gemini CLI. + +**Args:** +- `server_spec`: Python file to install, optionally with \:object suffix + diff --git a/docs/python-sdk/fastmcp-cli-install-mcp_json.mdx b/docs/python-sdk/fastmcp-cli-install-mcp_json.mdx new file mode 100644 index 000000000..56d41e746 --- /dev/null +++ b/docs/python-sdk/fastmcp-cli-install-mcp_json.mdx @@ -0,0 +1,49 @@ +--- +title: mcp_json +sidebarTitle: mcp_json +--- + +# `fastmcp.cli.install.mcp_json` + + +MCP configuration JSON generation for FastMCP install using Cyclopts. + +## Functions + +### `install_mcp_json` + +```python +install_mcp_json(file: Path, server_object: str | None, name: str) -> bool +``` + + +Generate MCP configuration JSON for manual installation. + +**Args:** +- `file`: Path to the server file +- `server_object`: Optional server object name (for \:object suffix) +- `name`: Name for the server in MCP config +- `with_editable`: Optional list of directories to install in editable mode +- `with_packages`: Optional list of additional packages to install +- `env_vars`: Optional dictionary of environment variables +- `copy`: If True, copy to clipboard instead of printing to stdout +- `python_version`: Optional Python version to use +- `with_requirements`: Optional requirements file to install from +- `project`: Optional project directory to run within + +**Returns:** +- True if generation was successful, False otherwise + + +### `mcp_json_command` + +```python +mcp_json_command(server_spec: str) -> None +``` + + +Generate MCP configuration JSON for manual installation. + +**Args:** +- `server_spec`: Python file to install, optionally with \:object suffix + diff --git a/docs/python-sdk/fastmcp-cli-install-shared.mdx b/docs/python-sdk/fastmcp-cli-install-shared.mdx new file mode 100644 index 000000000..337179267 --- /dev/null +++ b/docs/python-sdk/fastmcp-cli-install-shared.mdx @@ -0,0 +1,33 @@ +--- +title: shared +sidebarTitle: shared +--- + +# `fastmcp.cli.install.shared` + + +Shared utilities for install commands. + +## Functions + +### `parse_env_var` + +```python +parse_env_var(env_var: str) -> tuple[str, str] +``` + + +Parse environment variable string in format KEY=VALUE. + + +### `process_common_args` + +```python +process_common_args(server_spec: str, server_name: str | None, with_packages: list[str] | None, env_vars: list[str] | None, env_file: Path | None) -> tuple[Path, str | None, str, list[str], dict[str, str] | None] +``` + + +Process common arguments shared by all install commands. + +Handles both fastmcp.json config files and traditional file.py:object syntax. + diff --git a/docs/python-sdk/fastmcp-cli-run.mdx b/docs/python-sdk/fastmcp-cli-run.mdx new file mode 100644 index 000000000..f9a5f874d --- /dev/null +++ b/docs/python-sdk/fastmcp-cli-run.mdx @@ -0,0 +1,91 @@ +--- +title: run +sidebarTitle: run +--- + +# `fastmcp.cli.run` + + +FastMCP run command implementation with enhanced type hints. + +## Functions + +### `is_url` + +```python +is_url(path: str) -> bool +``` + + +Check if a string is a URL. + + +### `create_client_server` + +```python +create_client_server(url: str) -> Any +``` + + +Create a FastMCP server from a client URL. + +**Args:** +- `url`: The URL to connect to + +**Returns:** +- A FastMCP server instance + + +### `create_mcp_config_server` + +```python +create_mcp_config_server(mcp_config_path: Path) -> FastMCP[None] +``` + + +Create a FastMCP server from a MCPConfig. + + +### `load_mcp_server_config` + +```python +load_mcp_server_config(config_path: Path) -> MCPServerConfig +``` + + +Load a FastMCP configuration from a fastmcp.json file. + +**Args:** +- `config_path`: Path to fastmcp.json file + +**Returns:** +- MCPServerConfig object + + +### `run_command` + +```python +run_command(server_spec: str, transport: TransportType | None = None, host: str | None = None, port: int | None = None, path: str | None = None, log_level: LogLevelType | None = None, server_args: list[str] | None = None, show_banner: bool = True, use_direct_import: bool = False, skip_source: bool = False) -> None +``` + + +Run a MCP server or connect to a remote one. + +**Args:** +- `server_spec`: Python file, object specification (file\:obj), config file, or URL +- `transport`: Transport protocol to use +- `host`: Host to bind to when using http transport +- `port`: Port to bind to when using http transport +- `path`: Path to bind to when using http transport +- `log_level`: Log level +- `server_args`: Additional arguments to pass to the server +- `show_banner`: Whether to show the server banner +- `use_direct_import`: Whether to use direct import instead of subprocess +- `skip_source`: Whether to skip source preparation step + + +### `run_v1_server` + +```python +run_v1_server(server: FastMCP1x, host: str | None = None, port: int | None = None, transport: TransportType | None = None) -> None +``` diff --git a/docs/python-sdk/fastmcp-client-__init__.mdx b/docs/python-sdk/fastmcp-client-__init__.mdx new file mode 100644 index 000000000..bc145d4b7 --- /dev/null +++ b/docs/python-sdk/fastmcp-client-__init__.mdx @@ -0,0 +1,8 @@ +--- +title: __init__ +sidebarTitle: __init__ +--- + +# `fastmcp.client` + +*This module is empty or contains only private/internal implementations.* diff --git a/docs/python-sdk/fastmcp-client-auth-__init__.mdx b/docs/python-sdk/fastmcp-client-auth-__init__.mdx new file mode 100644 index 000000000..28242780d --- /dev/null +++ b/docs/python-sdk/fastmcp-client-auth-__init__.mdx @@ -0,0 +1,8 @@ +--- +title: __init__ +sidebarTitle: __init__ +--- + +# `fastmcp.client.auth` + +*This module is empty or contains only private/internal implementations.* diff --git a/docs/python-sdk/fastmcp-client-auth-bearer.mdx b/docs/python-sdk/fastmcp-client-auth-bearer.mdx new file mode 100644 index 000000000..c04905b14 --- /dev/null +++ b/docs/python-sdk/fastmcp-client-auth-bearer.mdx @@ -0,0 +1,18 @@ +--- +title: bearer +sidebarTitle: bearer +--- + +# `fastmcp.client.auth.bearer` + +## Classes + +### `BearerAuth` + +**Methods:** + +#### `auth_flow` + +```python +auth_flow(self, request) +``` diff --git a/docs/python-sdk/fastmcp-client-auth-oauth.mdx b/docs/python-sdk/fastmcp-client-auth-oauth.mdx new file mode 100644 index 000000000..cc3dd7cf5 --- /dev/null +++ b/docs/python-sdk/fastmcp-client-auth-oauth.mdx @@ -0,0 +1,160 @@ +--- +title: oauth +sidebarTitle: oauth +--- + +# `fastmcp.client.auth.oauth` + +## Functions + +### `default_cache_dir` + +```python +default_cache_dir() -> Path +``` + +### `check_if_auth_required` + +```python +check_if_auth_required(mcp_url: str, httpx_kwargs: dict[str, Any] | None = None) -> bool +``` + + +Check if the MCP endpoint requires authentication by making a test request. + +**Returns:** +- True if auth appears to be required, False otherwise + + +## Classes + +### `ClientNotFoundError` + + +Raised when OAuth client credentials are not found on the server. + + +### `StoredToken` + + +Token storage format with absolute expiry time. + + +### `FileTokenStorage` + + +File-based token storage implementation for OAuth credentials and tokens. +Implements the mcp.client.auth.TokenStorage protocol. + +Each instance is tied to a specific server URL for proper token isolation. +Uses JSONFileStorage internally for consistent file handling. + + +**Methods:** + +#### `get_base_url` + +```python +get_base_url(url: str) -> str +``` + +Extract the base URL (scheme + host) from a URL. + + +#### `get_tokens` + +```python +get_tokens(self) -> OAuthToken | None +``` + +Load tokens from file storage. + + +#### `set_tokens` + +```python +set_tokens(self, tokens: OAuthToken) -> None +``` + +Save tokens to file storage. + + +#### `get_client_info` + +```python +get_client_info(self) -> OAuthClientInformationFull | None +``` + +Load client information from file storage. + + +#### `set_client_info` + +```python +set_client_info(self, client_info: OAuthClientInformationFull) -> None +``` + +Save client information to file storage. + + +#### `clear` + +```python +clear(self) -> None +``` + +Clear all cached data for this server. + +Note: This is a synchronous method for backward compatibility. +Uses direct file operations instead of async storage methods. + + +#### `clear_all` + +```python +clear_all(cls, cache_dir: Path | None = None) -> None +``` + +Clear all cached data for all servers. + + +### `OAuth` + + +OAuth client provider for MCP servers with browser-based authentication. + +This class provides OAuth authentication for FastMCP clients by opening +a browser for user authorization and running a local callback server. + + +**Methods:** + +#### `redirect_handler` + +```python +redirect_handler(self, authorization_url: str) -> None +``` + +Open browser for authorization, with pre-flight check for invalid client. + + +#### `callback_handler` + +```python +callback_handler(self) -> tuple[str, str | None] +``` + +Handle OAuth callback and return (auth_code, state). + + +#### `async_auth_flow` + +```python +async_auth_flow(self, request: httpx.Request) -> AsyncGenerator[httpx.Request, httpx.Response] +``` + +HTTPX auth flow with automatic retry on stale cached credentials. + +If the OAuth flow fails due to invalid/stale client credentials, +clears the cache and retries once with fresh registration. + diff --git a/docs/python-sdk/fastmcp-client-client.mdx b/docs/python-sdk/fastmcp-client-client.mdx new file mode 100644 index 000000000..e089c8201 --- /dev/null +++ b/docs/python-sdk/fastmcp-client-client.mdx @@ -0,0 +1,509 @@ +--- +title: client +sidebarTitle: client +--- + +# `fastmcp.client.client` + +## Classes + +### `ClientSessionState` + + +Holds all session-related state for a Client instance. + +This allows clean separation of configuration (which is copied) from +session state (which should be fresh for each new client instance). + + +### `Client` + + +MCP client that delegates connection management to a Transport instance. + +The Client class is responsible for MCP protocol logic, while the Transport +handles connection establishment and management. Client provides methods for +working with resources, prompts, tools and other MCP capabilities. + +This client supports reentrant context managers (multiple concurrent +`async with client:` blocks) using reference counting and background session +management. This allows efficient session reuse in any scenario with +nested or concurrent client usage. + +MCP SDK 1.10 introduced automatic list_tools() calls during call_tool() +execution. This created a race condition where events could be reset while +other tasks were waiting on them, causing deadlocks. The issue was exposed +in proxy scenarios but affects any reentrant usage. + +The solution uses reference counting to track active context managers, +a background task to manage the session lifecycle, events to coordinate +between tasks, and ensures all session state changes happen within a lock. +Events are only created when needed, never reset outside locks. + +This design prevents race conditions where tasks wait on events that get +replaced by other tasks, ensuring reliable coordination in concurrent scenarios. + +**Args:** +- `transport`: +Connection source specification, which can be\: + + - ClientTransport\: Direct transport instance + - FastMCP\: In-process FastMCP server + - AnyUrl or str\: URL to connect to + - Path\: File path for local socket + - MCPConfig\: MCP server configuration + - dict\: Transport configuration +- `roots`: Optional RootsList or RootsHandler for filesystem access +- `sampling_handler`: Optional handler for sampling requests +- `log_handler`: Optional handler for log messages +- `message_handler`: Optional handler for protocol messages +- `progress_handler`: Optional handler for progress notifications +- `timeout`: Optional timeout for requests (seconds or timedelta) +- `init_timeout`: Optional timeout for initial connection (seconds or timedelta). +Set to 0 to disable. If None, uses the value in the FastMCP global settings. + +**Examples:** + +```python +# Connect to FastMCP server +client = Client("http://localhost:8080") + +async with client: + # List available resources + resources = await client.list_resources() + + # Call a tool + result = await client.call_tool("my_tool", {"param": "value"}) +``` + + +**Methods:** + +#### `session` + +```python +session(self) -> ClientSession +``` + +Get the current active session. Raises RuntimeError if not connected. + + +#### `initialize_result` + +```python +initialize_result(self) -> mcp.types.InitializeResult +``` + +Get the result of the initialization request. + + +#### `set_roots` + +```python +set_roots(self, roots: RootsList | RootsHandler) -> None +``` + +Set the roots for the client. This does not automatically call `send_roots_list_changed`. + + +#### `set_sampling_callback` + +```python +set_sampling_callback(self, sampling_callback: ClientSamplingHandler) -> None +``` + +Set the sampling callback for the client. + + +#### `set_elicitation_callback` + +```python +set_elicitation_callback(self, elicitation_callback: ElicitationHandler) -> None +``` + +Set the elicitation callback for the client. + + +#### `is_connected` + +```python +is_connected(self) -> bool +``` + +Check if the client is currently connected. + + +#### `new` + +```python +new(self) -> Client[ClientTransportT] +``` + +Create a new client instance with the same configuration but fresh session state. + +This creates a new client with the same transport, handlers, and configuration, +but with no active session. Useful for creating independent sessions that don't +share state with the original client. + +**Returns:** +- A new Client instance with the same configuration but disconnected state. + + +#### `close` + +```python +close(self) +``` + +#### `ping` + +```python +ping(self) -> bool +``` + +Send a ping request. + + +#### `cancel` + +```python +cancel(self, request_id: str | int, reason: str | None = None) -> None +``` + +Send a cancellation notification for an in-progress request. + + +#### `progress` + +```python +progress(self, progress_token: str | int, progress: float, total: float | None = None, message: str | None = None) -> None +``` + +Send a progress notification. + + +#### `set_logging_level` + +```python +set_logging_level(self, level: mcp.types.LoggingLevel) -> None +``` + +Send a logging/setLevel request. + + +#### `send_roots_list_changed` + +```python +send_roots_list_changed(self) -> None +``` + +Send a roots/list_changed notification. + + +#### `list_resources_mcp` + +```python +list_resources_mcp(self) -> mcp.types.ListResourcesResult +``` + +Send a resources/list request and return the complete MCP protocol result. + +**Returns:** +- mcp.types.ListResourcesResult: The complete response object from the protocol, +containing the list of resources and any additional metadata. + +**Raises:** +- `RuntimeError`: If called while the client is not connected. + + +#### `list_resources` + +```python +list_resources(self) -> list[mcp.types.Resource] +``` + +Retrieve a list of resources available on the server. + +**Returns:** +- list\[mcp.types.Resource]: A list of Resource objects. + +**Raises:** +- `RuntimeError`: If called while the client is not connected. + + +#### `list_resource_templates_mcp` + +```python +list_resource_templates_mcp(self) -> mcp.types.ListResourceTemplatesResult +``` + +Send a resources/listResourceTemplates request and return the complete MCP protocol result. + +**Returns:** +- mcp.types.ListResourceTemplatesResult: The complete response object from the protocol, +containing the list of resource templates and any additional metadata. + +**Raises:** +- `RuntimeError`: If called while the client is not connected. + + +#### `list_resource_templates` + +```python +list_resource_templates(self) -> list[mcp.types.ResourceTemplate] +``` + +Retrieve a list of resource templates available on the server. + +**Returns:** +- list\[mcp.types.ResourceTemplate]: A list of ResourceTemplate objects. + +**Raises:** +- `RuntimeError`: If called while the client is not connected. + + +#### `read_resource_mcp` + +```python +read_resource_mcp(self, uri: AnyUrl | str) -> mcp.types.ReadResourceResult +``` + +Send a resources/read request and return the complete MCP protocol result. + +**Args:** +- `uri`: The URI of the resource to read. Can be a string or an AnyUrl object. + +**Returns:** +- mcp.types.ReadResourceResult: The complete response object from the protocol, +containing the resource contents and any additional metadata. + +**Raises:** +- `RuntimeError`: If called while the client is not connected. + + +#### `read_resource` + +```python +read_resource(self, uri: AnyUrl | str) -> list[mcp.types.TextResourceContents | mcp.types.BlobResourceContents] +``` + +Read the contents of a resource or resolved template. + +**Args:** +- `uri`: The URI of the resource to read. Can be a string or an AnyUrl object. + +**Returns:** +- list\[mcp.types.TextResourceContents | mcp.types.BlobResourceContents]: A list of content +objects, typically containing either text or binary data. + +**Raises:** +- `RuntimeError`: If called while the client is not connected. + + +#### `list_prompts_mcp` + +```python +list_prompts_mcp(self) -> mcp.types.ListPromptsResult +``` + +Send a prompts/list request and return the complete MCP protocol result. + +**Returns:** +- mcp.types.ListPromptsResult: The complete response object from the protocol, +containing the list of prompts and any additional metadata. + +**Raises:** +- `RuntimeError`: If called while the client is not connected. + + +#### `list_prompts` + +```python +list_prompts(self) -> list[mcp.types.Prompt] +``` + +Retrieve a list of prompts available on the server. + +**Returns:** +- list\[mcp.types.Prompt]: A list of Prompt objects. + +**Raises:** +- `RuntimeError`: If called while the client is not connected. + + +#### `get_prompt_mcp` + +```python +get_prompt_mcp(self, name: str, arguments: dict[str, Any] | None = None) -> mcp.types.GetPromptResult +``` + +Send a prompts/get request and return the complete MCP protocol result. + +**Args:** +- `name`: The name of the prompt to retrieve. +- `arguments`: Arguments to pass to the prompt. Defaults to None. + +**Returns:** +- mcp.types.GetPromptResult: The complete response object from the protocol, +containing the prompt messages and any additional metadata. + +**Raises:** +- `RuntimeError`: If called while the client is not connected. + + +#### `get_prompt` + +```python +get_prompt(self, name: str, arguments: dict[str, Any] | None = None) -> mcp.types.GetPromptResult +``` + +Retrieve a rendered prompt message list from the server. + +**Args:** +- `name`: The name of the prompt to retrieve. +- `arguments`: Arguments to pass to the prompt. Defaults to None. + +**Returns:** +- mcp.types.GetPromptResult: The complete response object from the protocol, +containing the prompt messages and any additional metadata. + +**Raises:** +- `RuntimeError`: If called while the client is not connected. + + +#### `complete_mcp` + +```python +complete_mcp(self, ref: mcp.types.ResourceTemplateReference | mcp.types.PromptReference, argument: dict[str, str], context_arguments: dict[str, Any] | None = None) -> mcp.types.CompleteResult +``` + +Send a completion request and return the complete MCP protocol result. + +**Args:** +- `ref`: The reference to complete. +- `argument`: Arguments to pass to the completion request. +- `context_arguments`: Optional context arguments to +include with the completion request. Defaults to None. + +**Returns:** +- mcp.types.CompleteResult: The complete response object from the protocol, +containing the completion and any additional metadata. + +**Raises:** +- `RuntimeError`: If called while the client is not connected. + + +#### `complete` + +```python +complete(self, ref: mcp.types.ResourceTemplateReference | mcp.types.PromptReference, argument: dict[str, str], context_arguments: dict[str, Any] | None = None) -> mcp.types.Completion +``` + +Send a completion request to the server. + +**Args:** +- `ref`: The reference to complete. +- `argument`: Arguments to pass to the completion request. +- `context_arguments`: Optional context arguments to +include with the completion request. Defaults to None. + +**Returns:** +- mcp.types.Completion: The completion object. + +**Raises:** +- `RuntimeError`: If called while the client is not connected. + + +#### `list_tools_mcp` + +```python +list_tools_mcp(self) -> mcp.types.ListToolsResult +``` + +Send a tools/list request and return the complete MCP protocol result. + +**Returns:** +- mcp.types.ListToolsResult: The complete response object from the protocol, +containing the list of tools and any additional metadata. + +**Raises:** +- `RuntimeError`: If called while the client is not connected. + + +#### `list_tools` + +```python +list_tools(self) -> list[mcp.types.Tool] +``` + +Retrieve a list of tools available on the server. + +**Returns:** +- list\[mcp.types.Tool]: A list of Tool objects. + +**Raises:** +- `RuntimeError`: If called while the client is not connected. + + +#### `call_tool_mcp` + +```python +call_tool_mcp(self, name: str, arguments: dict[str, Any], progress_handler: ProgressHandler | None = None, timeout: datetime.timedelta | float | int | None = None) -> mcp.types.CallToolResult +``` + +Send a tools/call request and return the complete MCP protocol result. + +This method returns the raw CallToolResult object, which includes an isError flag +and other metadata. It does not raise an exception if the tool call results in an error. + +**Args:** +- `name`: The name of the tool to call. +- `arguments`: Arguments to pass to the tool. +- `timeout`: The timeout for the tool call. Defaults to None. +- `progress_handler`: The progress handler to use for the tool call. Defaults to None. + +**Returns:** +- mcp.types.CallToolResult: The complete response object from the protocol, +containing the tool result and any additional metadata. + +**Raises:** +- `RuntimeError`: If called while the client is not connected. + + +#### `call_tool` + +```python +call_tool(self, name: str, arguments: dict[str, Any] | None = None, timeout: datetime.timedelta | float | int | None = None, progress_handler: ProgressHandler | None = None, raise_on_error: bool = True) -> CallToolResult +``` + +Call a tool on the server. + +Unlike call_tool_mcp, this method raises a ToolError if the tool call results in an error. + +**Args:** +- `name`: The name of the tool to call. +- `arguments`: Arguments to pass to the tool. Defaults to None. +- `timeout`: The timeout for the tool call. Defaults to None. +- `progress_handler`: The progress handler to use for the tool call. Defaults to None. + +**Returns:** +- +The content returned by the tool. If the tool returns structured +outputs, they are returned as a dataclass (if an output schema +is available) or a dictionary; otherwise, a list of content +blocks is returned. Note: to receive both structured and +unstructured outputs, use call_tool_mcp instead and access the +raw result object. + +**Raises:** +- `ToolError`: If the tool call results in an error. +- `RuntimeError`: If called while the client is not connected. + + +#### `generate_name` + +```python +generate_name(cls, name: str | None = None) -> str +``` + +### `CallToolResult` diff --git a/docs/python-sdk/fastmcp-client-elicitation.mdx b/docs/python-sdk/fastmcp-client-elicitation.mdx new file mode 100644 index 000000000..838f6314d --- /dev/null +++ b/docs/python-sdk/fastmcp-client-elicitation.mdx @@ -0,0 +1,18 @@ +--- +title: elicitation +sidebarTitle: elicitation +--- + +# `fastmcp.client.elicitation` + +## Functions + +### `create_elicitation_callback` + +```python +create_elicitation_callback(elicitation_handler: ElicitationHandler) -> ElicitationFnT +``` + +## Classes + +### `ElicitResult` diff --git a/docs/python-sdk/fastmcp-client-logging.mdx b/docs/python-sdk/fastmcp-client-logging.mdx new file mode 100644 index 000000000..8d9176ea8 --- /dev/null +++ b/docs/python-sdk/fastmcp-client-logging.mdx @@ -0,0 +1,24 @@ +--- +title: logging +sidebarTitle: logging +--- + +# `fastmcp.client.logging` + +## Functions + +### `default_log_handler` + +```python +default_log_handler(message: LogMessage) -> None +``` + + +Default handler that properly routes server log messages to appropriate log levels. + + +### `create_log_callback` + +```python +create_log_callback(handler: LogHandler | None = None) -> LoggingFnT +``` diff --git a/docs/python-sdk/fastmcp-client-messages.mdx b/docs/python-sdk/fastmcp-client-messages.mdx new file mode 100644 index 000000000..ef1b87436 --- /dev/null +++ b/docs/python-sdk/fastmcp-client-messages.mdx @@ -0,0 +1,107 @@ +--- +title: messages +sidebarTitle: messages +--- + +# `fastmcp.client.messages` + +## Classes + +### `MessageHandler` + + +This class is used to handle MCP messages sent to the client. It is used to handle all messages, +requests, notifications, and exceptions. Users can override any of the hooks + + +**Methods:** + +#### `dispatch` + +```python +dispatch(self, message: Message) -> None +``` + +#### `on_message` + +```python +on_message(self, message: Message) -> None +``` + +#### `on_request` + +```python +on_request(self, message: RequestResponder[mcp.types.ServerRequest, mcp.types.ClientResult]) -> None +``` + +#### `on_ping` + +```python +on_ping(self, message: mcp.types.PingRequest) -> None +``` + +#### `on_list_roots` + +```python +on_list_roots(self, message: mcp.types.ListRootsRequest) -> None +``` + +#### `on_create_message` + +```python +on_create_message(self, message: mcp.types.CreateMessageRequest) -> None +``` + +#### `on_notification` + +```python +on_notification(self, message: mcp.types.ServerNotification) -> None +``` + +#### `on_exception` + +```python +on_exception(self, message: Exception) -> None +``` + +#### `on_progress` + +```python +on_progress(self, message: mcp.types.ProgressNotification) -> None +``` + +#### `on_logging_message` + +```python +on_logging_message(self, message: mcp.types.LoggingMessageNotification) -> None +``` + +#### `on_tool_list_changed` + +```python +on_tool_list_changed(self, message: mcp.types.ToolListChangedNotification) -> None +``` + +#### `on_resource_list_changed` + +```python +on_resource_list_changed(self, message: mcp.types.ResourceListChangedNotification) -> None +``` + +#### `on_prompt_list_changed` + +```python +on_prompt_list_changed(self, message: mcp.types.PromptListChangedNotification) -> None +``` + +#### `on_resource_updated` + +```python +on_resource_updated(self, message: mcp.types.ResourceUpdatedNotification) -> None +``` + +#### `on_cancelled` + +```python +on_cancelled(self, message: mcp.types.CancelledNotification) -> None +``` diff --git a/docs/python-sdk/fastmcp-client-oauth_callback.mdx b/docs/python-sdk/fastmcp-client-oauth_callback.mdx new file mode 100644 index 000000000..beddcd64f --- /dev/null +++ b/docs/python-sdk/fastmcp-client-oauth_callback.mdx @@ -0,0 +1,63 @@ +--- +title: oauth_callback +sidebarTitle: oauth_callback +--- + +# `fastmcp.client.oauth_callback` + + + +OAuth callback server for handling authorization code flows. + +This module provides a reusable callback server that can handle OAuth redirects +and display styled responses to users. + + +## Functions + +### `create_callback_html` + +```python +create_callback_html(message: str, is_success: bool = True, title: str = 'FastMCP OAuth', server_url: str | None = None) -> str +``` + + +Create a styled HTML response for OAuth callbacks. + + +### `create_oauth_callback_server` + +```python +create_oauth_callback_server(port: int, callback_path: str = '/callback', server_url: str | None = None, response_future: asyncio.Future | None = None) -> Server +``` + + +Create an OAuth callback server. + +**Args:** +- `port`: The port to run the server on +- `callback_path`: The path to listen for OAuth redirects on +- `server_url`: Optional server URL to display in success messages +- `response_future`: Optional future to resolve when OAuth callback is received + +**Returns:** +- Configured uvicorn Server instance (not yet running) + + +## Classes + +### `CallbackResponse` + +**Methods:** + +#### `from_dict` + +```python +from_dict(cls, data: dict[str, str]) -> CallbackResponse +``` + +#### `to_dict` + +```python +to_dict(self) -> dict[str, str] +``` diff --git a/docs/python-sdk/fastmcp-client-progress.mdx b/docs/python-sdk/fastmcp-client-progress.mdx new file mode 100644 index 000000000..123839072 --- /dev/null +++ b/docs/python-sdk/fastmcp-client-progress.mdx @@ -0,0 +1,25 @@ +--- +title: progress +sidebarTitle: progress +--- + +# `fastmcp.client.progress` + +## Functions + +### `default_progress_handler` + +```python +default_progress_handler(progress: float, total: float | None, message: str | None) -> None +``` + + +Default handler for progress notifications. + +Logs progress updates at debug level, properly handling missing total or message values. + +**Args:** +- `progress`: Current progress value +- `total`: Optional total expected value +- `message`: Optional status message + diff --git a/docs/python-sdk/fastmcp-client-roots.mdx b/docs/python-sdk/fastmcp-client-roots.mdx new file mode 100644 index 000000000..842c63d5a --- /dev/null +++ b/docs/python-sdk/fastmcp-client-roots.mdx @@ -0,0 +1,20 @@ +--- +title: roots +sidebarTitle: roots +--- + +# `fastmcp.client.roots` + +## Functions + +### `convert_roots_list` + +```python +convert_roots_list(roots: RootsList) -> list[mcp.types.Root] +``` + +### `create_roots_callback` + +```python +create_roots_callback(handler: RootsList | RootsHandler) -> ListRootsFnT +``` diff --git a/docs/python-sdk/fastmcp-client-sampling.mdx b/docs/python-sdk/fastmcp-client-sampling.mdx new file mode 100644 index 000000000..5fa3e70e1 --- /dev/null +++ b/docs/python-sdk/fastmcp-client-sampling.mdx @@ -0,0 +1,14 @@ +--- +title: sampling +sidebarTitle: sampling +--- + +# `fastmcp.client.sampling` + +## Functions + +### `create_sampling_callback` + +```python +create_sampling_callback(sampling_handler: ClientSamplingHandler[LifespanContextT]) -> SamplingFnT +``` diff --git a/docs/python-sdk/fastmcp-client-transports.mdx b/docs/python-sdk/fastmcp-client-transports.mdx new file mode 100644 index 000000000..af31fb50e --- /dev/null +++ b/docs/python-sdk/fastmcp-client-transports.mdx @@ -0,0 +1,300 @@ +--- +title: transports +sidebarTitle: transports +--- + +# `fastmcp.client.transports` + +## Functions + +### `infer_transport` + +```python +infer_transport(transport: ClientTransport | FastMCP | FastMCP1Server | AnyUrl | Path | MCPConfig | dict[str, Any] | str) -> ClientTransport +``` + + +Infer the appropriate transport type from the given transport argument. + +This function attempts to infer the correct transport type from the provided +argument, handling various input types and converting them to the appropriate +ClientTransport subclass. + +The function supports these input types: +- ClientTransport: Used directly without modification +- FastMCP or FastMCP1Server: Creates an in-memory FastMCPTransport +- Path or str (file path): Creates PythonStdioTransport (.py) or NodeStdioTransport (.js) +- AnyUrl or str (URL): Creates StreamableHttpTransport (default) or SSETransport (for /sse endpoints) +- MCPConfig or dict: Creates MCPConfigTransport, potentially connecting to multiple servers + +For HTTP URLs, they are assumed to be Streamable HTTP URLs unless they end in `/sse`. + +For MCPConfig with multiple servers, a composite client is created where each server +is mounted with its name as prefix. This allows accessing tools and resources from multiple +servers through a single unified client interface, using naming patterns like +`servername_toolname` for tools and `protocol://servername/path` for resources. +If the MCPConfig contains only one server, a direct connection is established without prefixing. + +**Examples:** + +```python +# Connect to a local Python script +transport = infer_transport("my_script.py") + +# Connect to a remote server via HTTP +transport = infer_transport("http://example.com/mcp") + +# Connect to multiple servers using MCPConfig +config = { + "mcpServers": { + "weather": {"url": "http://weather.example.com/mcp"}, + "calendar": {"url": "http://calendar.example.com/mcp"} + } +} +transport = infer_transport(config) +``` + + +## Classes + +### `SessionKwargs` + + +Keyword arguments for the MCP ClientSession constructor. + + +### `ClientTransport` + + +Abstract base class for different MCP client transport mechanisms. + +A Transport is responsible for establishing and managing connections +to an MCP server, and providing a ClientSession within an async context. + + +**Methods:** + +#### `connect_session` + +```python +connect_session(self, **session_kwargs: Unpack[SessionKwargs]) -> AsyncIterator[ClientSession] +``` + +Establishes a connection and yields an active ClientSession. + +The ClientSession is *not* expected to be initialized in this context manager. + +The session is guaranteed to be valid only within the scope of the +async context manager. Connection setup and teardown are handled +within this context. + +**Args:** +- `**session_kwargs`: Keyword arguments to pass to the ClientSession + constructor (e.g., callbacks, timeouts). + + +#### `close` + +```python +close(self) +``` + +Close the transport. + + +### `WSTransport` + + +Transport implementation that connects to an MCP server via WebSockets. + + +**Methods:** + +#### `connect_session` + +```python +connect_session(self, **session_kwargs: Unpack[SessionKwargs]) -> AsyncIterator[ClientSession] +``` + +### `SSETransport` + + +Transport implementation that connects to an MCP server via Server-Sent Events. + + +**Methods:** + +#### `connect_session` + +```python +connect_session(self, **session_kwargs: Unpack[SessionKwargs]) -> AsyncIterator[ClientSession] +``` + +### `StreamableHttpTransport` + + +Transport implementation that connects to an MCP server via Streamable HTTP Requests. + + +**Methods:** + +#### `connect_session` + +```python +connect_session(self, **session_kwargs: Unpack[SessionKwargs]) -> AsyncIterator[ClientSession] +``` + +### `StdioTransport` + + +Base transport for connecting to an MCP server via subprocess with stdio. + +This is a base class that can be subclassed for specific command-based +transports like Python, Node, Uvx, etc. + + +**Methods:** + +#### `connect_session` + +```python +connect_session(self, **session_kwargs: Unpack[SessionKwargs]) -> AsyncIterator[ClientSession] +``` + +#### `connect` + +```python +connect(self, **session_kwargs: Unpack[SessionKwargs]) -> ClientSession | None +``` + +#### `disconnect` + +```python +disconnect(self) +``` + +#### `close` + +```python +close(self) +``` + +### `PythonStdioTransport` + + +Transport for running Python scripts. + + +### `FastMCPStdioTransport` + + +Transport for running FastMCP servers using the FastMCP CLI. + + +### `NodeStdioTransport` + + +Transport for running Node.js scripts. + + +### `UvStdioTransport` + + +Transport for running commands via the uv tool. + + +### `UvxStdioTransport` + + +Transport for running commands via the uvx tool. + + +### `NpxStdioTransport` + + +Transport for running commands via the npx tool. + + +### `FastMCPTransport` + + +In-memory transport for FastMCP servers. + +This transport connects directly to a FastMCP server instance in the same +Python process. It works with both FastMCP 2.x servers and FastMCP 1.0 +servers from the low-level MCP SDK. This is particularly useful for unit +tests or scenarios where client and server run in the same runtime. + + +**Methods:** + +#### `connect_session` + +```python +connect_session(self, **session_kwargs: Unpack[SessionKwargs]) -> AsyncIterator[ClientSession] +``` + +### `MCPConfigTransport` + + +Transport for connecting to one or more MCP servers defined in an MCPConfig. + +This transport provides a unified interface to multiple MCP servers defined in an MCPConfig +object or dictionary matching the MCPConfig schema. It supports two key scenarios: + +1. If the MCPConfig contains exactly one server, it creates a direct transport to that server. +2. If the MCPConfig contains multiple servers, it creates a composite client by mounting + all servers on a single FastMCP instance, with each server's name, by default, used as its mounting prefix. + +In the multi-server case, tools are accessible with the prefix pattern `{server_name}_{tool_name}` +and resources with the pattern `protocol://{server_name}/path/to/resource`. + +This is particularly useful for creating clients that need to interact with multiple specialized +MCP servers through a single interface, simplifying client code. + +**Examples:** + +```python +from fastmcp import Client +from fastmcp.utilities.mcp_config import MCPConfig + +# Create a config with multiple servers +config = { + "mcpServers": { + "weather": { + "url": "https://weather-api.example.com/mcp", + "transport": "http" + }, + "calendar": { + "url": "https://calendar-api.example.com/mcp", + "transport": "http" + } + } +} + +# Create a client with the config +client = Client(config) + +async with client: + # Access tools with prefixes + weather = await client.call_tool("weather_get_forecast", {"city": "London"}) + events = await client.call_tool("calendar_list_events", {"date": "2023-06-01"}) + + # Access resources with prefixed URIs + icons = await client.read_resource("weather://weather/icons/sunny") +``` + + +**Methods:** + +#### `connect_session` + +```python +connect_session(self, **session_kwargs: Unpack[SessionKwargs]) -> AsyncIterator[ClientSession] +``` + +#### `close` + +```python +close(self) +``` diff --git a/docs/python-sdk/fastmcp-exceptions.mdx b/docs/python-sdk/fastmcp-exceptions.mdx new file mode 100644 index 000000000..26e62012a --- /dev/null +++ b/docs/python-sdk/fastmcp-exceptions.mdx @@ -0,0 +1,65 @@ +--- +title: exceptions +sidebarTitle: exceptions +--- + +# `fastmcp.exceptions` + + +Custom exceptions for FastMCP. + +## Classes + +### `FastMCPError` + + +Base error for FastMCP. + + +### `ValidationError` + + +Error in validating parameters or return values. + + +### `ResourceError` + + +Error in resource operations. + + +### `ToolError` + + +Error in tool operations. + + +### `PromptError` + + +Error in prompt operations. + + +### `InvalidSignature` + + +Invalid signature for use with FastMCP. + + +### `ClientError` + + +Error in client operations. + + +### `NotFoundError` + + +Object not found. + + +### `DisabledError` + + +Object is disabled. + diff --git a/docs/python-sdk/fastmcp-mcp_config.mdx b/docs/python-sdk/fastmcp-mcp_config.mdx new file mode 100644 index 000000000..805427bb5 --- /dev/null +++ b/docs/python-sdk/fastmcp-mcp_config.mdx @@ -0,0 +1,188 @@ +--- +title: mcp_config +sidebarTitle: mcp_config +--- + +# `fastmcp.mcp_config` + + +Canonical MCP Configuration Format. + +This module defines the standard configuration format for Model Context Protocol (MCP) servers. +It provides a client-agnostic, extensible format that can be used across all MCP implementations. + +The configuration format supports both stdio and remote (HTTP/SSE) transports, with comprehensive +field definitions for server metadata, authentication, and execution parameters. + +Example configuration: +```json +{ + "mcpServers": { + "my-server": { + "command": "npx", + "args": ["-y", "@my/mcp-server"], + "env": {"API_KEY": "secret"}, + "timeout": 30000, + "description": "My MCP server" + } + } +} +``` + + +## Functions + +### `infer_transport_type_from_url` + +```python +infer_transport_type_from_url(url: str | AnyUrl) -> Literal['http', 'sse'] +``` + + +Infer the appropriate transport type from the given URL. + + +### `update_config_file` + +```python +update_config_file(file_path: Path, server_name: str, server_config: CanonicalMCPServerTypes) -> None +``` + + +Update an MCP configuration file from a server object, preserving existing fields. + +This is used for updating the mcpServer configurations of third-party tools so we do not +worry about transforming server objects here. + + +## Classes + +### `StdioMCPServer` + + +MCP server configuration for stdio transport. + +This is the canonical configuration format for MCP servers using stdio transport. + + +**Methods:** + +#### `to_transport` + +```python +to_transport(self) -> StdioTransport +``` + +### `TransformingStdioMCPServer` + + +A Stdio server with tool transforms. + + +### `RemoteMCPServer` + + +MCP server configuration for HTTP/SSE transport. + +This is the canonical configuration format for MCP servers using remote transports. + + +**Methods:** + +#### `to_transport` + +```python +to_transport(self) -> StreamableHttpTransport | SSETransport +``` + +### `TransformingRemoteMCPServer` + + +A Remote server with tool transforms. + + +### `MCPConfig` + + +A configuration object for MCP Servers that conforms to the canonical MCP configuration format +while adding additional fields for enabling FastMCP-specific features like tool transformations +and filtering by tags. + +For an MCPConfig that is strictly canonical, see the `CanonicalMCPConfig` class. + + +**Methods:** + +#### `wrap_servers_at_root` + +```python +wrap_servers_at_root(cls, values: dict[str, Any]) -> dict[str, Any] +``` + +If there's no mcpServers key but there are server configs at root, wrap them. + + +#### `add_server` + +```python +add_server(self, name: str, server: MCPServerTypes) -> None +``` + +Add or update a server in the configuration. + + +#### `from_dict` + +```python +from_dict(cls, config: dict[str, Any]) -> Self +``` + +Parse MCP configuration from dictionary format. + + +#### `to_dict` + +```python +to_dict(self) -> dict[str, Any] +``` + +Convert MCPConfig to dictionary format, preserving all fields. + + +#### `write_to_file` + +```python +write_to_file(self, file_path: Path) -> None +``` + +Write configuration to JSON file. + + +#### `from_file` + +```python +from_file(cls, file_path: Path) -> Self +``` + +Load configuration from JSON file. + + +### `CanonicalMCPConfig` + + +Canonical MCP configuration format. + +This defines the standard configuration format for Model Context Protocol servers. +The format is designed to be client-agnostic and extensible for future use cases. + + +**Methods:** + +#### `add_server` + +```python +add_server(self, name: str, server: CanonicalMCPServerTypes) -> None +``` + +Add or update a server in the configuration. + diff --git a/docs/python-sdk/fastmcp-prompts-__init__.mdx b/docs/python-sdk/fastmcp-prompts-__init__.mdx new file mode 100644 index 000000000..8ef80b59e --- /dev/null +++ b/docs/python-sdk/fastmcp-prompts-__init__.mdx @@ -0,0 +1,8 @@ +--- +title: __init__ +sidebarTitle: __init__ +--- + +# `fastmcp.prompts` + +*This module is empty or contains only private/internal implementations.* diff --git a/docs/python-sdk/fastmcp-prompts-prompt.mdx b/docs/python-sdk/fastmcp-prompts-prompt.mdx new file mode 100644 index 000000000..075ff97e1 --- /dev/null +++ b/docs/python-sdk/fastmcp-prompts-prompt.mdx @@ -0,0 +1,114 @@ +--- +title: prompt +sidebarTitle: prompt +--- + +# `fastmcp.prompts.prompt` + + +Base classes for FastMCP prompts. + +## Functions + +### `Message` + +```python +Message(content: str | ContentBlock, role: Role | None = None, **kwargs: Any) -> PromptMessage +``` + + +A user-friendly constructor for PromptMessage. + + +## Classes + +### `PromptArgument` + + +An argument that can be passed to a prompt. + + +### `Prompt` + + +A prompt template that can be rendered with parameters. + + +**Methods:** + +#### `enable` + +```python +enable(self) -> None +``` + +#### `disable` + +```python +disable(self) -> None +``` + +#### `to_mcp_prompt` + +```python +to_mcp_prompt(self, **overrides: Any) -> MCPPrompt +``` + +Convert the prompt to an MCP prompt. + + +#### `from_function` + +```python +from_function(fn: Callable[..., PromptResult | Awaitable[PromptResult]], name: str | None = None, title: str | None = None, description: str | None = None, tags: set[str] | None = None, enabled: bool | None = None, meta: dict[str, Any] | None = None) -> FunctionPrompt +``` + +Create a Prompt from a function. + +The function can return: +- A string (converted to a message) +- A Message object +- A dict (converted to a message) +- A sequence of any of the above + + +#### `render` + +```python +render(self, arguments: dict[str, Any] | None = None) -> list[PromptMessage] +``` + +Render the prompt with arguments. + + +### `FunctionPrompt` + + +A prompt that is a function. + + +**Methods:** + +#### `from_function` + +```python +from_function(cls, fn: Callable[..., PromptResult | Awaitable[PromptResult]], name: str | None = None, title: str | None = None, description: str | None = None, tags: set[str] | None = None, enabled: bool | None = None, meta: dict[str, Any] | None = None) -> FunctionPrompt +``` + +Create a Prompt from a function. + +The function can return: +- A string (converted to a message) +- A Message object +- A dict (converted to a message) +- A sequence of any of the above + + +#### `render` + +```python +render(self, arguments: dict[str, Any] | None = None) -> list[PromptMessage] +``` + +Render the prompt with arguments. + diff --git a/docs/python-sdk/fastmcp-prompts-prompt_manager.mdx b/docs/python-sdk/fastmcp-prompts-prompt_manager.mdx new file mode 100644 index 000000000..d0fcfae56 --- /dev/null +++ b/docs/python-sdk/fastmcp-prompts-prompt_manager.mdx @@ -0,0 +1,89 @@ +--- +title: prompt_manager +sidebarTitle: prompt_manager +--- + +# `fastmcp.prompts.prompt_manager` + +## Classes + +### `PromptManager` + + +Manages FastMCP prompts. + + +**Methods:** + +#### `mount` + +```python +mount(self, server: MountedServer) -> None +``` + +Adds a mounted server as a source for prompts. + + +#### `has_prompt` + +```python +has_prompt(self, key: str) -> bool +``` + +Check if a prompt exists. + + +#### `get_prompt` + +```python +get_prompt(self, key: str) -> Prompt +``` + +Get prompt by key. + + +#### `get_prompts` + +```python +get_prompts(self) -> dict[str, Prompt] +``` + +Gets the complete, unfiltered inventory of all prompts. + + +#### `list_prompts` + +```python +list_prompts(self) -> list[Prompt] +``` + +Lists all prompts, applying protocol filtering. + + +#### `add_prompt_from_fn` + +```python +add_prompt_from_fn(self, fn: Callable[..., PromptResult | Awaitable[PromptResult]], name: str | None = None, description: str | None = None, tags: set[str] | None = None) -> FunctionPrompt +``` + +Create a prompt from a function. + + +#### `add_prompt` + +```python +add_prompt(self, prompt: Prompt) -> Prompt +``` + +Add a prompt to the manager. + + +#### `render_prompt` + +```python +render_prompt(self, name: str, arguments: dict[str, Any] | None = None) -> GetPromptResult +``` + +Internal API for servers: Finds and renders a prompt, respecting the +filtered protocol path. + diff --git a/docs/python-sdk/fastmcp-resources-__init__.mdx b/docs/python-sdk/fastmcp-resources-__init__.mdx new file mode 100644 index 000000000..cc5fd2786 --- /dev/null +++ b/docs/python-sdk/fastmcp-resources-__init__.mdx @@ -0,0 +1,8 @@ +--- +title: __init__ +sidebarTitle: __init__ +--- + +# `fastmcp.resources` + +*This module is empty or contains only private/internal implementations.* diff --git a/docs/python-sdk/fastmcp-resources-resource.mdx b/docs/python-sdk/fastmcp-resources-resource.mdx new file mode 100644 index 000000000..6acd375af --- /dev/null +++ b/docs/python-sdk/fastmcp-resources-resource.mdx @@ -0,0 +1,120 @@ +--- +title: resource +sidebarTitle: resource +--- + +# `fastmcp.resources.resource` + + +Base classes and interfaces for FastMCP resources. + +## Classes + +### `Resource` + + +Base class for all resources. + + +**Methods:** + +#### `enable` + +```python +enable(self) -> None +``` + +#### `disable` + +```python +disable(self) -> None +``` + +#### `from_function` + +```python +from_function(fn: Callable[..., Any], uri: str | AnyUrl, name: str | None = None, title: str | None = None, description: str | None = None, mime_type: str | None = None, tags: set[str] | None = None, enabled: bool | None = None, annotations: Annotations | None = None, meta: dict[str, Any] | None = None) -> FunctionResource +``` + +#### `set_default_mime_type` + +```python +set_default_mime_type(cls, mime_type: str | None) -> str +``` + +Set default MIME type if not provided. + + +#### `set_default_name` + +```python +set_default_name(self) -> Self +``` + +Set default name from URI if not provided. + + +#### `read` + +```python +read(self) -> str | bytes +``` + +Read the resource content. + + +#### `to_mcp_resource` + +```python +to_mcp_resource(self, **overrides: Any) -> MCPResource +``` + +Convert the resource to an MCPResource. + + +#### `key` + +```python +key(self) -> str +``` + +The key of the component. This is used for internal bookkeeping +and may reflect e.g. prefixes or other identifiers. You should not depend on +keys having a certain value, as the same tool loaded from different +hierarchies of servers may have different keys. + + +### `FunctionResource` + + +A resource that defers data loading by wrapping a function. + +The function is only called when the resource is read, allowing for lazy loading +of potentially expensive data. This is particularly useful when listing resources, +as the function won't be called until the resource is actually accessed. + +The function can return: +- str for text content (default) +- bytes for binary content +- other types will be converted to JSON + + +**Methods:** + +#### `from_function` + +```python +from_function(cls, fn: Callable[..., Any], uri: str | AnyUrl, name: str | None = None, title: str | None = None, description: str | None = None, mime_type: str | None = None, tags: set[str] | None = None, enabled: bool | None = None, annotations: Annotations | None = None, meta: dict[str, Any] | None = None) -> FunctionResource +``` + +Create a FunctionResource from a function. + + +#### `read` + +```python +read(self) -> str | bytes +``` + +Read the resource by calling the wrapped function. + diff --git a/docs/python-sdk/fastmcp-resources-resource_manager.mdx b/docs/python-sdk/fastmcp-resources-resource_manager.mdx new file mode 100644 index 000000000..3a4c7c59c --- /dev/null +++ b/docs/python-sdk/fastmcp-resources-resource_manager.mdx @@ -0,0 +1,181 @@ +--- +title: resource_manager +sidebarTitle: resource_manager +--- + +# `fastmcp.resources.resource_manager` + + +Resource manager functionality. + +## Classes + +### `ResourceManager` + + +Manages FastMCP resources. + + +**Methods:** + +#### `mount` + +```python +mount(self, server: MountedServer) -> None +``` + +Adds a mounted server as a source for resources and templates. + + +#### `get_resources` + +```python +get_resources(self) -> dict[str, Resource] +``` + +Get all registered resources, keyed by URI. + + +#### `get_resource_templates` + +```python +get_resource_templates(self) -> dict[str, ResourceTemplate] +``` + +Get all registered templates, keyed by URI template. + + +#### `list_resources` + +```python +list_resources(self) -> list[Resource] +``` + +Lists all resources, applying protocol filtering. + + +#### `list_resource_templates` + +```python +list_resource_templates(self) -> list[ResourceTemplate] +``` + +Lists all templates, applying protocol filtering. + + +#### `add_resource_or_template_from_fn` + +```python +add_resource_or_template_from_fn(self, fn: Callable[..., Any], uri: str, name: str | None = None, description: str | None = None, mime_type: str | None = None, tags: set[str] | None = None) -> Resource | ResourceTemplate +``` + +Add a resource or template to the manager from a function. + +**Args:** +- `fn`: The function to register as a resource or template +- `uri`: The URI for the resource or template +- `name`: Optional name for the resource or template +- `description`: Optional description of the resource or template +- `mime_type`: Optional MIME type for the resource or template +- `tags`: Optional set of tags for categorizing the resource or template + +**Returns:** +- The added resource or template. If a resource or template with the same URI already exists, +- returns the existing resource or template. + + +#### `add_resource_from_fn` + +```python +add_resource_from_fn(self, fn: Callable[..., Any], uri: str, name: str | None = None, description: str | None = None, mime_type: str | None = None, tags: set[str] | None = None) -> Resource +``` + +Add a resource to the manager from a function. + +**Args:** +- `fn`: The function to register as a resource +- `uri`: The URI for the resource +- `name`: Optional name for the resource +- `description`: Optional description of the resource +- `mime_type`: Optional MIME type for the resource +- `tags`: Optional set of tags for categorizing the resource + +**Returns:** +- The added resource. If a resource with the same URI already exists, +- returns the existing resource. + + +#### `add_resource` + +```python +add_resource(self, resource: Resource) -> Resource +``` + +Add a resource to the manager. + +**Args:** +- `resource`: A Resource instance to add. The resource's .key attribute +will be used as the storage key. To overwrite it, call +Resource.model_copy(key=new_key) before calling this method. + + +#### `add_template_from_fn` + +```python +add_template_from_fn(self, fn: Callable[..., Any], uri_template: str, name: str | None = None, description: str | None = None, mime_type: str | None = None, tags: set[str] | None = None) -> ResourceTemplate +``` + +Create a template from a function. + + +#### `add_template` + +```python +add_template(self, template: ResourceTemplate) -> ResourceTemplate +``` + +Add a template to the manager. + +**Args:** +- `template`: A ResourceTemplate instance to add. The template's .key attribute +will be used as the storage key. To overwrite it, call +ResourceTemplate.model_copy(key=new_key) before calling this method. + +**Returns:** +- The added template. If a template with the same URI already exists, +- returns the existing template. + + +#### `has_resource` + +```python +has_resource(self, uri: AnyUrl | str) -> bool +``` + +Check if a resource exists. + + +#### `get_resource` + +```python +get_resource(self, uri: AnyUrl | str) -> Resource +``` + +Get resource by URI, checking concrete resources first, then templates. + +**Args:** +- `uri`: The URI of the resource to get + +**Raises:** +- `NotFoundError`: If no resource or template matching the URI is found. + + +#### `read_resource` + +```python +read_resource(self, uri: AnyUrl | str) -> str | bytes +``` + +Internal API for servers: Finds and reads a resource, respecting the +filtered protocol path. + diff --git a/docs/python-sdk/fastmcp-resources-template.mdx b/docs/python-sdk/fastmcp-resources-template.mdx new file mode 100644 index 000000000..f67ac5e14 --- /dev/null +++ b/docs/python-sdk/fastmcp-resources-template.mdx @@ -0,0 +1,143 @@ +--- +title: template +sidebarTitle: template +--- + +# `fastmcp.resources.template` + + +Resource template functionality. + +## Functions + +### `build_regex` + +```python +build_regex(template: str) -> re.Pattern +``` + +### `match_uri_template` + +```python +match_uri_template(uri: str, uri_template: str) -> dict[str, str] | None +``` + +## Classes + +### `ResourceTemplate` + + +A template for dynamically creating resources. + + +**Methods:** + +#### `enable` + +```python +enable(self) -> None +``` + +#### `disable` + +```python +disable(self) -> None +``` + +#### `from_function` + +```python +from_function(fn: Callable[..., Any], uri_template: str, name: str | None = None, title: str | None = None, description: str | None = None, mime_type: str | None = None, tags: set[str] | None = None, enabled: bool | None = None, annotations: Annotations | None = None, meta: dict[str, Any] | None = None) -> FunctionResourceTemplate +``` + +#### `set_default_mime_type` + +```python +set_default_mime_type(cls, mime_type: str | None) -> str +``` + +Set default MIME type if not provided. + + +#### `matches` + +```python +matches(self, uri: str) -> dict[str, Any] | None +``` + +Check if URI matches template and extract parameters. + + +#### `read` + +```python +read(self, arguments: dict[str, Any]) -> str | bytes +``` + +Read the resource content. + + +#### `create_resource` + +```python +create_resource(self, uri: str, params: dict[str, Any]) -> Resource +``` + +Create a resource from the template with the given parameters. + + +#### `to_mcp_template` + +```python +to_mcp_template(self, **overrides: Any) -> MCPResourceTemplate +``` + +Convert the resource template to an MCPResourceTemplate. + + +#### `from_mcp_template` + +```python +from_mcp_template(cls, mcp_template: MCPResourceTemplate) -> ResourceTemplate +``` + +Creates a FastMCP ResourceTemplate from a raw MCP ResourceTemplate object. + + +#### `key` + +```python +key(self) -> str +``` + +The key of the component. This is used for internal bookkeeping +and may reflect e.g. prefixes or other identifiers. You should not depend on +keys having a certain value, as the same tool loaded from different +hierarchies of servers may have different keys. + + +### `FunctionResourceTemplate` + + +A template for dynamically creating resources. + + +**Methods:** + +#### `read` + +```python +read(self, arguments: dict[str, Any]) -> str | bytes +``` + +Read the resource content. + + +#### `from_function` + +```python +from_function(cls, fn: Callable[..., Any], uri_template: str, name: str | None = None, title: str | None = None, description: str | None = None, mime_type: str | None = None, tags: set[str] | None = None, enabled: bool | None = None, annotations: Annotations | None = None, meta: dict[str, Any] | None = None) -> FunctionResourceTemplate +``` + +Create a template from a function. + diff --git a/docs/python-sdk/fastmcp-resources-types.mdx b/docs/python-sdk/fastmcp-resources-types.mdx new file mode 100644 index 000000000..cc71f5c6b --- /dev/null +++ b/docs/python-sdk/fastmcp-resources-types.mdx @@ -0,0 +1,134 @@ +--- +title: types +sidebarTitle: types +--- + +# `fastmcp.resources.types` + + +Concrete resource implementations. + +## Classes + +### `TextResource` + + +A resource that reads from a string. + + +**Methods:** + +#### `read` + +```python +read(self) -> str +``` + +Read the text content. + + +### `BinaryResource` + + +A resource that reads from bytes. + + +**Methods:** + +#### `read` + +```python +read(self) -> bytes +``` + +Read the binary content. + + +### `FileResource` + + +A resource that reads from a file. + +Set is_binary=True to read file as binary data instead of text. + + +**Methods:** + +#### `validate_absolute_path` + +```python +validate_absolute_path(cls, path: Path) -> Path +``` + +Ensure path is absolute. + + +#### `set_binary_from_mime_type` + +```python +set_binary_from_mime_type(cls, is_binary: bool, info: ValidationInfo) -> bool +``` + +Set is_binary based on mime_type if not explicitly set. + + +#### `read` + +```python +read(self) -> str | bytes +``` + +Read the file content. + + +### `HttpResource` + + +A resource that reads from an HTTP endpoint. + + +**Methods:** + +#### `read` + +```python +read(self) -> str | bytes +``` + +Read the HTTP content. + + +### `DirectoryResource` + + +A resource that lists files in a directory. + + +**Methods:** + +#### `validate_absolute_path` + +```python +validate_absolute_path(cls, path: Path) -> Path +``` + +Ensure path is absolute. + + +#### `list_files` + +```python +list_files(self) -> list[Path] +``` + +List files in the directory. + + +#### `read` + +```python +read(self) -> str +``` + +Read the directory listing. + diff --git a/docs/python-sdk/fastmcp-server-__init__.mdx b/docs/python-sdk/fastmcp-server-__init__.mdx new file mode 100644 index 000000000..157a018ce --- /dev/null +++ b/docs/python-sdk/fastmcp-server-__init__.mdx @@ -0,0 +1,8 @@ +--- +title: __init__ +sidebarTitle: __init__ +--- + +# `fastmcp.server` + +*This module is empty or contains only private/internal implementations.* diff --git a/docs/python-sdk/fastmcp-server-auth-__init__.mdx b/docs/python-sdk/fastmcp-server-auth-__init__.mdx new file mode 100644 index 000000000..c86f07005 --- /dev/null +++ b/docs/python-sdk/fastmcp-server-auth-__init__.mdx @@ -0,0 +1,8 @@ +--- +title: __init__ +sidebarTitle: __init__ +--- + +# `fastmcp.server.auth` + +*This module is empty or contains only private/internal implementations.* diff --git a/docs/python-sdk/fastmcp-server-auth-auth.mdx b/docs/python-sdk/fastmcp-server-auth-auth.mdx new file mode 100644 index 000000000..d95e116d2 --- /dev/null +++ b/docs/python-sdk/fastmcp-server-auth-auth.mdx @@ -0,0 +1,181 @@ +--- +title: auth +sidebarTitle: auth +--- + +# `fastmcp.server.auth.auth` + +## Classes + +### `AccessToken` + + +AccessToken that includes all JWT claims. + + +### `AuthProvider` + + +Base class for all FastMCP authentication providers. + +This class provides a unified interface for all authentication providers, +whether they are simple token verifiers or full OAuth authorization servers. +All providers must be able to verify tokens and can optionally provide +custom authentication routes. + + +**Methods:** + +#### `verify_token` + +```python +verify_token(self, token: str) -> AccessToken | None +``` + +Verify a bearer token and return access info if valid. + +All auth providers must implement token verification. + +**Args:** +- `token`: The token string to validate + +**Returns:** +- AccessToken object if valid, None if invalid or expired + + +#### `get_routes` + +```python +get_routes(self, mcp_path: str | None = None, mcp_endpoint: Any | None = None) -> list[Route] +``` + +Get the routes for this authentication provider. + +Each provider is responsible for creating whatever routes it needs: +- TokenVerifier: typically no routes (default implementation) +- RemoteAuthProvider: protected resource metadata routes +- OAuthProvider: full OAuth authorization server routes +- Custom providers: whatever routes they need + +**Args:** +- `mcp_path`: The path where the MCP endpoint is mounted (e.g., "/mcp") +- `mcp_endpoint`: The MCP endpoint handler to protect with auth + +**Returns:** +- List of routes for this provider, including protected MCP endpoints if provided + + +#### `get_middleware` + +```python +get_middleware(self) -> list +``` + +Get HTTP application-level middleware for this auth provider. + +**Returns:** +- List of Starlette Middleware instances to apply to the HTTP app + + +### `TokenVerifier` + + +Base class for token verifiers (Resource Servers). + +This class provides token verification capability without OAuth server functionality. +Token verifiers typically don't provide authentication routes by default. + + +**Methods:** + +#### `verify_token` + +```python +verify_token(self, token: str) -> AccessToken | None +``` + +Verify a bearer token and return access info if valid. + + +### `RemoteAuthProvider` + + +Authentication provider for resource servers that verify tokens from known authorization servers. + +This provider composes a TokenVerifier with authorization server metadata to create +standardized OAuth 2.0 Protected Resource endpoints (RFC 9728). Perfect for: +- JWT verification with known issuers +- Remote token introspection services +- Any resource server that knows where its tokens come from + +Use this when you have token verification logic and want to advertise +the authorization servers that issue valid tokens. + + +**Methods:** + +#### `verify_token` + +```python +verify_token(self, token: str) -> AccessToken | None +``` + +Verify token using the configured token verifier. + + +#### `get_routes` + +```python +get_routes(self, mcp_path: str | None = None, mcp_endpoint: Any | None = None) -> list[Route] +``` + +Get OAuth routes for this provider. + +Creates protected resource metadata routes and optionally wraps MCP endpoints with auth. + + +### `OAuthProvider` + + +OAuth Authorization Server provider. + +This class provides full OAuth server functionality including client registration, +authorization flows, token issuance, and token verification. + + +**Methods:** + +#### `verify_token` + +```python +verify_token(self, token: str) -> AccessToken | None +``` + +Verify a bearer token and return access info if valid. + +This method implements the TokenVerifier protocol by delegating +to our existing load_access_token method. + +**Args:** +- `token`: The token string to validate + +**Returns:** +- AccessToken object if valid, None if invalid or expired + + +#### `get_routes` + +```python +get_routes(self, mcp_path: str | None = None, mcp_endpoint: Any | None = None) -> list[Route] +``` + +Get OAuth authorization server routes and optional protected resource routes. + +This method creates the full set of OAuth routes including: +- Standard OAuth authorization server routes (/.well-known/oauth-authorization-server, /authorize, /token, etc.) +- Optional protected resource routes +- Protected MCP endpoints if provided + +**Returns:** +- List of OAuth routes + diff --git a/docs/python-sdk/fastmcp-server-auth-oauth_proxy.mdx b/docs/python-sdk/fastmcp-server-auth-oauth_proxy.mdx new file mode 100644 index 000000000..df2a262ba --- /dev/null +++ b/docs/python-sdk/fastmcp-server-auth-oauth_proxy.mdx @@ -0,0 +1,305 @@ +--- +title: oauth_proxy +sidebarTitle: oauth_proxy +--- + +# `fastmcp.server.auth.oauth_proxy` + + +OAuth Proxy Provider for FastMCP. + +This provider acts as a transparent proxy to an upstream OAuth Authorization Server, +handling Dynamic Client Registration locally while forwarding all other OAuth flows. +This enables authentication with upstream providers that don't support DCR or have +restricted client registration policies. + +Key features: +- Proxies authorization and token endpoints to upstream server +- Implements local Dynamic Client Registration with fixed upstream credentials +- Validates tokens using upstream JWKS +- Maintains minimal local state for bookkeeping +- Enhanced logging with request correlation + +This implementation is based on the OAuth 2.1 specification and is designed for +production use with enterprise identity providers. + + +## Classes + +### `ProxyDCRClient` + + +Client for DCR proxy with configurable redirect URI validation. + +This special client class is critical for the OAuth proxy to work correctly +with Dynamic Client Registration (DCR). Here's why it exists: + +Problem: +-------- +When MCP clients use OAuth, they dynamically register with random localhost +ports (e.g., http://localhost:55454/callback). The OAuth proxy needs to: +1. Accept these dynamic redirect URIs from clients based on configured patterns +2. Use its own fixed redirect URI with the upstream provider (Google, GitHub, etc.) +3. Forward the authorization code back to the client's dynamic URI + +Solution: +--------- +This class validates redirect URIs against configurable patterns, +while the proxy internally uses its own fixed redirect URI with the upstream +provider. This allows the flow to work even when clients reconnect with +different ports or when tokens are cached. + +Without proper validation, clients could get "Redirect URI not registered" errors +when trying to authenticate with cached tokens, or security vulnerabilities could +arise from accepting arbitrary redirect URIs. + + +**Methods:** + +#### `validate_redirect_uri` + +```python +validate_redirect_uri(self, redirect_uri: AnyUrl | None) -> AnyUrl +``` + +Validate redirect URI against allowed patterns. + +Since we're acting as a proxy and clients register dynamically, +we validate their redirect URIs against configurable patterns. +This is essential for cached token scenarios where the client may +reconnect with a different port. + + +### `OAuthProxy` + + +OAuth provider that presents a DCR-compliant interface while proxying to non-DCR IDPs. + +Purpose +------- +MCP clients expect OAuth providers to support Dynamic Client Registration (DCR), +where clients can register themselves dynamically and receive unique credentials. +Most enterprise IDPs (Google, GitHub, Azure AD, etc.) don't support DCR and require +pre-registered OAuth applications with fixed credentials. + +This proxy bridges that gap by: +- Presenting a full DCR-compliant OAuth interface to MCP clients +- Translating DCR registration requests to use pre-configured upstream credentials +- Proxying all OAuth flows to the upstream IDP with appropriate translations +- Managing the state and security requirements of both protocols + +Architecture Overview +-------------------- +The proxy maintains a single OAuth app registration with the upstream provider +while allowing unlimited MCP clients to register and authenticate dynamically. +It implements the complete OAuth 2.1 + DCR specification for clients while +translating to whatever OAuth variant the upstream provider requires. + +Key Translation Challenges Solved +--------------------------------- +1. Dynamic Client Registration: + - MCP clients expect to register dynamically and get unique credentials + - Upstream IDPs require pre-registered apps with fixed credentials + - Solution: Accept DCR requests, return shared upstream credentials + +2. Dynamic Redirect URIs: + - MCP clients use random localhost ports that change between sessions + - Upstream IDPs require fixed, pre-registered redirect URIs + - Solution: Use proxy's fixed callback URL with upstream, forward to client's dynamic URI + +3. Authorization Code Mapping: + - Upstream returns codes for the proxy's redirect URI + - Clients expect codes for their own redirect URIs + - Solution: Exchange upstream code server-side, issue new code to client + +4. State Parameter Collision: + - Both client and proxy need to maintain state through the flow + - Only one state parameter available in OAuth + - Solution: Use transaction ID as state with upstream, preserve client's state + +5. Token Management: + - Clients may expect different token formats/claims than upstream provides + - Need to track tokens for revocation and refresh + - Solution: Store token relationships, forward upstream tokens transparently + +OAuth Flow Implementation +------------------------ +1. Client Registration (DCR): + - Accept any client registration request + - Store ProxyDCRClient that accepts dynamic redirect URIs + +2. Authorization: + - Store transaction mapping client details to proxy flow + - Redirect to upstream with proxy's fixed redirect URI + - Use transaction ID as state parameter with upstream + +3. Upstream Callback: + - Exchange upstream authorization code for tokens (server-side) + - Generate new authorization code bound to client's PKCE challenge + - Redirect to client's original dynamic redirect URI + +4. Token Exchange: + - Validate client's code and PKCE verifier + - Return previously obtained upstream tokens + - Clean up one-time use authorization code + +5. Token Refresh: + - Forward refresh requests to upstream using authlib + - Handle token rotation if upstream issues new refresh token + - Update local token mappings + +State Management +--------------- +The proxy maintains minimal but crucial state: +- _clients: DCR registrations (all use ProxyDCRClient for flexibility) +- _oauth_transactions: Active authorization flows with client context +- _client_codes: Authorization codes with PKCE challenges and upstream tokens +- _access_tokens, _refresh_tokens: Token storage for revocation +- Token relationship mappings for cleanup and rotation + +Security Considerations +---------------------- +- PKCE enforced end-to-end (client to proxy, proxy to upstream) +- Authorization codes are single-use with short expiry +- Transaction IDs are cryptographically random +- All state is cleaned up after use to prevent replay +- Token validation delegates to upstream provider + +Provider Compatibility +--------------------- +Works with any OAuth 2.0 provider that supports: +- Authorization code flow +- Fixed redirect URI (configured in provider's app settings) +- Standard token endpoint + +Handles provider-specific requirements: +- Google: Ensures minimum scope requirements +- GitHub: Compatible with OAuth Apps and GitHub Apps +- Azure AD: Handles tenant-specific endpoints +- Generic: Works with any spec-compliant provider + + +**Methods:** + +#### `get_client` + +```python +get_client(self, client_id: str) -> OAuthClientInformationFull | None +``` + +Get client information by ID. This is generally the random ID +provided to the DCR client during registration, not the upstream client ID. + +For unregistered clients, returns None (which will raise an error in the SDK). + + +#### `register_client` + +```python +register_client(self, client_info: OAuthClientInformationFull) -> None +``` + +Register a client locally + +When a client registers, we create a ProxyDCRClient that is more +forgiving about validating redirect URIs, since the DCR client's +redirect URI will likely be localhost or unknown to the proxied IDP. The +proxied IDP only knows about this server's fixed redirect URI. + + +#### `authorize` + +```python +authorize(self, client: OAuthClientInformationFull, params: AuthorizationParams) -> str +``` + +Start OAuth transaction and redirect to upstream IdP. + +This implements the DCR-compliant proxy pattern: +1. Store transaction with client details and PKCE challenge +2. Generate proxy's own PKCE parameters if forwarding is enabled +3. Use transaction ID as state for IdP +4. Redirect to IdP with our fixed callback URL and proxy's PKCE + + +#### `load_authorization_code` + +```python +load_authorization_code(self, client: OAuthClientInformationFull, authorization_code: str) -> AuthorizationCode | None +``` + +Load authorization code for validation. + +Look up our client code and return authorization code object +with PKCE challenge for validation. + + +#### `exchange_authorization_code` + +```python +exchange_authorization_code(self, client: OAuthClientInformationFull, authorization_code: AuthorizationCode) -> OAuthToken +``` + +Exchange authorization code for stored IdP tokens. + +For the DCR-compliant proxy flow, we return the IdP tokens that were obtained +during the IdP callback exchange. PKCE validation is handled by the MCP framework. + + +#### `load_refresh_token` + +```python +load_refresh_token(self, client: OAuthClientInformationFull, refresh_token: str) -> RefreshToken | None +``` + +Load refresh token from local storage. + + +#### `exchange_refresh_token` + +```python +exchange_refresh_token(self, client: OAuthClientInformationFull, refresh_token: RefreshToken, scopes: list[str]) -> OAuthToken +``` + +Exchange refresh token for new access token using authlib. + + +#### `load_access_token` + +```python +load_access_token(self, token: str) -> AccessToken | None +``` + +Validate access token using upstream JWKS. + +Delegates to the JWT verifier which handles signature validation, +expiration checking, and claims validation using the upstream JWKS. + + +#### `revoke_token` + +```python +revoke_token(self, token: AccessToken | RefreshToken) -> None +``` + +Revoke token locally and with upstream server if supported. + +Removes tokens from local storage and attempts to revoke them with +the upstream server if a revocation endpoint is configured. + + +#### `get_routes` + +```python +get_routes(self, mcp_path: str | None = None, mcp_endpoint: Any | None = None) -> list[Route] +``` + +Get OAuth routes with custom proxy token handler. + +This method creates standard OAuth routes and replaces the token endpoint +with our proxy handler that forwards requests to the upstream OAuth server. + +**Args:** +- `mcp_path`: The path where the MCP endpoint is mounted (e.g., "/mcp") +- `mcp_endpoint`: The MCP endpoint handler to protect with auth + diff --git a/docs/python-sdk/fastmcp-server-auth-oidc_proxy.mdx b/docs/python-sdk/fastmcp-server-auth-oidc_proxy.mdx new file mode 100644 index 000000000..f20b2dcdf --- /dev/null +++ b/docs/python-sdk/fastmcp-server-auth-oidc_proxy.mdx @@ -0,0 +1,82 @@ +--- +title: oidc_proxy +sidebarTitle: oidc_proxy +--- + +# `fastmcp.server.auth.oidc_proxy` + + +OIDC Proxy Provider for FastMCP. + +This provider acts as a transparent proxy to an upstream OIDC compliant Authorization +Server. It leverages the OAuthProxy class to handle Dynamic Client Registration and +forwarding of all OAuth flows. + +This implementation is based on: + OpenID Connect Discovery 1.0 - https://openid.net/specs/openid-connect-discovery-1_0.html + OAuth 2.0 Authorization Server Metadata - https://datatracker.ietf.org/doc/html/rfc8414 + + +## Classes + +### `OIDCConfiguration` + + +OIDC Configuration. + + +**Methods:** + +#### `get_oidc_configuration` + +```python +get_oidc_configuration(cls, config_url: AnyHttpUrl) -> Self +``` + +Get the OIDC configuration for the specified config URL. + +**Args:** +- `config_url`: The OIDC config URL +- `strict`: The strict flag for the configuration +- `timeout_seconds`: HTTP request timeout in seconds + + +### `OIDCProxy` + + +OAuth provider that wraps OAuthProxy to provide configuration via an OIDC configuration URL. + +This provider makes it easier to add OAuth protection for any upstream provider +that is OIDC compliant. + + +**Methods:** + +#### `get_oidc_configuration` + +```python +get_oidc_configuration(self, config_url: AnyHttpUrl, strict: bool | None, timeout_seconds: int | None) -> OIDCConfiguration +``` + +Gets the OIDC configuration for the specified configuration URL. + +**Args:** +- `config_url`: The OIDC configuration URL +- `strict`: The strict flag for the configuration +- `timeout_seconds`: HTTP request timeout in seconds + + +#### `get_token_verifier` + +```python +get_token_verifier(self) -> TokenVerifier +``` + +Creates the token verifier for the specified OIDC configuration and arguments. + +**Args:** +- `algorithm`: Optional token verifier algorithm +- `audience`: Optional token verifier audience +- `required_scopes`: Optional token verifier required_scopes +- `timeout_seconds`: HTTP request timeout in seconds + diff --git a/docs/python-sdk/fastmcp-server-auth-providers-__init__.mdx b/docs/python-sdk/fastmcp-server-auth-providers-__init__.mdx new file mode 100644 index 000000000..9de7cce8a --- /dev/null +++ b/docs/python-sdk/fastmcp-server-auth-providers-__init__.mdx @@ -0,0 +1,8 @@ +--- +title: __init__ +sidebarTitle: __init__ +--- + +# `fastmcp.server.auth.providers` + +*This module is empty or contains only private/internal implementations.* diff --git a/docs/python-sdk/fastmcp-server-auth-providers-auth0.mdx b/docs/python-sdk/fastmcp-server-auth-providers-auth0.mdx new file mode 100644 index 000000000..344f29b64 --- /dev/null +++ b/docs/python-sdk/fastmcp-server-auth-providers-auth0.mdx @@ -0,0 +1,47 @@ +--- +title: auth0 +sidebarTitle: auth0 +--- + +# `fastmcp.server.auth.providers.auth0` + + +Auth0 OAuth provider for FastMCP. + +This module provides a complete Auth0 integration that's ready to use with +just the configuration URL, client ID, client secret, audience, and base URL. + +Example: + ```python + from fastmcp import FastMCP + from fastmcp.server.auth.providers.auth0 import Auth0Provider + + # Simple Auth0 OAuth protection + auth = Auth0Provider( + config_url="https://auth0.config.url", + client_id="your-auth0-client-id", + client_secret="your-auth0-client-secret", + audience="your-auth0-api-audience", + base_url="http://localhost:8000", + ) + + mcp = FastMCP("My Protected Server", auth=auth) + ``` + + +## Classes + +### `Auth0ProviderSettings` + + +Settings for Auth0 OIDC provider. + + +### `Auth0Provider` + + +An Auth0 provider implementation for FastMCP. + +This provider is a complete Auth0 integration that's ready to use with +just the configuration URL, client ID, client secret, audience, and base URL. + diff --git a/docs/python-sdk/fastmcp-server-auth-providers-aws.mdx b/docs/python-sdk/fastmcp-server-auth-providers-aws.mdx new file mode 100644 index 000000000..290951a5a --- /dev/null +++ b/docs/python-sdk/fastmcp-server-auth-providers-aws.mdx @@ -0,0 +1,88 @@ +--- +title: aws +sidebarTitle: aws +--- + +# `fastmcp.server.auth.providers.aws` + + +AWS Cognito OAuth provider for FastMCP. + +This module provides a complete AWS Cognito OAuth integration that's ready to use +with a user pool ID, domain prefix, client ID and client secret. It handles all +the complexity of AWS Cognito's OAuth flow, token validation, and user management. + +Example: + ```python + from fastmcp import FastMCP + from fastmcp.server.auth.providers.aws_cognito import AWSCognitoProvider + + # Simple AWS Cognito OAuth protection + auth = AWSCognitoProvider( + user_pool_id="your-user-pool-id", + aws_region="eu-central-1", + client_id="your-cognito-client-id", + client_secret="your-cognito-client-secret" + ) + + mcp = FastMCP("My Protected Server", auth=auth) + ``` + + +## Classes + +### `AWSCognitoProviderSettings` + + +Settings for AWS Cognito OAuth provider. + + +### `AWSCognitoTokenVerifier` + + +Token verifier that filters claims to Cognito-specific subset. + + +**Methods:** + +#### `verify_token` + +```python +verify_token(self, token: str) -> AccessToken | None +``` + +Verify token and filter claims to Cognito-specific subset. + + +### `AWSCognitoProvider` + + +Complete AWS Cognito OAuth provider for FastMCP. + +This provider makes it trivial to add AWS Cognito OAuth protection to any +FastMCP server using OIDC Discovery. Just provide your Cognito User Pool details, +client credentials, and a base URL, and you're ready to go. + +Features: +- Automatic OIDC Discovery from AWS Cognito User Pool +- Automatic JWT token validation via Cognito's public keys +- Cognito-specific claim filtering (sub, username, cognito:groups) +- Support for Cognito User Pools + + +**Methods:** + +#### `get_token_verifier` + +```python +get_token_verifier(self) -> TokenVerifier +``` + +Creates a Cognito-specific token verifier with claim filtering. + +**Args:** +- `algorithm`: Optional token verifier algorithm +- `audience`: Optional token verifier audience +- `required_scopes`: Optional token verifier required_scopes +- `timeout_seconds`: HTTP request timeout in seconds + diff --git a/docs/python-sdk/fastmcp-server-auth-providers-azure.mdx b/docs/python-sdk/fastmcp-server-auth-providers-azure.mdx new file mode 100644 index 000000000..463e5f731 --- /dev/null +++ b/docs/python-sdk/fastmcp-server-auth-providers-azure.mdx @@ -0,0 +1,63 @@ +--- +title: azure +sidebarTitle: azure +--- + +# `fastmcp.server.auth.providers.azure` + + +Azure (Microsoft Entra) OAuth provider for FastMCP. + +This provider implements Azure/Microsoft Entra ID OAuth authentication +using the OAuth Proxy pattern for non-DCR OAuth flows. + + +## Classes + +### `AzureProviderSettings` + + +Settings for Azure OAuth provider. + + +### `AzureTokenVerifier` + + +Token verifier for Azure OAuth tokens. + +Azure tokens are JWTs, but we verify them by calling the Microsoft Graph API +to get user information and validate the token. + + +**Methods:** + +#### `verify_token` + +```python +verify_token(self, token: str) -> AccessToken | None +``` + +Verify Azure OAuth token by calling Microsoft Graph API. + + +### `AzureProvider` + + +Azure (Microsoft Entra) OAuth provider for FastMCP. + +This provider implements Azure/Microsoft Entra ID authentication using the +OAuth Proxy pattern. It supports both organizational accounts and personal +Microsoft accounts depending on the tenant configuration. + +Features: +- Transparent OAuth proxy to Azure/Microsoft identity platform +- Automatic token validation via Microsoft Graph API +- User information extraction +- Support for different tenant configurations (common, organizations, consumers) + +Setup Requirements: +1. Register an application in Azure Portal (portal.azure.com) +2. Configure redirect URI as: http://localhost:8000/auth/callback +3. Note your Application (client) ID and create a client secret +4. Optionally note your Directory (tenant) ID for single-tenant apps + diff --git a/docs/python-sdk/fastmcp-server-auth-providers-bearer.mdx b/docs/python-sdk/fastmcp-server-auth-providers-bearer.mdx new file mode 100644 index 000000000..3cfbf0239 --- /dev/null +++ b/docs/python-sdk/fastmcp-server-auth-providers-bearer.mdx @@ -0,0 +1,13 @@ +--- +title: bearer +sidebarTitle: bearer +--- + +# `fastmcp.server.auth.providers.bearer` + + +Backwards compatibility shim for BearerAuthProvider. + +The BearerAuthProvider class has been moved to fastmcp.server.auth.providers.jwt.JWTVerifier +for better organization. This module provides a backwards-compatible import. + diff --git a/docs/python-sdk/fastmcp-server-auth-providers-descope.mdx b/docs/python-sdk/fastmcp-server-auth-providers-descope.mdx new file mode 100644 index 000000000..2064a25b1 --- /dev/null +++ b/docs/python-sdk/fastmcp-server-auth-providers-descope.mdx @@ -0,0 +1,62 @@ +--- +title: descope +sidebarTitle: descope +--- + +# `fastmcp.server.auth.providers.descope` + + +Descope authentication provider for FastMCP. + +This module provides DescopeProvider - a complete authentication solution that integrates +with Descope's OAuth 2.1 and OpenID Connect services, supporting Dynamic Client Registration (DCR) +for seamless MCP client authentication. + + +## Classes + +### `DescopeProviderSettings` + +### `DescopeProvider` + + +Descope metadata provider for DCR (Dynamic Client Registration). + +This provider implements Descope integration using metadata forwarding. +This is the recommended approach for Descope DCR +as it allows Descope to handle the OAuth flow directly while FastMCP acts +as a resource server. + +IMPORTANT SETUP REQUIREMENTS: + +1. Enable Dynamic Client Registration in Descope Console: + - Go to the [Inbound Apps page](https://app.descope.com/apps/inbound) of the Descope Console + - Click **DCR Settings** + - Enable **Dynamic Client Registration (DCR)** + - Define allowed scopes + +2. Note your Project ID: + - Save your Project ID from [Project Settings](https://app.descope.com/settings/project) + - Example: P2abc...123 + +For detailed setup instructions, see: +https://docs.descope.com/identity-federation/inbound-apps/creating-inbound-apps#method-2-dynamic-client-registration-dcr + + +**Methods:** + +#### `get_routes` + +```python +get_routes(self, mcp_path: str | None = None, mcp_endpoint: Any | None = None) -> list[Route] +``` + +Get OAuth routes including Descope authorization server metadata forwarding. + +This returns the standard protected resource routes plus an authorization server +metadata endpoint that forwards Descope's OAuth metadata to clients. + +**Args:** +- `mcp_path`: The path where the MCP endpoint is mounted (e.g., "/mcp") +- `mcp_endpoint`: The MCP endpoint handler to protect with auth + diff --git a/docs/python-sdk/fastmcp-server-auth-providers-github.mdx b/docs/python-sdk/fastmcp-server-auth-providers-github.mdx new file mode 100644 index 000000000..e72034495 --- /dev/null +++ b/docs/python-sdk/fastmcp-server-auth-providers-github.mdx @@ -0,0 +1,72 @@ +--- +title: github +sidebarTitle: github +--- + +# `fastmcp.server.auth.providers.github` + + +GitHub OAuth provider for FastMCP. + +This module provides a complete GitHub OAuth integration that's ready to use +with just a client ID and client secret. It handles all the complexity of +GitHub's OAuth flow, token validation, and user management. + +Example: + ```python + from fastmcp import FastMCP + from fastmcp.server.auth.providers.github import GitHubProvider + + # Simple GitHub OAuth protection + auth = GitHubProvider( + client_id="your-github-client-id", + client_secret="your-github-client-secret" + ) + + mcp = FastMCP("My Protected Server", auth=auth) + ``` + + +## Classes + +### `GitHubProviderSettings` + + +Settings for GitHub OAuth provider. + + +### `GitHubTokenVerifier` + + +Token verifier for GitHub OAuth tokens. + +GitHub OAuth tokens are opaque (not JWTs), so we verify them +by calling GitHub's API to check if they're valid and get user info. + + +**Methods:** + +#### `verify_token` + +```python +verify_token(self, token: str) -> AccessToken | None +``` + +Verify GitHub OAuth token by calling GitHub API. + + +### `GitHubProvider` + + +Complete GitHub OAuth provider for FastMCP. + +This provider makes it trivial to add GitHub OAuth protection to any +FastMCP server. Just provide your GitHub OAuth app credentials and +a base URL, and you're ready to go. + +Features: +- Transparent OAuth proxy to GitHub +- Automatic token validation via GitHub API +- User information extraction +- Minimal configuration required + diff --git a/docs/python-sdk/fastmcp-server-auth-providers-google.mdx b/docs/python-sdk/fastmcp-server-auth-providers-google.mdx new file mode 100644 index 000000000..20d3a63e9 --- /dev/null +++ b/docs/python-sdk/fastmcp-server-auth-providers-google.mdx @@ -0,0 +1,72 @@ +--- +title: google +sidebarTitle: google +--- + +# `fastmcp.server.auth.providers.google` + + +Google OAuth provider for FastMCP. + +This module provides a complete Google OAuth integration that's ready to use +with just a client ID and client secret. It handles all the complexity of +Google's OAuth flow, token validation, and user management. + +Example: + ```python + from fastmcp import FastMCP + from fastmcp.server.auth.providers.google import GoogleProvider + + # Simple Google OAuth protection + auth = GoogleProvider( + client_id="your-google-client-id.apps.googleusercontent.com", + client_secret="your-google-client-secret" + ) + + mcp = FastMCP("My Protected Server", auth=auth) + ``` + + +## Classes + +### `GoogleProviderSettings` + + +Settings for Google OAuth provider. + + +### `GoogleTokenVerifier` + + +Token verifier for Google OAuth tokens. + +Google OAuth tokens are opaque (not JWTs), so we verify them +by calling Google's tokeninfo API to check if they're valid and get user info. + + +**Methods:** + +#### `verify_token` + +```python +verify_token(self, token: str) -> AccessToken | None +``` + +Verify Google OAuth token by calling Google's tokeninfo API. + + +### `GoogleProvider` + + +Complete Google OAuth provider for FastMCP. + +This provider makes it trivial to add Google OAuth protection to any +FastMCP server. Just provide your Google OAuth app credentials and +a base URL, and you're ready to go. + +Features: +- Transparent OAuth proxy to Google +- Automatic token validation via Google's tokeninfo API +- User information extraction from Google APIs +- Minimal configuration required + diff --git a/docs/python-sdk/fastmcp-server-auth-providers-in_memory.mdx b/docs/python-sdk/fastmcp-server-auth-providers-in_memory.mdx new file mode 100644 index 000000000..cb97653c8 --- /dev/null +++ b/docs/python-sdk/fastmcp-server-auth-providers-in_memory.mdx @@ -0,0 +1,96 @@ +--- +title: in_memory +sidebarTitle: in_memory +--- + +# `fastmcp.server.auth.providers.in_memory` + +## Classes + +### `InMemoryOAuthProvider` + + +An in-memory OAuth provider for testing purposes. +It simulates the OAuth 2.1 flow locally without external calls. + + +**Methods:** + +#### `get_client` + +```python +get_client(self, client_id: str) -> OAuthClientInformationFull | None +``` + +#### `register_client` + +```python +register_client(self, client_info: OAuthClientInformationFull) -> None +``` + +#### `authorize` + +```python +authorize(self, client: OAuthClientInformationFull, params: AuthorizationParams) -> str +``` + +Simulates user authorization and generates an authorization code. +Returns a redirect URI with the code and state. + + +#### `load_authorization_code` + +```python +load_authorization_code(self, client: OAuthClientInformationFull, authorization_code: str) -> AuthorizationCode | None +``` + +#### `exchange_authorization_code` + +```python +exchange_authorization_code(self, client: OAuthClientInformationFull, authorization_code: AuthorizationCode) -> OAuthToken +``` + +#### `load_refresh_token` + +```python +load_refresh_token(self, client: OAuthClientInformationFull, refresh_token: str) -> RefreshToken | None +``` + +#### `exchange_refresh_token` + +```python +exchange_refresh_token(self, client: OAuthClientInformationFull, refresh_token: RefreshToken, scopes: list[str]) -> OAuthToken +``` + +#### `load_access_token` + +```python +load_access_token(self, token: str) -> AccessToken | None +``` + +#### `verify_token` + +```python +verify_token(self, token: str) -> AccessToken | None +``` + +Verify a bearer token and return access info if valid. + +This method implements the TokenVerifier protocol by delegating +to our existing load_access_token method. + +**Args:** +- `token`: The token string to validate + +**Returns:** +- AccessToken object if valid, None if invalid or expired + + +#### `revoke_token` + +```python +revoke_token(self, token: AccessToken | RefreshToken) -> None +``` + +Revokes an access or refresh token and its counterpart. + diff --git a/docs/python-sdk/fastmcp-server-auth-providers-jwt.mdx b/docs/python-sdk/fastmcp-server-auth-providers-jwt.mdx new file mode 100644 index 000000000..e46059402 --- /dev/null +++ b/docs/python-sdk/fastmcp-server-auth-providers-jwt.mdx @@ -0,0 +1,152 @@ +--- +title: jwt +sidebarTitle: jwt +--- + +# `fastmcp.server.auth.providers.jwt` + + +TokenVerifier implementations for FastMCP. + +## Classes + +### `JWKData` + + +JSON Web Key data structure. + + +### `JWKSData` + + +JSON Web Key Set data structure. + + +### `RSAKeyPair` + + +RSA key pair for JWT testing. + + +**Methods:** + +#### `generate` + +```python +generate(cls) -> RSAKeyPair +``` + +Generate an RSA key pair for testing. + +**Returns:** +- Generated key pair + + +#### `create_token` + +```python +create_token(self, subject: str = 'fastmcp-user', issuer: str = 'https://fastmcp.example.com', audience: str | list[str] | None = None, scopes: list[str] | None = None, expires_in_seconds: int = 3600, additional_claims: dict[str, Any] | None = None, kid: str | None = None) -> str +``` + +Generate a test JWT token for testing purposes. + +**Args:** +- `subject`: Subject claim (usually user ID) +- `issuer`: Issuer claim +- `audience`: Audience claim - can be a string or list of strings (optional) +- `scopes`: List of scopes to include +- `expires_in_seconds`: Token expiration time in seconds +- `additional_claims`: Any additional claims to include +- `kid`: Key ID to include in header + + +### `JWTVerifierSettings` + + +Settings for JWT token verification. + + +### `JWTVerifier` + + +JWT token verifier supporting both asymmetric (RSA/ECDSA) and symmetric (HMAC) algorithms. + +This verifier validates JWT tokens using various signing algorithms: +- **Asymmetric algorithms** (RS256/384/512, ES256/384/512, PS256/384/512): + Uses public/private key pairs. Ideal for external clients and services where + only the authorization server has the private key. +- **Symmetric algorithms** (HS256/384/512): Uses a shared secret for both + signing and verification. Perfect for internal microservices and trusted + environments where the secret can be securely shared. + +Use this when: +- You have JWT tokens issued by an external service (asymmetric) +- You need JWKS support for automatic key rotation (asymmetric) +- You have internal microservices sharing a secret key (symmetric) +- Your tokens contain standard OAuth scopes and claims + + +**Methods:** + +#### `load_access_token` + +```python +load_access_token(self, token: str) -> AccessToken | None +``` + +Validates the provided JWT bearer token. + +**Args:** +- `token`: The JWT token string to validate + +**Returns:** +- AccessToken object if valid, None if invalid or expired + + +#### `verify_token` + +```python +verify_token(self, token: str) -> AccessToken | None +``` + +Verify a bearer token and return access info if valid. + +This method implements the TokenVerifier protocol by delegating +to our existing load_access_token method. + +**Args:** +- `token`: The JWT token string to validate + +**Returns:** +- AccessToken object if valid, None if invalid or expired + + +### `StaticTokenVerifier` + + +Simple static token verifier for testing and development. + +This verifier validates tokens against a predefined dictionary of valid token +strings and their associated claims. When a token string matches a key in the +dictionary, the verifier returns the corresponding claims as if the token was +validated by a real authorization server. + +Use this when: +- You're developing or testing locally without a real OAuth server +- You need predictable tokens for automated testing +- You want to simulate different users/scopes without complex setup +- You're prototyping and need simple API key-style authentication + +WARNING: Never use this in production - tokens are stored in plain text! + + +**Methods:** + +#### `verify_token` + +```python +verify_token(self, token: str) -> AccessToken | None +``` + +Verify token against static token dictionary. + diff --git a/docs/python-sdk/fastmcp-server-auth-providers-scalekit.mdx b/docs/python-sdk/fastmcp-server-auth-providers-scalekit.mdx new file mode 100644 index 000000000..f1b9a5df0 --- /dev/null +++ b/docs/python-sdk/fastmcp-server-auth-providers-scalekit.mdx @@ -0,0 +1,64 @@ +--- +title: scalekit +sidebarTitle: scalekit +--- + +# `fastmcp.server.auth.providers.scalekit` + + +Scalekit authentication provider for FastMCP. + +This module provides ScalekitProvider - a complete authentication solution that integrates +with Scalekit's OAuth 2.1 and OpenID Connect services, supporting Resource Server +authentication for seamless MCP client authentication. + + +## Classes + +### `ScalekitProviderSettings` + +### `ScalekitProvider` + + +Scalekit resource server provider for OAuth 2.1 authentication. + +This provider implements Scalekit integration using resource server pattern. +FastMCP acts as a protected resource server that validates access tokens issued +by Scalekit's authorization server. + +IMPORTANT SETUP REQUIREMENTS: + +1. Create an MCP Server in Scalekit Dashboard: + - Go to your [Scalekit Dashboard](https://app.scalekit.com/) + - Navigate to MCP Servers section + - Register a new MCP Server with appropriate scopes + - Ensure the Resource Identifier matches exactly what you configure as MCP URL + - Note the Resource ID + +2. Environment Configuration: + - Set SCALEKIT_ENVIRONMENT_URL (e.g., https://your-env.scalekit.com) + - Set SCALEKIT_CLIENT_ID from your OAuth application + - Set SCALEKIT_RESOURCE_ID from your created resource + - Set MCP_URL to your FastMCP server's public URL + +For detailed setup instructions, see: +https://docs.scalekit.com/mcp/overview/ + + +**Methods:** + +#### `get_routes` + +```python +get_routes(self, mcp_path: str | None = None, mcp_endpoint: Any | None = None) -> list[Route] +``` + +Get OAuth routes including Scalekit authorization server metadata forwarding. + +This returns the standard protected resource routes plus an authorization server +metadata endpoint that forwards Scalekit's OAuth metadata to clients. + +**Args:** +- `mcp_path`: The path where the MCP endpoint is mounted (e.g., "/mcp") +- `mcp_endpoint`: The MCP endpoint handler to protect with auth + diff --git a/docs/python-sdk/fastmcp-server-auth-providers-workos.mdx b/docs/python-sdk/fastmcp-server-auth-providers-workos.mdx new file mode 100644 index 000000000..a4ac73e02 --- /dev/null +++ b/docs/python-sdk/fastmcp-server-auth-providers-workos.mdx @@ -0,0 +1,110 @@ +--- +title: workos +sidebarTitle: workos +--- + +# `fastmcp.server.auth.providers.workos` + + +WorkOS authentication providers for FastMCP. + +This module provides two WorkOS authentication strategies: + +1. WorkOSProvider - OAuth proxy for WorkOS Connect applications (non-DCR) +2. AuthKitProvider - DCR-compliant provider for WorkOS AuthKit + +Choose based on your WorkOS setup and authentication requirements. + + +## Classes + +### `WorkOSProviderSettings` + + +Settings for WorkOS OAuth provider. + + +### `WorkOSTokenVerifier` + + +Token verifier for WorkOS OAuth tokens. + +WorkOS AuthKit tokens are opaque, so we verify them by calling +the /oauth2/userinfo endpoint to check validity and get user info. + + +**Methods:** + +#### `verify_token` + +```python +verify_token(self, token: str) -> AccessToken | None +``` + +Verify WorkOS OAuth token by calling userinfo endpoint. + + +### `WorkOSProvider` + + +Complete WorkOS OAuth provider for FastMCP. + +This provider implements WorkOS AuthKit OAuth using the OAuth Proxy pattern. +It provides OAuth2 authentication for users through WorkOS Connect applications. + +Features: +- Transparent OAuth proxy to WorkOS AuthKit +- Automatic token validation via userinfo endpoint +- User information extraction from ID tokens +- Support for standard OAuth scopes (openid, profile, email) + +Setup Requirements: +1. Create a WorkOS Connect application in your dashboard +2. Note your AuthKit domain (e.g., "https://your-app.authkit.app") +3. Configure redirect URI as: http://localhost:8000/auth/callback +4. Note your Client ID and Client Secret + + +### `AuthKitProviderSettings` + +### `AuthKitProvider` + + +AuthKit metadata provider for DCR (Dynamic Client Registration). + +This provider implements AuthKit integration using metadata forwarding +instead of OAuth proxying. This is the recommended approach for WorkOS DCR +as it allows WorkOS to handle the OAuth flow directly while FastMCP acts +as a resource server. + +IMPORTANT SETUP REQUIREMENTS: + +1. Enable Dynamic Client Registration in WorkOS Dashboard: + - Go to Applications โ†’ Configuration + - Toggle "Dynamic Client Registration" to enabled + +2. Configure your FastMCP server URL as a callback: + - Add your server URL to the Redirects tab in WorkOS dashboard + - Example: https://your-fastmcp-server.com/oauth2/callback + +For detailed setup instructions, see: +https://workos.com/docs/authkit/mcp/integrating/token-verification + + +**Methods:** + +#### `get_routes` + +```python +get_routes(self, mcp_path: str | None = None, mcp_endpoint: Any | None = None) -> list[Route] +``` + +Get OAuth routes including AuthKit authorization server metadata forwarding. + +This returns the standard protected resource routes plus an authorization server +metadata endpoint that forwards AuthKit's OAuth metadata to clients. + +**Args:** +- `mcp_path`: The path where the MCP endpoint is mounted (e.g., "/mcp") +- `mcp_endpoint`: The MCP endpoint handler to protect with auth + diff --git a/docs/python-sdk/fastmcp-server-auth-redirect_validation.mdx b/docs/python-sdk/fastmcp-server-auth-redirect_validation.mdx new file mode 100644 index 000000000..c7aa26786 --- /dev/null +++ b/docs/python-sdk/fastmcp-server-auth-redirect_validation.mdx @@ -0,0 +1,53 @@ +--- +title: redirect_validation +sidebarTitle: redirect_validation +--- + +# `fastmcp.server.auth.redirect_validation` + + +Utilities for validating client redirect URIs in OAuth flows. + +## Functions + +### `matches_allowed_pattern` + +```python +matches_allowed_pattern(uri: str, pattern: str) -> bool +``` + + +Check if a URI matches an allowed pattern with wildcard support. + +Patterns support * wildcard matching: +- http://localhost:* matches any localhost port +- http://127.0.0.1:* matches any 127.0.0.1 port +- https://*.example.com/* matches any subdomain of example.com +- https://app.example.com/auth/* matches any path under /auth/ + +**Args:** +- `uri`: The redirect URI to validate +- `pattern`: The allowed pattern (may contain wildcards) + +**Returns:** +- True if the URI matches the pattern + + +### `validate_redirect_uri` + +```python +validate_redirect_uri(redirect_uri: str | AnyUrl | None, allowed_patterns: list[str] | None) -> bool +``` + + +Validate a redirect URI against allowed patterns. + +**Args:** +- `redirect_uri`: The redirect URI to validate +- `allowed_patterns`: List of allowed patterns. If None, all URIs are allowed (for DCR compatibility). + If empty list, no URIs are allowed. + To restrict to localhost only, explicitly pass DEFAULT_LOCALHOST_PATTERNS. + +**Returns:** +- True if the redirect URI is allowed + diff --git a/docs/python-sdk/fastmcp-server-context.mdx b/docs/python-sdk/fastmcp-server-context.mdx new file mode 100644 index 000000000..f828d513f --- /dev/null +++ b/docs/python-sdk/fastmcp-server-context.mdx @@ -0,0 +1,340 @@ +--- +title: context +sidebarTitle: context +--- + +# `fastmcp.server.context` + +## Functions + +### `set_context` + +```python +set_context(context: Context) -> Generator[Context, None, None] +``` + +## Classes + +### `LogData` + + +Data object for passing log arguments to client-side handlers. + +This provides an interface to match the Python standard library logging, +for compatibility with structured logging. + + +### `Context` + + +Context object providing access to MCP capabilities. + +This provides a cleaner interface to MCP's RequestContext functionality. +It gets injected into tool and resource functions that request it via type hints. + +To use context in a tool function, add a parameter with the Context type annotation: + +```python +@server.tool +async def my_tool(x: int, ctx: Context) -> str: + # Log messages to the client + await ctx.info(f"Processing {x}") + await ctx.debug("Debug info") + await ctx.warning("Warning message") + await ctx.error("Error message") + + # Report progress + await ctx.report_progress(50, 100, "Processing") + + # Access resources + data = await ctx.read_resource("resource://data") + + # Get request info + request_id = ctx.request_id + client_id = ctx.client_id + + # Manage state across the request + ctx.set_state("key", "value") + value = ctx.get_state("key") + + return str(x) +``` + +State Management: +Context objects maintain a state dictionary that can be used to store and share +data across middleware and tool calls within a request. When a new context +is created (nested contexts), it inherits a copy of its parent's state, ensuring +that modifications in child contexts don't affect parent contexts. + +The context parameter name can be anything as long as it's annotated with Context. +The context is optional - tools that don't need it can omit the parameter. + + +**Methods:** + +#### `fastmcp` + +```python +fastmcp(self) -> FastMCP +``` + +Get the FastMCP instance. + + +#### `request_context` + +```python +request_context(self) -> RequestContext[ServerSession, Any, Request] +``` + +Access to the underlying request context. + +If called outside of a request context, this will raise a ValueError. + + +#### `report_progress` + +```python +report_progress(self, progress: float, total: float | None = None, message: str | None = None) -> None +``` + +Report progress for the current operation. + +**Args:** +- `progress`: Current progress value e.g. 24 +- `total`: Optional total value e.g. 100 + + +#### `read_resource` + +```python +read_resource(self, uri: str | AnyUrl) -> list[ReadResourceContents] +``` + +Read a resource by URI. + +**Args:** +- `uri`: Resource URI to read + +**Returns:** +- The resource content as either text or bytes + + +#### `log` + +```python +log(self, message: str, level: LoggingLevel | None = None, logger_name: str | None = None, extra: Mapping[str, Any] | None = None) -> None +``` + +Send a log message to the client. + +**Args:** +- `message`: Log message +- `level`: Optional log level. One of "debug", "info", "notice", "warning", "error", "critical", +"alert", or "emergency". Default is "info". +- `logger_name`: Optional logger name +- `extra`: Optional mapping for additional arguments + + +#### `client_id` + +```python +client_id(self) -> str | None +``` + +Get the client ID if available. + + +#### `request_id` + +```python +request_id(self) -> str +``` + +Get the unique ID for this request. + + +#### `session_id` + +```python +session_id(self) -> str +``` + +Get the MCP session ID for ALL transports. + +Returns the session ID that can be used as a key for session-based +data storage (e.g., Redis) to share data between tool calls within +the same client session. + +**Returns:** +- The session ID for StreamableHTTP transports, or a generated ID +- for other transports. + + +#### `session` + +```python +session(self) -> ServerSession +``` + +Access to the underlying session for advanced usage. + + +#### `debug` + +```python +debug(self, message: str, logger_name: str | None = None, extra: Mapping[str, Any] | None = None) -> None +``` + +Send a debug log message. + + +#### `info` + +```python +info(self, message: str, logger_name: str | None = None, extra: Mapping[str, Any] | None = None) -> None +``` + +Send an info log message. + + +#### `warning` + +```python +warning(self, message: str, logger_name: str | None = None, extra: Mapping[str, Any] | None = None) -> None +``` + +Send a warning log message. + + +#### `error` + +```python +error(self, message: str, logger_name: str | None = None, extra: Mapping[str, Any] | None = None) -> None +``` + +Send an error log message. + + +#### `list_roots` + +```python +list_roots(self) -> list[Root] +``` + +List the roots available to the server, as indicated by the client. + + +#### `send_tool_list_changed` + +```python +send_tool_list_changed(self) -> None +``` + +Send a tool list changed notification to the client. + + +#### `send_resource_list_changed` + +```python +send_resource_list_changed(self) -> None +``` + +Send a resource list changed notification to the client. + + +#### `send_prompt_list_changed` + +```python +send_prompt_list_changed(self) -> None +``` + +Send a prompt list changed notification to the client. + + +#### `sample` + +```python +sample(self, messages: str | Sequence[str | SamplingMessage], system_prompt: str | None = None, include_context: IncludeContext | None = None, temperature: float | None = None, max_tokens: int | None = None, model_preferences: ModelPreferences | str | list[str] | None = None) -> TextContent | ImageContent | AudioContent +``` + +Send a sampling request to the client and await the response. + +Call this method at any time to have the server request an LLM +completion from the client. The client must be appropriately configured, +or the request will error. + + +#### `elicit` + +```python +elicit(self, message: str, response_type: None) -> AcceptedElicitation[dict[str, Any]] | DeclinedElicitation | CancelledElicitation +``` + +#### `elicit` + +```python +elicit(self, message: str, response_type: type[T]) -> AcceptedElicitation[T] | DeclinedElicitation | CancelledElicitation +``` + +#### `elicit` + +```python +elicit(self, message: str, response_type: list[str]) -> AcceptedElicitation[str] | DeclinedElicitation | CancelledElicitation +``` + +#### `elicit` + +```python +elicit(self, message: str, response_type: type[T] | list[str] | None = None) -> AcceptedElicitation[T] | AcceptedElicitation[dict[str, Any]] | AcceptedElicitation[str] | DeclinedElicitation | CancelledElicitation +``` + +Send an elicitation request to the client and await the response. + +Call this method at any time to request additional information from +the user through the client. The client must support elicitation, +or the request will error. + +Note that the MCP protocol only supports simple object schemas with +primitive types. You can provide a dataclass, TypedDict, or BaseModel to +comply. If you provide a primitive type, an object schema with a single +"value" field will be generated for the MCP interaction and +automatically deconstructed into the primitive type upon response. + +If the response_type is None, the generated schema will be that of an +empty object in order to comply with the MCP protocol requirements. +Clients must send an empty object ("{}")in response. + +**Args:** +- `message`: A human-readable message explaining what information is needed +- `response_type`: The type of the response, which should be a primitive +type or dataclass or BaseModel. If it is a primitive type, an +object schema with a single "value" field will be generated. + + +#### `get_http_request` + +```python +get_http_request(self) -> Request +``` + +Get the active starlette request. + + +#### `set_state` + +```python +set_state(self, key: str, value: Any) -> None +``` + +Set a value in the context state. + + +#### `get_state` + +```python +get_state(self, key: str) -> Any +``` + +Get a value from the context state. Returns None if the key is not found. + diff --git a/docs/python-sdk/fastmcp-server-dependencies.mdx b/docs/python-sdk/fastmcp-server-dependencies.mdx new file mode 100644 index 000000000..d9a6d6bed --- /dev/null +++ b/docs/python-sdk/fastmcp-server-dependencies.mdx @@ -0,0 +1,49 @@ +--- +title: dependencies +sidebarTitle: dependencies +--- + +# `fastmcp.server.dependencies` + +## Functions + +### `get_context` + +```python +get_context() -> Context +``` + +### `get_http_request` + +```python +get_http_request() -> Request +``` + +### `get_http_headers` + +```python +get_http_headers(include_all: bool = False) -> dict[str, str] +``` + + +Extract headers from the current HTTP request if available. + +Never raises an exception, even if there is no active HTTP request (in which case +an empty dict is returned). + +By default, strips problematic headers like `content-length` that cause issues if forwarded to downstream clients. +If `include_all` is True, all headers are returned. + + +### `get_access_token` + +```python +get_access_token() -> AccessToken | None +``` + + +Get the FastMCP access token from the current context. + +**Returns:** +- The access token if an authenticated user is available, None otherwise. + diff --git a/docs/python-sdk/fastmcp-server-elicitation.mdx b/docs/python-sdk/fastmcp-server-elicitation.mdx new file mode 100644 index 000000000..c21fc533d --- /dev/null +++ b/docs/python-sdk/fastmcp-server-elicitation.mdx @@ -0,0 +1,87 @@ +--- +title: elicitation +sidebarTitle: elicitation +--- + +# `fastmcp.server.elicitation` + +## Functions + +### `get_elicitation_schema` + +```python +get_elicitation_schema(response_type: type[T]) -> dict[str, Any] +``` + + +Get the schema for an elicitation response. + +**Args:** +- `response_type`: The type of the response + + +### `validate_elicitation_json_schema` + +```python +validate_elicitation_json_schema(schema: dict[str, Any]) -> None +``` + + +Validate that a JSON schema follows MCP elicitation requirements. + +This ensures the schema is compatible with MCP elicitation requirements: +- Must be an object schema +- Must only contain primitive field types (string, number, integer, boolean) +- Must be flat (no nested objects or arrays of objects) +- Allows const fields (for Literal types) and enum fields (for Enum types) +- Only primitive types and their nullable variants are allowed + +**Args:** +- `schema`: The JSON schema to validate + +**Raises:** +- `TypeError`: If the schema doesn't meet MCP elicitation requirements + + +## Classes + +### `ElicitationJsonSchema` + + +Custom JSON schema generator for MCP elicitation that always inlines enums. + +MCP elicitation requires inline enum schemas without $ref/$defs references. +This generator ensures enums are always generated inline for compatibility. +Optionally adds enumNames for better UI display when available. + + +**Methods:** + +#### `generate_inner` + +```python +generate_inner(self, schema: core_schema.CoreSchema) -> JsonSchemaValue +``` + +Override to prevent ref generation for enums. + + +#### `enum_schema` + +```python +enum_schema(self, schema: core_schema.EnumSchema) -> JsonSchemaValue +``` + +Generate inline enum schema with optional enumNames for better UI. + +If enum members have a _display_name_ attribute or custom __str__, +we'll include enumNames for better UI representation. + + +### `AcceptedElicitation` + + +Result when user accepts the elicitation. + + +### `ScalarElicitationType` diff --git a/docs/python-sdk/fastmcp-server-http.mdx b/docs/python-sdk/fastmcp-server-http.mdx new file mode 100644 index 000000000..0a7ee46ca --- /dev/null +++ b/docs/python-sdk/fastmcp-server-http.mdx @@ -0,0 +1,103 @@ +--- +title: http +sidebarTitle: http +--- + +# `fastmcp.server.http` + +## Functions + +### `set_http_request` + +```python +set_http_request(request: Request) -> Generator[Request, None, None] +``` + +### `create_base_app` + +```python +create_base_app(routes: list[BaseRoute], middleware: list[Middleware], debug: bool = False, lifespan: Callable | None = None) -> StarletteWithLifespan +``` + + +Create a base Starlette app with common middleware and routes. + +**Args:** +- `routes`: List of routes to include in the app +- `middleware`: List of middleware to include in the app +- `debug`: Whether to enable debug mode +- `lifespan`: Optional lifespan manager for the app + +**Returns:** +- A Starlette application + + +### `create_sse_app` + +```python +create_sse_app(server: FastMCP[LifespanResultT], message_path: str, sse_path: str, auth: AuthProvider | None = None, debug: bool = False, routes: list[BaseRoute] | None = None, middleware: list[Middleware] | None = None) -> StarletteWithLifespan +``` + + +Return an instance of the SSE server app. + +**Args:** +- `server`: The FastMCP server instance +- `message_path`: Path for SSE messages +- `sse_path`: Path for SSE connections +- `auth`: Optional authentication provider (AuthProvider) +- `debug`: Whether to enable debug mode +- `routes`: Optional list of custom routes +- `middleware`: Optional list of middleware + +Returns: + A Starlette application with RequestContextMiddleware + + +### `create_streamable_http_app` + +```python +create_streamable_http_app(server: FastMCP[LifespanResultT], streamable_http_path: str, event_store: EventStore | None = None, auth: AuthProvider | None = None, json_response: bool = False, stateless_http: bool = False, debug: bool = False, routes: list[BaseRoute] | None = None, middleware: list[Middleware] | None = None) -> StarletteWithLifespan +``` + + +Return an instance of the StreamableHTTP server app. + +**Args:** +- `server`: The FastMCP server instance +- `streamable_http_path`: Path for StreamableHTTP connections +- `event_store`: Optional event store for session management +- `auth`: Optional authentication provider (AuthProvider) +- `json_response`: Whether to use JSON response format +- `stateless_http`: Whether to use stateless mode (new transport per request) +- `debug`: Whether to enable debug mode +- `routes`: Optional list of custom routes +- `middleware`: Optional list of middleware + +**Returns:** +- A Starlette application with StreamableHTTP support + + +## Classes + +### `StreamableHTTPASGIApp` + + +ASGI application wrapper for Streamable HTTP server transport. + + +### `StarletteWithLifespan` + +**Methods:** + +#### `lifespan` + +```python +lifespan(self) -> Lifespan[Starlette] +``` + +### `RequestContextMiddleware` + + +Middleware that stores each request in a ContextVar + diff --git a/docs/python-sdk/fastmcp-server-low_level.mdx b/docs/python-sdk/fastmcp-server-low_level.mdx new file mode 100644 index 000000000..2f7197358 --- /dev/null +++ b/docs/python-sdk/fastmcp-server-low_level.mdx @@ -0,0 +1,18 @@ +--- +title: low_level +sidebarTitle: low_level +--- + +# `fastmcp.server.low_level` + +## Classes + +### `LowLevelServer` + +**Methods:** + +#### `create_initialization_options` + +```python +create_initialization_options(self, notification_options: NotificationOptions | None = None, experimental_capabilities: dict[str, dict[str, Any]] | None = None, **kwargs: Any) -> InitializationOptions +``` diff --git a/docs/python-sdk/fastmcp-server-middleware-__init__.mdx b/docs/python-sdk/fastmcp-server-middleware-__init__.mdx new file mode 100644 index 000000000..8583b1df9 --- /dev/null +++ b/docs/python-sdk/fastmcp-server-middleware-__init__.mdx @@ -0,0 +1,8 @@ +--- +title: __init__ +sidebarTitle: __init__ +--- + +# `fastmcp.server.middleware` + +*This module is empty or contains only private/internal implementations.* diff --git a/docs/python-sdk/fastmcp-server-middleware-error_handling.mdx b/docs/python-sdk/fastmcp-server-middleware-error_handling.mdx new file mode 100644 index 000000000..842c31ce0 --- /dev/null +++ b/docs/python-sdk/fastmcp-server-middleware-error_handling.mdx @@ -0,0 +1,60 @@ +--- +title: error_handling +sidebarTitle: error_handling +--- + +# `fastmcp.server.middleware.error_handling` + + +Error handling middleware for consistent error responses and tracking. + +## Classes + +### `ErrorHandlingMiddleware` + + +Middleware that provides consistent error handling and logging. + +Catches exceptions, logs them appropriately, and converts them to +proper MCP error responses. Also tracks error patterns for monitoring. + + +**Methods:** + +#### `on_message` + +```python +on_message(self, context: MiddlewareContext, call_next: CallNext) -> Any +``` + +Handle errors for all messages. + + +#### `get_error_stats` + +```python +get_error_stats(self) -> dict[str, int] +``` + +Get error statistics for monitoring. + + +### `RetryMiddleware` + + +Middleware that implements automatic retry logic for failed requests. + +Retries requests that fail with transient errors, using exponential +backoff to avoid overwhelming the server or external dependencies. + + +**Methods:** + +#### `on_request` + +```python +on_request(self, context: MiddlewareContext, call_next: CallNext) -> Any +``` + +Implement retry logic for requests. + diff --git a/docs/python-sdk/fastmcp-server-middleware-logging.mdx b/docs/python-sdk/fastmcp-server-middleware-logging.mdx new file mode 100644 index 000000000..fd6ee041c --- /dev/null +++ b/docs/python-sdk/fastmcp-server-middleware-logging.mdx @@ -0,0 +1,58 @@ +--- +title: logging +sidebarTitle: logging +--- + +# `fastmcp.server.middleware.logging` + + +Comprehensive logging middleware for FastMCP servers. + +## Functions + +### `default_serializer` + +```python +default_serializer(data: Any) -> str +``` + + +The default serializer for Payloads in the logging middleware. + + +## Classes + +### `BaseLoggingMiddleware` + + +Base class for logging middleware. + + +**Methods:** + +#### `on_message` + +```python +on_message(self, context: MiddlewareContext[Any], call_next: CallNext[Any, Any]) -> Any +``` + +Log all messages. + + +### `LoggingMiddleware` + + +Middleware that provides comprehensive request and response logging. + +Logs all MCP messages with configurable detail levels. Useful for debugging, +monitoring, and understanding server usage patterns. + + +### `StructuredLoggingMiddleware` + + +Middleware that provides structured JSON logging for better log analysis. + +Outputs structured logs that are easier to parse and analyze with log +aggregation tools like ELK stack, Splunk, or cloud logging services. + diff --git a/docs/python-sdk/fastmcp-server-middleware-middleware.mdx b/docs/python-sdk/fastmcp-server-middleware-middleware.mdx new file mode 100644 index 000000000..a530af4b1 --- /dev/null +++ b/docs/python-sdk/fastmcp-server-middleware-middleware.mdx @@ -0,0 +1,106 @@ +--- +title: middleware +sidebarTitle: middleware +--- + +# `fastmcp.server.middleware.middleware` + +## Functions + +### `make_middleware_wrapper` + +```python +make_middleware_wrapper(middleware: Middleware, call_next: CallNext[T, R]) -> CallNext[T, R] +``` + + +Create a wrapper that applies a single middleware to a context. The +closure bakes in the middleware and call_next function, so it can be +passed to other functions that expect a call_next function. + + +## Classes + +### `CallNext` + +### `MiddlewareContext` + + +Unified context for all middleware operations. + + +**Methods:** + +#### `copy` + +```python +copy(self, **kwargs: Any) -> MiddlewareContext[T] +``` + +### `Middleware` + + +Base class for FastMCP middleware with dispatching hooks. + + +**Methods:** + +#### `on_message` + +```python +on_message(self, context: MiddlewareContext[Any], call_next: CallNext[Any, Any]) -> Any +``` + +#### `on_request` + +```python +on_request(self, context: MiddlewareContext[mt.Request], call_next: CallNext[mt.Request, Any]) -> Any +``` + +#### `on_notification` + +```python +on_notification(self, context: MiddlewareContext[mt.Notification], call_next: CallNext[mt.Notification, Any]) -> Any +``` + +#### `on_call_tool` + +```python +on_call_tool(self, context: MiddlewareContext[mt.CallToolRequestParams], call_next: CallNext[mt.CallToolRequestParams, ToolResult]) -> ToolResult +``` + +#### `on_read_resource` + +```python +on_read_resource(self, context: MiddlewareContext[mt.ReadResourceRequestParams], call_next: CallNext[mt.ReadResourceRequestParams, list[ReadResourceContents]]) -> list[ReadResourceContents] +``` + +#### `on_get_prompt` + +```python +on_get_prompt(self, context: MiddlewareContext[mt.GetPromptRequestParams], call_next: CallNext[mt.GetPromptRequestParams, mt.GetPromptResult]) -> mt.GetPromptResult +``` + +#### `on_list_tools` + +```python +on_list_tools(self, context: MiddlewareContext[mt.ListToolsRequest], call_next: CallNext[mt.ListToolsRequest, list[Tool]]) -> list[Tool] +``` + +#### `on_list_resources` + +```python +on_list_resources(self, context: MiddlewareContext[mt.ListResourcesRequest], call_next: CallNext[mt.ListResourcesRequest, list[Resource]]) -> list[Resource] +``` + +#### `on_list_resource_templates` + +```python +on_list_resource_templates(self, context: MiddlewareContext[mt.ListResourceTemplatesRequest], call_next: CallNext[mt.ListResourceTemplatesRequest, list[ResourceTemplate]]) -> list[ResourceTemplate] +``` + +#### `on_list_prompts` + +```python +on_list_prompts(self, context: MiddlewareContext[mt.ListPromptsRequest], call_next: CallNext[mt.ListPromptsRequest, list[Prompt]]) -> list[Prompt] +``` diff --git a/docs/python-sdk/fastmcp-server-middleware-rate_limiting.mdx b/docs/python-sdk/fastmcp-server-middleware-rate_limiting.mdx new file mode 100644 index 000000000..1c6da0c11 --- /dev/null +++ b/docs/python-sdk/fastmcp-server-middleware-rate_limiting.mdx @@ -0,0 +1,97 @@ +--- +title: rate_limiting +sidebarTitle: rate_limiting +--- + +# `fastmcp.server.middleware.rate_limiting` + + +Rate limiting middleware for protecting FastMCP servers from abuse. + +## Classes + +### `RateLimitError` + + +Error raised when rate limit is exceeded. + + +### `TokenBucketRateLimiter` + + +Token bucket implementation for rate limiting. + + +**Methods:** + +#### `consume` + +```python +consume(self, tokens: int = 1) -> bool +``` + +Try to consume tokens from the bucket. + +**Args:** +- `tokens`: Number of tokens to consume + +**Returns:** +- True if tokens were available and consumed, False otherwise + + +### `SlidingWindowRateLimiter` + + +Sliding window rate limiter implementation. + + +**Methods:** + +#### `is_allowed` + +```python +is_allowed(self) -> bool +``` + +Check if a request is allowed. + + +### `RateLimitingMiddleware` + + +Middleware that implements rate limiting to prevent server abuse. + +Uses a token bucket algorithm by default, allowing for burst traffic +while maintaining a sustainable long-term rate. + + +**Methods:** + +#### `on_request` + +```python +on_request(self, context: MiddlewareContext, call_next: CallNext) -> Any +``` + +Apply rate limiting to requests. + + +### `SlidingWindowRateLimitingMiddleware` + + +Middleware that implements sliding window rate limiting. + +Uses a sliding window approach which provides more precise rate limiting +but uses more memory to track individual request timestamps. + + +**Methods:** + +#### `on_request` + +```python +on_request(self, context: MiddlewareContext, call_next: CallNext) -> Any +``` + +Apply sliding window rate limiting to requests. + diff --git a/docs/python-sdk/fastmcp-server-middleware-timing.mdx b/docs/python-sdk/fastmcp-server-middleware-timing.mdx new file mode 100644 index 000000000..dafcc1586 --- /dev/null +++ b/docs/python-sdk/fastmcp-server-middleware-timing.mdx @@ -0,0 +1,105 @@ +--- +title: timing +sidebarTitle: timing +--- + +# `fastmcp.server.middleware.timing` + + +Timing middleware for measuring and logging request performance. + +## Classes + +### `TimingMiddleware` + + +Middleware that logs the execution time of requests. + +Only measures and logs timing for request messages (not notifications). +Provides insights into performance characteristics of your MCP server. + + +**Methods:** + +#### `on_request` + +```python +on_request(self, context: MiddlewareContext, call_next: CallNext) -> Any +``` + +Time request execution and log the results. + + +### `DetailedTimingMiddleware` + + +Enhanced timing middleware with per-operation breakdowns. + +Provides detailed timing information for different types of MCP operations, +allowing you to identify performance bottlenecks in specific operations. + + +**Methods:** + +#### `on_call_tool` + +```python +on_call_tool(self, context: MiddlewareContext, call_next: CallNext) -> Any +``` + +Time tool execution. + + +#### `on_read_resource` + +```python +on_read_resource(self, context: MiddlewareContext, call_next: CallNext) -> Any +``` + +Time resource reading. + + +#### `on_get_prompt` + +```python +on_get_prompt(self, context: MiddlewareContext, call_next: CallNext) -> Any +``` + +Time prompt retrieval. + + +#### `on_list_tools` + +```python +on_list_tools(self, context: MiddlewareContext, call_next: CallNext) -> Any +``` + +Time tool listing. + + +#### `on_list_resources` + +```python +on_list_resources(self, context: MiddlewareContext, call_next: CallNext) -> Any +``` + +Time resource listing. + + +#### `on_list_resource_templates` + +```python +on_list_resource_templates(self, context: MiddlewareContext, call_next: CallNext) -> Any +``` + +Time resource template listing. + + +#### `on_list_prompts` + +```python +on_list_prompts(self, context: MiddlewareContext, call_next: CallNext) -> Any +``` + +Time prompt listing. + diff --git a/docs/python-sdk/fastmcp-server-openapi.mdx b/docs/python-sdk/fastmcp-server-openapi.mdx new file mode 100644 index 000000000..bc1a1f730 --- /dev/null +++ b/docs/python-sdk/fastmcp-server-openapi.mdx @@ -0,0 +1,91 @@ +--- +title: openapi +sidebarTitle: openapi +--- + +# `fastmcp.server.openapi` + + +FastMCP server implementation for OpenAPI integration. + +## Classes + +### `MCPType` + + +Type of FastMCP component to create from a route. + + +### `RouteType` + + +Deprecated: Use MCPType instead. + +This enum is kept for backward compatibility and will be removed in a future version. + + +### `RouteMap` + + +Mapping configuration for HTTP routes to FastMCP component types. + + +### `OpenAPITool` + + +Tool implementation for OpenAPI endpoints. + + +**Methods:** + +#### `run` + +```python +run(self, arguments: dict[str, Any]) -> ToolResult +``` + +Execute the HTTP request based on the route configuration. + + +### `OpenAPIResource` + + +Resource implementation for OpenAPI endpoints. + + +**Methods:** + +#### `read` + +```python +read(self) -> str | bytes +``` + +Fetch the resource data by making an HTTP request. + + +### `OpenAPIResourceTemplate` + + +Resource template implementation for OpenAPI endpoints. + + +**Methods:** + +#### `create_resource` + +```python +create_resource(self, uri: str, params: dict[str, Any], context: Context | None = None) -> Resource +``` + +Create a resource with the given parameters. + + +### `FastMCPOpenAPI` + + +FastMCP server implementation that creates components from an OpenAPI schema. + +This class parses an OpenAPI specification and creates appropriate FastMCP components +(Tools, Resources, ResourceTemplates) based on route mappings. + diff --git a/docs/python-sdk/fastmcp-server-proxy.mdx b/docs/python-sdk/fastmcp-server-proxy.mdx new file mode 100644 index 000000000..3be5ddc97 --- /dev/null +++ b/docs/python-sdk/fastmcp-server-proxy.mdx @@ -0,0 +1,339 @@ +--- +title: proxy +sidebarTitle: proxy +--- + +# `fastmcp.server.proxy` + +## Functions + +### `default_proxy_roots_handler` + +```python +default_proxy_roots_handler(context: RequestContext[ClientSession, LifespanContextT]) -> RootsList +``` + + +A handler that forwards the list roots request from the remote server to the proxy's connected clients and relays the response back to the remote server. + + +## Classes + +### `ProxyManagerMixin` + + +A mixin for proxy managers to provide a unified client retrieval method. + + +### `ProxyToolManager` + + +A ToolManager that sources its tools from a remote client in addition to local and mounted tools. + + +**Methods:** + +#### `get_tools` + +```python +get_tools(self) -> dict[str, Tool] +``` + +Gets the unfiltered tool inventory including local, mounted, and proxy tools. + + +#### `list_tools` + +```python +list_tools(self) -> list[Tool] +``` + +Gets the filtered list of tools including local, mounted, and proxy tools. + + +#### `call_tool` + +```python +call_tool(self, key: str, arguments: dict[str, Any]) -> ToolResult +``` + +Calls a tool, trying local/mounted first, then proxy if not found. + + +### `ProxyResourceManager` + + +A ResourceManager that sources its resources from a remote client in addition to local and mounted resources. + + +**Methods:** + +#### `get_resources` + +```python +get_resources(self) -> dict[str, Resource] +``` + +Gets the unfiltered resource inventory including local, mounted, and proxy resources. + + +#### `get_resource_templates` + +```python +get_resource_templates(self) -> dict[str, ResourceTemplate] +``` + +Gets the unfiltered template inventory including local, mounted, and proxy templates. + + +#### `list_resources` + +```python +list_resources(self) -> list[Resource] +``` + +Gets the filtered list of resources including local, mounted, and proxy resources. + + +#### `list_resource_templates` + +```python +list_resource_templates(self) -> list[ResourceTemplate] +``` + +Gets the filtered list of templates including local, mounted, and proxy templates. + + +#### `read_resource` + +```python +read_resource(self, uri: AnyUrl | str) -> str | bytes +``` + +Reads a resource, trying local/mounted first, then proxy if not found. + + +### `ProxyPromptManager` + + +A PromptManager that sources its prompts from a remote client in addition to local and mounted prompts. + + +**Methods:** + +#### `get_prompts` + +```python +get_prompts(self) -> dict[str, Prompt] +``` + +Gets the unfiltered prompt inventory including local, mounted, and proxy prompts. + + +#### `list_prompts` + +```python +list_prompts(self) -> list[Prompt] +``` + +Gets the filtered list of prompts including local, mounted, and proxy prompts. + + +#### `render_prompt` + +```python +render_prompt(self, name: str, arguments: dict[str, Any] | None = None) -> GetPromptResult +``` + +Renders a prompt, trying local/mounted first, then proxy if not found. + + +### `ProxyTool` + + +A Tool that represents and executes a tool on a remote server. + + +**Methods:** + +#### `from_mcp_tool` + +```python +from_mcp_tool(cls, client: Client, mcp_tool: mcp.types.Tool) -> ProxyTool +``` + +Factory method to create a ProxyTool from a raw MCP tool schema. + + +#### `run` + +```python +run(self, arguments: dict[str, Any], context: Context | None = None) -> ToolResult +``` + +Executes the tool by making a call through the client. + + +### `ProxyResource` + + +A Resource that represents and reads a resource from a remote server. + + +**Methods:** + +#### `from_mcp_resource` + +```python +from_mcp_resource(cls, client: Client, mcp_resource: mcp.types.Resource) -> ProxyResource +``` + +Factory method to create a ProxyResource from a raw MCP resource schema. + + +#### `read` + +```python +read(self) -> str | bytes +``` + +Read the resource content from the remote server. + + +### `ProxyTemplate` + + +A ResourceTemplate that represents and creates resources from a remote server template. + + +**Methods:** + +#### `from_mcp_template` + +```python +from_mcp_template(cls, client: Client, mcp_template: mcp.types.ResourceTemplate) -> ProxyTemplate +``` + +Factory method to create a ProxyTemplate from a raw MCP template schema. + + +#### `create_resource` + +```python +create_resource(self, uri: str, params: dict[str, Any], context: Context | None = None) -> ProxyResource +``` + +Create a resource from the template by calling the remote server. + + +### `ProxyPrompt` + + +A Prompt that represents and renders a prompt from a remote server. + + +**Methods:** + +#### `from_mcp_prompt` + +```python +from_mcp_prompt(cls, client: Client, mcp_prompt: mcp.types.Prompt) -> ProxyPrompt +``` + +Factory method to create a ProxyPrompt from a raw MCP prompt schema. + + +#### `render` + +```python +render(self, arguments: dict[str, Any]) -> list[PromptMessage] +``` + +Render the prompt by making a call through the client. + + +### `FastMCPProxy` + + +A FastMCP server that acts as a proxy to a remote MCP-compliant server. +It uses specialized managers that fulfill requests via a client factory. + + +### `ProxyClient` + + +A proxy client that forwards advanced interactions between a remote MCP server and the proxy's connected clients. +Supports forwarding roots, sampling, elicitation, logging, and progress. + + +**Methods:** + +#### `default_sampling_handler` + +```python +default_sampling_handler(cls, messages: list[mcp.types.SamplingMessage], params: mcp.types.CreateMessageRequestParams, context: RequestContext[ClientSession, LifespanContextT]) -> mcp.types.CreateMessageResult +``` + +A handler that forwards the sampling request from the remote server to the proxy's connected clients and relays the response back to the remote server. + + +#### `default_elicitation_handler` + +```python +default_elicitation_handler(cls, message: str, response_type: type, params: mcp.types.ElicitRequestParams, context: RequestContext[ClientSession, LifespanContextT]) -> ElicitResult +``` + +A handler that forwards the elicitation request from the remote server to the proxy's connected clients and relays the response back to the remote server. + + +#### `default_log_handler` + +```python +default_log_handler(cls, message: LogMessage) -> None +``` + +A handler that forwards the log notification from the remote server to the proxy's connected clients. + + +#### `default_progress_handler` + +```python +default_progress_handler(cls, progress: float, total: float | None, message: str | None) -> None +``` + +A handler that forwards the progress notification from the remote server to the proxy's connected clients. + + +### `StatefulProxyClient` + + +A proxy client that provides a stateful client factory for the proxy server. + +The stateful proxy client bound its copy to the server session. +And it will be disconnected when the session is exited. + +This is useful to proxy a stateful mcp server such as the Playwright MCP server. +Note that it is essential to ensure that the proxy server itself is also stateful. + + +**Methods:** + +#### `clear` + +```python +clear(self) +``` + +Clear all cached clients and force disconnect them. + + +#### `new_stateful` + +```python +new_stateful(self) -> Client[ClientTransportT] +``` + +Create a new stateful proxy client instance with the same configuration. + +Use this method as the client factory for stateful proxy server. + diff --git a/docs/python-sdk/fastmcp-server-server.mdx b/docs/python-sdk/fastmcp-server-server.mdx new file mode 100644 index 000000000..20b58f460 --- /dev/null +++ b/docs/python-sdk/fastmcp-server-server.mdx @@ -0,0 +1,861 @@ +--- +title: server +sidebarTitle: server +--- + +# `fastmcp.server.server` + + +FastMCP - A more ergonomic interface for MCP servers. + +## Functions + +### `default_lifespan` + +```python +default_lifespan(server: FastMCP[LifespanResultT]) -> AsyncIterator[Any] +``` + + +Default lifespan context manager that does nothing. + +**Args:** +- `server`: The server instance this lifespan is managing + +**Returns:** +- An empty context object + + +### `add_resource_prefix` + +```python +add_resource_prefix(uri: str, prefix: str, prefix_format: Literal['protocol', 'path'] | None = None) -> str +``` + + +Add a prefix to a resource URI. + +**Args:** +- `uri`: The original resource URI +- `prefix`: The prefix to add + +**Returns:** +- The resource URI with the prefix added + +**Examples:** + +With new style: +```python +add_resource_prefix("resource://path/to/resource", "prefix") +"resource://prefix/path/to/resource" +``` +With legacy style: +```python +add_resource_prefix("resource://path/to/resource", "prefix") +"prefix+resource://path/to/resource" +``` +With absolute path: +```python +add_resource_prefix("resource:///absolute/path", "prefix") +"resource://prefix//absolute/path" +``` + +**Raises:** +- `ValueError`: If the URI doesn't match the expected protocol\://path format + + +### `remove_resource_prefix` + +```python +remove_resource_prefix(uri: str, prefix: str, prefix_format: Literal['protocol', 'path'] | None = None) -> str +``` + + +Remove a prefix from a resource URI. + +**Args:** +- `uri`: The resource URI with a prefix +- `prefix`: The prefix to remove +- `prefix_format`: The format of the prefix to remove + +Returns: + The resource URI with the prefix removed + +**Examples:** + +With new style: +```python +remove_resource_prefix("resource://prefix/path/to/resource", "prefix") +"resource://path/to/resource" +``` +With legacy style: +```python +remove_resource_prefix("prefix+resource://path/to/resource", "prefix") +"resource://path/to/resource" +``` +With absolute path: +```python +remove_resource_prefix("resource://prefix//absolute/path", "prefix") +"resource:///absolute/path" +``` + +**Raises:** +- `ValueError`: If the URI doesn't match the expected protocol\://path format + + +### `has_resource_prefix` + +```python +has_resource_prefix(uri: str, prefix: str, prefix_format: Literal['protocol', 'path'] | None = None) -> bool +``` + + +Check if a resource URI has a specific prefix. + +**Args:** +- `uri`: The resource URI to check +- `prefix`: The prefix to look for + +**Returns:** +- True if the URI has the specified prefix, False otherwise + +**Examples:** + +With new style: +```python +has_resource_prefix("resource://prefix/path/to/resource", "prefix") +True +``` +With legacy style: +```python +has_resource_prefix("prefix+resource://path/to/resource", "prefix") +True +``` +With other path: +```python +has_resource_prefix("resource://other/path/to/resource", "prefix") +False +``` + +**Raises:** +- `ValueError`: If the URI doesn't match the expected protocol\://path format + + +## Classes + +### `FastMCP` + +**Methods:** + +#### `settings` + +```python +settings(self) -> Settings +``` + +#### `name` + +```python +name(self) -> str +``` + +#### `instructions` + +```python +instructions(self) -> str | None +``` + +#### `instructions` + +```python +instructions(self, value: str | None) -> None +``` + +#### `version` + +```python +version(self) -> str | None +``` + +#### `run_async` + +```python +run_async(self, transport: Transport | None = None, show_banner: bool = True, **transport_kwargs: Any) -> None +``` + +Run the FastMCP server asynchronously. + +**Args:** +- `transport`: Transport protocol to use ("stdio", "sse", or "streamable-http") + + +#### `run` + +```python +run(self, transport: Transport | None = None, show_banner: bool = True, **transport_kwargs: Any) -> None +``` + +Run the FastMCP server. Note this is a synchronous function. + +**Args:** +- `transport`: Transport protocol to use ("stdio", "sse", or "streamable-http") + + +#### `add_middleware` + +```python +add_middleware(self, middleware: Middleware) -> None +``` + +#### `get_tools` + +```python +get_tools(self) -> dict[str, Tool] +``` + +Get all registered tools, indexed by registered key. + + +#### `get_tool` + +```python +get_tool(self, key: str) -> Tool +``` + +#### `get_resources` + +```python +get_resources(self) -> dict[str, Resource] +``` + +Get all registered resources, indexed by registered key. + + +#### `get_resource` + +```python +get_resource(self, key: str) -> Resource +``` + +#### `get_resource_templates` + +```python +get_resource_templates(self) -> dict[str, ResourceTemplate] +``` + +Get all registered resource templates, indexed by registered key. + + +#### `get_resource_template` + +```python +get_resource_template(self, key: str) -> ResourceTemplate +``` + +Get a registered resource template by key. + + +#### `get_prompts` + +```python +get_prompts(self) -> dict[str, Prompt] +``` + +List all available prompts. + + +#### `get_prompt` + +```python +get_prompt(self, key: str) -> Prompt +``` + +#### `custom_route` + +```python +custom_route(self, path: str, methods: list[str], name: str | None = None, include_in_schema: bool = True) -> Callable[[Callable[[Request], Awaitable[Response]]], Callable[[Request], Awaitable[Response]]] +``` + +Decorator to register a custom HTTP route on the FastMCP server. + +Allows adding arbitrary HTTP endpoints outside the standard MCP protocol, +which can be useful for OAuth callbacks, health checks, or admin APIs. +The handler function must be an async function that accepts a Starlette +Request and returns a Response. + +**Args:** +- `path`: URL path for the route (e.g., "/auth/callback") +- `methods`: List of HTTP methods to support (e.g., ["GET", "POST"]) +- `name`: Optional name for the route (to reference this route with +Starlette's reverse URL lookup feature) +- `include_in_schema`: Whether to include in OpenAPI schema, defaults to True + + +#### `add_tool` + +```python +add_tool(self, tool: Tool) -> Tool +``` + +Add a tool to the server. + +The tool function can optionally request a Context object by adding a parameter +with the Context type annotation. See the @tool decorator for examples. + +**Args:** +- `tool`: The Tool instance to register + +**Returns:** +- The tool instance that was added to the server. + + +#### `remove_tool` + +```python +remove_tool(self, name: str) -> None +``` + +Remove a tool from the server. + +**Args:** +- `name`: The name of the tool to remove + +**Raises:** +- `NotFoundError`: If the tool is not found + + +#### `add_tool_transformation` + +```python +add_tool_transformation(self, tool_name: str, transformation: ToolTransformConfig) -> None +``` + +Add a tool transformation. + + +#### `remove_tool_transformation` + +```python +remove_tool_transformation(self, tool_name: str) -> None +``` + +Remove a tool transformation. + + +#### `tool` + +```python +tool(self, name_or_fn: AnyFunction) -> FunctionTool +``` + +#### `tool` + +```python +tool(self, name_or_fn: str | None = None) -> Callable[[AnyFunction], FunctionTool] +``` + +#### `tool` + +```python +tool(self, name_or_fn: str | AnyFunction | None = None) -> Callable[[AnyFunction], FunctionTool] | FunctionTool +``` + +Decorator to register a tool. + +Tools can optionally request a Context object by adding a parameter with the +Context type annotation. The context provides access to MCP capabilities like +logging, progress reporting, and resource access. + +This decorator supports multiple calling patterns: +- @server.tool (without 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) + +**Args:** +- `name_or_fn`: Either a function (when used as @tool), a string name, or None +- `name`: Optional name for the tool (keyword-only, alternative to name_or_fn) +- `description`: Optional description of what the tool does +- `tags`: Optional set of tags for categorizing the tool +- `output_schema`: Optional JSON schema for the tool's output +- `annotations`: Optional annotations about the tool's behavior +- `exclude_args`: Optional list of argument names to exclude from the tool schema +- `meta`: Optional meta information about the tool +- `enabled`: Optional boolean to enable or disable the tool + +**Examples:** + +Register a tool with a custom name: +```python +@server.tool +def my_tool(x: int) -> str: + return str(x) + +# Register a tool with a custom name +@server.tool +def my_tool(x: int) -> str: + return str(x) + +@server.tool("custom_name") +def my_tool(x: int) -> str: + return str(x) + +@server.tool(name="custom_name") +def my_tool(x: int) -> str: + return str(x) + +# Direct function call +server.tool(my_function, name="custom_name") +``` + + +#### `add_resource` + +```python +add_resource(self, resource: Resource) -> Resource +``` + +Add a resource to the server. + +**Args:** +- `resource`: A Resource instance to add + +**Returns:** +- The resource instance that was added to the server. + + +#### `add_template` + +```python +add_template(self, template: ResourceTemplate) -> ResourceTemplate +``` + +Add a resource template to the server. + +**Args:** +- `template`: A ResourceTemplate instance to add + +**Returns:** +- The template instance that was added to the server. + + +#### `add_resource_fn` + +```python +add_resource_fn(self, fn: AnyFunction, uri: str, name: str | None = None, description: str | None = None, mime_type: str | None = None, tags: set[str] | None = None) -> None +``` + +Add a resource or template to the server from a function. + +If the URI contains parameters (e.g. "resource://{param}") or the function +has parameters, it will be registered as a template resource. + +**Args:** +- `fn`: The function to register as a resource +- `uri`: The URI for the resource +- `name`: Optional name for the resource +- `description`: Optional description of the resource +- `mime_type`: Optional MIME type for the resource +- `tags`: Optional set of tags for categorizing the resource + + +#### `resource` + +```python +resource(self, uri: str) -> Callable[[AnyFunction], Resource | ResourceTemplate] +``` + +Decorator to register a function as a resource. + +The function will be called when the resource is read to generate its content. +The function can return: +- str for text content +- bytes for binary content +- other types will be converted to JSON + +Resources can optionally request a Context object by adding a parameter with the +Context type annotation. The context provides access to MCP capabilities like +logging, progress reporting, and session information. + +If the URI contains parameters (e.g. "resource://{param}") or the function +has parameters, it will be registered as a template resource. + +**Args:** +- `uri`: URI for the resource (e.g. "resource\://my-resource" or "resource\://{param}") +- `name`: Optional name for the resource +- `description`: Optional description of the resource +- `mime_type`: Optional MIME type for the resource +- `tags`: Optional set of tags for categorizing the resource +- `enabled`: Optional boolean to enable or disable the resource +- `annotations`: Optional annotations about the resource's behavior +- `meta`: Optional meta information about the resource + +**Examples:** + +Register a resource with a custom name: +```python +@server.resource("resource://my-resource") +def get_data() -> str: + return "Hello, world!" + +@server.resource("resource://my-resource") +async get_data() -> str: + data = await fetch_data() + return f"Hello, world! {data}" + +@server.resource("resource://{city}/weather") +def get_weather(city: str) -> str: + return f"Weather for {city}" + +@server.resource("resource://{city}/weather") +async def get_weather_with_context(city: str, ctx: Context) -> str: + await ctx.info(f"Fetching weather for {city}") + return f"Weather for {city}" + +@server.resource("resource://{city}/weather") +async def get_weather(city: str) -> str: + data = await fetch_weather(city) + return f"Weather for {city}: {data}" +``` + + +#### `add_prompt` + +```python +add_prompt(self, prompt: Prompt) -> Prompt +``` + +Add a prompt to the server. + +**Args:** +- `prompt`: A Prompt instance to add + +**Returns:** +- The prompt instance that was added to the server. + + +#### `prompt` + +```python +prompt(self, name_or_fn: AnyFunction) -> FunctionPrompt +``` + +#### `prompt` + +```python +prompt(self, name_or_fn: str | None = None) -> Callable[[AnyFunction], FunctionPrompt] +``` + +#### `prompt` + +```python +prompt(self, name_or_fn: str | AnyFunction | None = None) -> Callable[[AnyFunction], FunctionPrompt] | FunctionPrompt +``` + +Decorator to register a prompt. + + Prompts can optionally request a Context object by adding a parameter with the + Context type annotation. The context provides access to MCP capabilities like + logging, progress reporting, and session information. + + This decorator supports multiple calling patterns: + - @server.prompt (without parentheses) + - @server.prompt() (with empty parentheses) + - @server.prompt("custom_name") (with name as first argument) + - @server.prompt(name="custom_name") (with name as keyword argument) + - server.prompt(function, name="custom_name") (direct function call) + + Args: + name_or_fn: Either a function (when used as @prompt), a string name, or None + name: Optional name for the prompt (keyword-only, alternative to name_or_fn) + description: Optional description of what the prompt does + tags: Optional set of tags for categorizing the prompt + enabled: Optional boolean to enable or disable the prompt + meta: Optional meta information about the prompt + + Examples: + + ```python + @server.prompt + def analyze_table(table_name: str) -> list[Message]: + schema = read_table_schema(table_name) + return [ + { + "role": "user", + "content": f"Analyze this schema: +{schema}" + } + ] + + @server.prompt() + async def analyze_with_context(table_name: str, ctx: Context) -> list[Message]: + await ctx.info(f"Analyzing table {table_name}") + schema = read_table_schema(table_name) + return [ + { + "role": "user", + "content": f"Analyze this schema: +{schema}" + } + ] + + @server.prompt("custom_name") + async def analyze_file(path: str) -> list[Message]: + content = await read_file(path) + return [ + { + "role": "user", + "content": { + "type": "resource", + "resource": { + "uri": f"file://{path}", + "text": content + } + } + } + ] + + @server.prompt(name="custom_name") + def another_prompt(data: str) -> list[Message]: + return [{"role": "user", "content": data}] + + # Direct function call + server.prompt(my_function, name="custom_name") + ``` + + +#### `run_stdio_async` + +```python +run_stdio_async(self, show_banner: bool = True, log_level: str | None = None) -> None +``` + +Run the server using stdio transport. + +**Args:** +- `show_banner`: Whether to display the server banner +- `log_level`: Log level for the server + + +#### `run_http_async` + +```python +run_http_async(self, show_banner: bool = True, transport: Literal['http', 'streamable-http', 'sse'] = 'http', host: str | None = None, port: int | None = None, log_level: str | None = None, path: str | None = None, uvicorn_config: dict[str, Any] | None = None, middleware: list[ASGIMiddleware] | None = None, stateless_http: bool | None = None) -> None +``` + +Run the server using HTTP transport. + +**Args:** +- `transport`: Transport protocol to use - either "streamable-http" (default) or "sse" +- `host`: Host address to bind to (defaults to settings.host) +- `port`: Port to bind to (defaults to settings.port) +- `log_level`: Log level for the server (defaults to settings.log_level) +- `path`: Path for the endpoint (defaults to settings.streamable_http_path or settings.sse_path) +- `uvicorn_config`: Additional configuration for the Uvicorn server +- `middleware`: A list of middleware to apply to the app +- `stateless_http`: Whether to use stateless HTTP (defaults to settings.stateless_http) + + +#### `run_sse_async` + +```python +run_sse_async(self, host: str | None = None, port: int | None = None, log_level: str | None = None, path: str | None = None, uvicorn_config: dict[str, Any] | None = None) -> None +``` + +Run the server using SSE transport. + + +#### `sse_app` + +```python +sse_app(self, path: str | None = None, message_path: str | None = None, middleware: list[ASGIMiddleware] | None = None) -> StarletteWithLifespan +``` + +Create a Starlette app for the SSE server. + +**Args:** +- `path`: The path to the SSE endpoint +- `message_path`: The path to the message endpoint +- `middleware`: A list of middleware to apply to the app + + +#### `streamable_http_app` + +```python +streamable_http_app(self, path: str | None = None, middleware: list[ASGIMiddleware] | None = None) -> StarletteWithLifespan +``` + +Create a Starlette app for the StreamableHTTP server. + +**Args:** +- `path`: The path to the StreamableHTTP endpoint +- `middleware`: A list of middleware to apply to the app + + +#### `http_app` + +```python +http_app(self, path: str | None = None, middleware: list[ASGIMiddleware] | None = None, json_response: bool | None = None, stateless_http: bool | None = None, transport: Literal['http', 'streamable-http', 'sse'] = 'http') -> StarletteWithLifespan +``` + +Create a Starlette app using the specified HTTP transport. + +**Args:** +- `path`: The path for the HTTP endpoint +- `middleware`: A list of middleware to apply to the app +- `transport`: Transport protocol to use - either "streamable-http" (default) or "sse" + +**Returns:** +- A Starlette application configured with the specified transport + + +#### `run_streamable_http_async` + +```python +run_streamable_http_async(self, host: str | None = None, port: int | None = None, log_level: str | None = None, path: str | None = None, uvicorn_config: dict[str, Any] | None = None) -> None +``` + +#### `mount` + +```python +mount(self, server: FastMCP[LifespanResultT], prefix: str | None = None, as_proxy: bool | None = None) -> None +``` + +Mount another FastMCP server on this server with an optional prefix. + +Unlike importing (with import_server), mounting establishes a dynamic connection +between servers. When a client interacts with a mounted server's objects through +the parent server, requests are forwarded to the mounted server in real-time. +This means changes to the mounted server are immediately reflected when accessed +through the parent. + +When a server is mounted with a prefix: +- Tools from the mounted server are accessible with prefixed names. + Example: If server has a tool named "get_weather", it will be available as "prefix_get_weather". +- Resources are accessible with prefixed URIs. + Example: If server has a resource with URI "weather://forecast", it will be available as + "weather://prefix/forecast". +- Templates are accessible with prefixed URI templates. + Example: If server has a template with URI "weather://location/{id}", it will be available + as "weather://prefix/location/{id}". +- Prompts are accessible with prefixed names. + Example: If server has a prompt named "weather_prompt", it will be available as + "prefix_weather_prompt". + +When a server is mounted without a prefix (prefix=None), its tools, resources, templates, +and prompts are accessible with their original names. Multiple servers can be mounted +without prefixes, and they will be tried in order until a match is found. + +There are two modes for mounting servers: +1. Direct mounting (default when server has no custom lifespan): The parent server + directly accesses the mounted server's objects in-memory for better performance. + In this mode, no client lifecycle events occur on the mounted server, including + lifespan execution. + +2. Proxy mounting (default when server has a custom lifespan): The parent server + treats the mounted server as a separate entity and communicates with it via a + Client transport. This preserves all client-facing behaviors, including lifespan + execution, but with slightly higher overhead. + +**Args:** +- `server`: The FastMCP server to mount. +- `prefix`: Optional prefix to use for the mounted server's objects. If None, +the server's objects are accessible with their original names. +- `as_proxy`: Whether to treat the mounted server as a proxy. If None (default), +automatically determined based on whether the server has a custom lifespan +(True if it has a custom lifespan, False otherwise). +- `tool_separator`: Deprecated. Separator character for tool names. +- `resource_separator`: Deprecated. Separator character for resource URIs. +- `prompt_separator`: Deprecated. Separator character for prompt names. + + +#### `import_server` + +```python +import_server(self, server: FastMCP[LifespanResultT], prefix: str | None = None, tool_separator: str | None = None, resource_separator: str | None = None, prompt_separator: str | None = None) -> None +``` + +Import the MCP objects from another FastMCP server into this one, +optionally with a given prefix. + +Note that when a server is *imported*, its objects are immediately +registered to the importing server. This is a one-time operation and +future changes to the imported server will not be reflected in the +importing server. Server-level configurations and lifespans are not imported. + +When a server is imported with a prefix: +- The tools are imported with prefixed names + Example: If server has a tool named "get_weather", it will be + available as "prefix_get_weather" +- The resources are imported with prefixed URIs using the new format + Example: If server has a resource with URI "weather://forecast", it will + be available as "weather://prefix/forecast" +- The templates are imported with prefixed URI templates using the new format + Example: If server has a template with URI "weather://location/{id}", it will + be available as "weather://prefix/location/{id}" +- The prompts are imported with prefixed names + Example: If server has a prompt named "weather_prompt", it will be available as + "prefix_weather_prompt" + +When a server is imported without a prefix (prefix=None), its tools, resources, +templates, and prompts are imported with their original names. + +**Args:** +- `server`: The FastMCP server to import +- `prefix`: Optional prefix to use for the imported server's objects. If None, +objects are imported with their original names. +- `tool_separator`: Deprecated. Separator for tool names. +- `resource_separator`: Deprecated and ignored. Prefix is now +applied using the protocol\://prefix/path format +- `prompt_separator`: Deprecated. Separator for prompt names. + + +#### `from_openapi` + +```python +from_openapi(cls, openapi_spec: dict[str, Any], client: httpx.AsyncClient, route_maps: list[RouteMap] | list[RouteMapNew] | None = None, route_map_fn: OpenAPIRouteMapFn | OpenAPIRouteMapFnNew | None = None, mcp_component_fn: OpenAPIComponentFn | OpenAPIComponentFnNew | None = None, mcp_names: dict[str, str] | None = None, tags: set[str] | None = None, **settings: Any) -> FastMCPOpenAPI | FastMCPOpenAPINew +``` + +Create a FastMCP server from an OpenAPI specification. + + +#### `from_fastapi` + +```python +from_fastapi(cls, app: Any, name: str | None = None, route_maps: list[RouteMap] | list[RouteMapNew] | None = None, route_map_fn: OpenAPIRouteMapFn | OpenAPIRouteMapFnNew | None = None, mcp_component_fn: OpenAPIComponentFn | OpenAPIComponentFnNew | None = None, mcp_names: dict[str, str] | None = None, httpx_client_kwargs: dict[str, Any] | None = None, tags: set[str] | None = None, **settings: Any) -> FastMCPOpenAPI | FastMCPOpenAPINew +``` + +Create a FastMCP server from a FastAPI application. + + +#### `as_proxy` + +```python +as_proxy(cls, backend: Client[ClientTransportT] | ClientTransport | FastMCP[Any] | AnyUrl | Path | MCPConfig | dict[str, Any] | str, **settings: Any) -> FastMCPProxy +``` + +Create a FastMCP proxy server for the given backend. + +The `backend` argument can be either an existing `fastmcp.client.Client` +instance or any value accepted as the `transport` argument of +`fastmcp.client.Client`. This mirrors the convenience of the +`fastmcp.client.Client` constructor. + + +#### `from_client` + +```python +from_client(cls, client: Client[ClientTransportT], **settings: Any) -> FastMCPProxy +``` + +Create a FastMCP proxy server from a FastMCP client. + + +#### `generate_name` + +```python +generate_name(cls, name: str | None = None) -> str +``` + +### `MountedServer` diff --git a/docs/python-sdk/fastmcp-settings.mdx b/docs/python-sdk/fastmcp-settings.mdx new file mode 100644 index 000000000..6f14415ec --- /dev/null +++ b/docs/python-sdk/fastmcp-settings.mdx @@ -0,0 +1,84 @@ +--- +title: settings +sidebarTitle: settings +--- + +# `fastmcp.settings` + +## Classes + +### `ExtendedEnvSettingsSource` + + +A special EnvSettingsSource that allows for multiple env var prefixes to be used. + +Raises a deprecation warning if the old `FASTMCP_SERVER_` prefix is used. + + +**Methods:** + +#### `get_field_value` + +```python +get_field_value(self, field: FieldInfo, field_name: str) -> tuple[Any, str, bool] +``` + +### `ExtendedSettingsConfigDict` + +### `ExperimentalSettings` + +### `Settings` + + +FastMCP settings. + + +**Methods:** + +#### `get_setting` + +```python +get_setting(self, attr: str) -> Any +``` + +Get a setting. If the setting contains one or more `__`, it will be +treated as a nested setting. + + +#### `set_setting` + +```python +set_setting(self, attr: str, value: Any) -> None +``` + +Set a setting. If the setting contains one or more `__`, it will be +treated as a nested setting. + + +#### `settings_customise_sources` + +```python +settings_customise_sources(cls, settings_cls: type[BaseSettings], init_settings: PydanticBaseSettingsSource, env_settings: PydanticBaseSettingsSource, dotenv_settings: PydanticBaseSettingsSource, file_secret_settings: PydanticBaseSettingsSource) -> tuple[PydanticBaseSettingsSource, ...] +``` + +#### `settings` + +```python +settings(self) -> Self +``` + +This property is for backwards compatibility with FastMCP < 2.8.0, +which accessed fastmcp.settings.settings + + +#### `normalize_log_level` + +```python +normalize_log_level(cls, v) +``` + +#### `server_auth_class` + +```python +server_auth_class(self) -> AuthProvider | None +``` diff --git a/docs/python-sdk/fastmcp-tools-__init__.mdx b/docs/python-sdk/fastmcp-tools-__init__.mdx new file mode 100644 index 000000000..5b7c8b04d --- /dev/null +++ b/docs/python-sdk/fastmcp-tools-__init__.mdx @@ -0,0 +1,8 @@ +--- +title: __init__ +sidebarTitle: __init__ +--- + +# `fastmcp.tools` + +*This module is empty or contains only private/internal implementations.* diff --git a/docs/python-sdk/fastmcp-tools-tool.mdx b/docs/python-sdk/fastmcp-tools-tool.mdx new file mode 100644 index 000000000..ef6ba5fed --- /dev/null +++ b/docs/python-sdk/fastmcp-tools-tool.mdx @@ -0,0 +1,117 @@ +--- +title: tool +sidebarTitle: tool +--- + +# `fastmcp.tools.tool` + +## Functions + +### `default_serializer` + +```python +default_serializer(data: Any) -> str +``` + +## Classes + +### `ToolResult` + +**Methods:** + +#### `to_mcp_result` + +```python +to_mcp_result(self) -> list[ContentBlock] | tuple[list[ContentBlock], dict[str, Any]] +``` + +### `Tool` + + +Internal tool registration info. + + +**Methods:** + +#### `enable` + +```python +enable(self) -> None +``` + +#### `disable` + +```python +disable(self) -> None +``` + +#### `to_mcp_tool` + +```python +to_mcp_tool(self, **overrides: Any) -> MCPTool +``` + +Convert the FastMCP tool to an MCP tool. + + +#### `from_function` + +```python +from_function(fn: Callable[..., Any], name: str | None = None, title: str | None = None, description: str | None = None, tags: set[str] | None = None, annotations: ToolAnnotations | None = None, exclude_args: list[str] | None = None, output_schema: dict[str, Any] | None | NotSetT | Literal[False] = NotSet, serializer: ToolResultSerializerType | None = None, meta: dict[str, Any] | None = None, enabled: bool | None = None) -> FunctionTool +``` + +Create a Tool from a function. + + +#### `run` + +```python +run(self, arguments: dict[str, Any]) -> ToolResult +``` + +Run the tool with arguments. + +This method is not implemented in the base Tool class and must be +implemented by subclasses. + +`run()` can EITHER return a list of ContentBlocks, or a tuple of +(list of ContentBlocks, dict of structured output). + + +#### `from_tool` + +```python +from_tool(cls, tool: Tool) -> TransformedTool +``` + +### `FunctionTool` + +**Methods:** + +#### `from_function` + +```python +from_function(cls, fn: Callable[..., Any], name: str | None = None, title: str | None = None, description: str | None = None, tags: set[str] | None = None, annotations: ToolAnnotations | None = None, exclude_args: list[str] | None = None, output_schema: dict[str, Any] | None | NotSetT | Literal[False] = NotSet, serializer: ToolResultSerializerType | None = None, meta: dict[str, Any] | None = None, enabled: bool | None = None) -> FunctionTool +``` + +Create a Tool from a function. + + +#### `run` + +```python +run(self, arguments: dict[str, Any]) -> ToolResult +``` + +Run the tool with arguments. + + +### `ParsedFunction` + +**Methods:** + +#### `from_function` + +```python +from_function(cls, fn: Callable[..., Any], exclude_args: list[str] | None = None, validate: bool = True, wrap_non_object_output_schema: bool = True) -> ParsedFunction +``` diff --git a/docs/python-sdk/fastmcp-tools-tool_manager.mdx b/docs/python-sdk/fastmcp-tools-tool_manager.mdx new file mode 100644 index 000000000..15cbee1db --- /dev/null +++ b/docs/python-sdk/fastmcp-tools-tool_manager.mdx @@ -0,0 +1,131 @@ +--- +title: tool_manager +sidebarTitle: tool_manager +--- + +# `fastmcp.tools.tool_manager` + +## Classes + +### `ToolManager` + + +Manages FastMCP tools. + + +**Methods:** + +#### `mount` + +```python +mount(self, server: MountedServer) -> None +``` + +Adds a mounted server as a source for tools. + + +#### `has_tool` + +```python +has_tool(self, key: str) -> bool +``` + +Check if a tool exists. + + +#### `get_tool` + +```python +get_tool(self, key: str) -> Tool +``` + +Get tool by key. + + +#### `get_tools` + +```python +get_tools(self) -> dict[str, Tool] +``` + +Gets the complete, unfiltered inventory of all tools. + + +#### `list_tools` + +```python +list_tools(self) -> list[Tool] +``` + +Lists all tools, applying protocol filtering. + + +#### `add_tool_from_fn` + +```python +add_tool_from_fn(self, fn: Callable[..., Any], name: str | None = None, description: str | None = None, tags: set[str] | None = None, annotations: ToolAnnotations | None = None, serializer: Callable[[Any], str] | None = None, exclude_args: list[str] | None = None) -> Tool +``` + +Add a tool to the server. + + +#### `add_tool` + +```python +add_tool(self, tool: Tool) -> Tool +``` + +Register a tool with the server. + + +#### `add_tool_transformation` + +```python +add_tool_transformation(self, tool_name: str, transformation: ToolTransformConfig) -> None +``` + +Add a tool transformation. + + +#### `get_tool_transformation` + +```python +get_tool_transformation(self, tool_name: str) -> ToolTransformConfig | None +``` + +Get a tool transformation. + + +#### `remove_tool_transformation` + +```python +remove_tool_transformation(self, tool_name: str) -> None +``` + +Remove a tool transformation. + + +#### `remove_tool` + +```python +remove_tool(self, key: str) -> None +``` + +Remove a tool from the server. + +**Args:** +- `key`: The key of the tool to remove + +**Raises:** +- `NotFoundError`: If the tool is not found + + +#### `call_tool` + +```python +call_tool(self, key: str, arguments: dict[str, Any]) -> ToolResult +``` + +Internal API for servers: Finds and calls a tool, respecting the +filtered protocol path. + diff --git a/docs/python-sdk/fastmcp-tools-tool_transform.mdx b/docs/python-sdk/fastmcp-tools-tool_transform.mdx new file mode 100644 index 000000000..ea8652fab --- /dev/null +++ b/docs/python-sdk/fastmcp-tools-tool_transform.mdx @@ -0,0 +1,290 @@ +--- +title: tool_transform +sidebarTitle: tool_transform +--- + +# `fastmcp.tools.tool_transform` + +## Functions + +### `forward` + +```python +forward(**kwargs) -> ToolResult +``` + + +Forward to parent tool with argument transformation applied. + +This function can only be called from within a transformed tool's custom +function. It applies argument transformation (renaming, validation) before +calling the parent tool. + +For example, if the parent tool has args `x` and `y`, but the transformed +tool has args `a` and `b`, and an `transform_args` was provided that maps `x` to +`a` and `y` to `b`, then `forward(a=1, b=2)` will call the parent tool with +`x=1` and `y=2`. + +**Args:** +- `**kwargs`: Arguments to forward to the parent tool (using transformed names). + +**Returns:** +- The ToolResult from the parent tool execution. + +**Raises:** +- `RuntimeError`: If called outside a transformed tool context. +- `TypeError`: If provided arguments don't match the transformed schema. + + +### `forward_raw` + +```python +forward_raw(**kwargs) -> ToolResult +``` + + +Forward directly to parent tool without transformation. + +This function bypasses all argument transformation and validation, calling the parent +tool directly with the provided arguments. Use this when you need to call the parent +with its original parameter names and structure. + +For example, if the parent tool has args `x` and `y`, then `forward_raw(x=1, +y=2)` will call the parent tool with `x=1` and `y=2`. + +**Args:** +- `**kwargs`: Arguments to pass directly to the parent tool (using original names). + +**Returns:** +- The ToolResult from the parent tool execution. + +**Raises:** +- `RuntimeError`: If called outside a transformed tool context. + + +### `apply_transformations_to_tools` + +```python +apply_transformations_to_tools(tools: dict[str, Tool], transformations: dict[str, ToolTransformConfig]) -> dict[str, Tool] +``` + + +Apply a list of transformations to a list of tools. Tools that do not have any transformations +are left unchanged. + + +## Classes + +### `ArgTransform` + + +Configuration for transforming a parent tool's argument. + +This class allows fine-grained control over how individual arguments are transformed +when creating a new tool from an existing one. You can rename arguments, change their +descriptions, add default values, or hide them from clients while passing constants. + +**Examples:** + +Rename argument 'old_name' to 'new_name' +```python +ArgTransform(name="new_name") +``` + +Change description only +```python +ArgTransform(description="Updated description") +``` + +Add a default value (makes argument optional) +```python +ArgTransform(default=42) +``` + +Add a default factory (makes argument optional) +```python +ArgTransform(default_factory=lambda: time.time()) +``` + +Change the type +```python +ArgTransform(type=str) +``` + +Hide the argument entirely from clients +```python +ArgTransform(hide=True) +``` + +Hide argument but pass a constant value to parent +```python +ArgTransform(hide=True, default="constant_value") +``` + +Hide argument but pass a factory-generated value to parent +```python +ArgTransform(hide=True, default_factory=lambda: uuid.uuid4().hex) +``` + +Make an optional parameter required (removes any default) +```python +ArgTransform(required=True) +``` + +Combine multiple transformations +```python +ArgTransform(name="new_name", description="New desc", default=None, type=int) +``` + + +### `ArgTransformConfig` + + +A model for requesting a single argument transform. + + +**Methods:** + +#### `to_arg_transform` + +```python +to_arg_transform(self) -> ArgTransform +``` + +Convert the argument transform to a FastMCP argument transform. + + +### `TransformedTool` + + +A tool that is transformed from another tool. + +This class represents a tool that has been created by transforming another tool. +It supports argument renaming, schema modification, custom function injection, +structured output control, and provides context for the forward() and forward_raw() functions. + +The transformation can be purely schema-based (argument renaming, dropping, etc.) +or can include a custom function that uses forward() to call the parent tool +with transformed arguments. Output schemas and structured outputs are automatically +inherited from the parent tool but can be overridden or disabled. + + +**Methods:** + +#### `run` + +```python +run(self, arguments: dict[str, Any]) -> ToolResult +``` + +Run the tool with context set for forward() functions. + +This method executes the tool's function while setting up the context +that allows forward() and forward_raw() to work correctly within custom +functions. + +**Args:** +- `arguments`: Dictionary of arguments to pass to the tool's function. + +**Returns:** +- ToolResult object containing content and optional structured output. + + +#### `from_tool` + +```python +from_tool(cls, tool: Tool, name: str | None = None, title: str | None | NotSetT = NotSet, description: str | None | NotSetT = NotSet, tags: set[str] | None = None, transform_fn: Callable[..., Any] | None = None, transform_args: dict[str, ArgTransform] | None = None, annotations: ToolAnnotations | None | NotSetT = NotSet, output_schema: dict[str, Any] | None | NotSetT | Literal[False] = NotSet, serializer: Callable[[Any], str] | None | NotSetT = NotSet, meta: dict[str, Any] | None | NotSetT = NotSet, enabled: bool | None = None) -> TransformedTool +``` + +Create a transformed tool from a parent tool. + +**Args:** +- `tool`: The parent tool to transform. +- `transform_fn`: Optional custom function. Can use forward() and forward_raw() +to call the parent tool. Functions with **kwargs receive transformed +argument names. +- `name`: New name for the tool. Defaults to parent tool's name. +- `title`: New title for the tool. Defaults to parent tool's title. +- `transform_args`: Optional transformations for parent tool arguments. +Only specified arguments are transformed, others pass through unchanged\: +- Simple rename (str) +- Complex transformation (rename/description/default/drop) (ArgTransform) +- Drop the argument (None) +- `description`: New description. Defaults to parent's description. +- `tags`: New tags. Defaults to parent's tags. +- `annotations`: New annotations. Defaults to parent's annotations. +- `output_schema`: Control output schema for structured outputs\: +- None (default)\: Inherit from transform_fn if available, then parent tool +- dict\: Use custom output schema +- False\: Disable output schema and structured outputs +- `serializer`: New serializer. Defaults to parent's serializer. +- `meta`: Control meta information\: +- NotSet (default)\: Inherit from parent tool +- dict\: Use custom meta information +- None\: Remove meta information + +**Returns:** +- TransformedTool with the specified transformations. + +**Examples:** + +# Transform specific arguments only +```python +Tool.from_tool(parent, transform_args={"old": "new"}) # Others unchanged +``` + +# Custom function with partial transforms +```python +async def custom(x: int, y: int) -> str: + result = await forward(x=x, y=y) + return f"Custom: {result}" + +Tool.from_tool(parent, transform_fn=custom, transform_args={"a": "x", "b": "y"}) +``` + +# Using **kwargs (gets all args, transformed and untransformed) +```python +async def flexible(**kwargs) -> str: + result = await forward(**kwargs) + return f"Got: {kwargs}" + +Tool.from_tool(parent, transform_fn=flexible, transform_args={"a": "x"}) +``` + +# Control structured outputs and schemas +```python +# Custom output schema +Tool.from_tool(parent, output_schema={ + "type": "object", + "properties": {"status": {"type": "string"}} +}) + +# Disable structured outputs +Tool.from_tool(parent, output_schema=None) + +# Return ToolResult for full control +async def custom_output(**kwargs) -> ToolResult: + result = await forward(**kwargs) + return ToolResult( + content=[TextContent(text="Summary")], + structured_content={"processed": True} + ) +``` + + +### `ToolTransformConfig` + + +Provides a way to transform a tool. + + +**Methods:** + +#### `apply` + +```python +apply(self, tool: Tool) -> TransformedTool +``` + +Create a TransformedTool from a provided tool and this transformation configuration. + diff --git a/docs/python-sdk/fastmcp-utilities-__init__.mdx b/docs/python-sdk/fastmcp-utilities-__init__.mdx new file mode 100644 index 000000000..12e12b4ed --- /dev/null +++ b/docs/python-sdk/fastmcp-utilities-__init__.mdx @@ -0,0 +1,9 @@ +--- +title: __init__ +sidebarTitle: __init__ +--- + +# `fastmcp.utilities` + + +FastMCP utility modules. diff --git a/docs/python-sdk/fastmcp-utilities-auth.mdx b/docs/python-sdk/fastmcp-utilities-auth.mdx new file mode 100644 index 000000000..f30966e96 --- /dev/null +++ b/docs/python-sdk/fastmcp-utilities-auth.mdx @@ -0,0 +1,25 @@ +--- +title: auth +sidebarTitle: auth +--- + +# `fastmcp.utilities.auth` + + +Authentication utility helpers. + +## Functions + +### `parse_scopes` + +```python +parse_scopes(value: Any) -> list[str] | None +``` + + +Parse scopes from environment variables or settings values. + +Accepts either a JSON array string, a comma- or space-separated string, +a list of strings, or ``None``. Returns a list of scopes or ``None`` if +no value is provided. + diff --git a/docs/python-sdk/fastmcp-utilities-cli.mdx b/docs/python-sdk/fastmcp-utilities-cli.mdx new file mode 100644 index 000000000..eb4691358 --- /dev/null +++ b/docs/python-sdk/fastmcp-utilities-cli.mdx @@ -0,0 +1,55 @@ +--- +title: cli +sidebarTitle: cli +--- + +# `fastmcp.utilities.cli` + +## Functions + +### `is_already_in_uv_subprocess` + +```python +is_already_in_uv_subprocess() -> bool +``` + + +Check if we're already running in a FastMCP uv subprocess. + + +### `load_and_merge_config` + +```python +load_and_merge_config(server_spec: str | None, **cli_overrides) -> tuple[MCPServerConfig, str] +``` + + +Load config from server_spec and apply CLI overrides. + +This consolidates the config parsing logic that was duplicated across +run, inspect, and dev commands. + +**Args:** +- `server_spec`: Python file, config file, URL, or None to auto-detect +- `cli_overrides`: CLI arguments that override config values + +**Returns:** +- Tuple of (MCPServerConfig, resolved_server_spec) + + +### `log_server_banner` + +```python +log_server_banner(server: FastMCP[Any], transport: Literal['stdio', 'http', 'sse', 'streamable-http']) -> None +``` + + +Creates and logs a formatted banner with server information and logo. + +**Args:** +- `transport`: The transport protocol being used +- `server_name`: Optional server name to display +- `host`: Host address (for HTTP transports) +- `port`: Port number (for HTTP transports) +- `path`: Server path (for HTTP transports) + diff --git a/docs/python-sdk/fastmcp-utilities-components.mdx b/docs/python-sdk/fastmcp-utilities-components.mdx new file mode 100644 index 000000000..c7494f52f --- /dev/null +++ b/docs/python-sdk/fastmcp-utilities-components.mdx @@ -0,0 +1,121 @@ +--- +title: components +sidebarTitle: components +--- + +# `fastmcp.utilities.components` + +## Classes + +### `FastMCPMeta` + +### `FastMCPComponent` + + +Base class for FastMCP tools, prompts, resources, and resource templates. + + +**Methods:** + +#### `key` + +```python +key(self) -> str +``` + +The key of the component. This is used for internal bookkeeping +and may reflect e.g. prefixes or other identifiers. You should not depend on +keys having a certain value, as the same tool loaded from different +hierarchies of servers may have different keys. + + +#### `get_meta` + +```python +get_meta(self, include_fastmcp_meta: bool | None = None) -> dict[str, Any] | None +``` + +Get the meta information about the component. + +If include_fastmcp_meta is True, a `_fastmcp` key will be added to the +meta, containing a `tags` field with the tags of the component. + + +#### `model_copy` + +```python +model_copy(self) -> Self +``` + +Create a copy of the component. + +**Args:** +- `update`: A dictionary of fields to update. +- `deep`: Whether to deep copy the component. +- `key`: The key to use for the copy. + + +#### `enable` + +```python +enable(self) -> None +``` + +Enable the component. + + +#### `disable` + +```python +disable(self) -> None +``` + +Disable the component. + + +#### `copy` + +```python +copy(self) -> Self +``` + +Create a copy of the component. + + +### `MirroredComponent` + + +Base class for components that are mirrored from a remote server. + +Mirrored components cannot be enabled or disabled directly. Call copy() first +to create a local version you can modify. + + +**Methods:** + +#### `enable` + +```python +enable(self) -> None +``` + +Enable the component. + + +#### `disable` + +```python +disable(self) -> None +``` + +Disable the component. + + +#### `copy` + +```python +copy(self) -> Self +``` + +Create a copy of the component that can be modified. + diff --git a/docs/python-sdk/fastmcp-utilities-exceptions.mdx b/docs/python-sdk/fastmcp-utilities-exceptions.mdx new file mode 100644 index 000000000..be51a4213 --- /dev/null +++ b/docs/python-sdk/fastmcp-utilities-exceptions.mdx @@ -0,0 +1,20 @@ +--- +title: exceptions +sidebarTitle: exceptions +--- + +# `fastmcp.utilities.exceptions` + +## Functions + +### `iter_exc` + +```python +iter_exc(group: BaseExceptionGroup) +``` + +### `get_catch_handlers` + +```python +get_catch_handlers() -> Mapping[type[BaseException] | Iterable[type[BaseException]], Callable[[BaseExceptionGroup[Any]], Any]] +``` diff --git a/docs/python-sdk/fastmcp-utilities-http.mdx b/docs/python-sdk/fastmcp-utilities-http.mdx new file mode 100644 index 000000000..609c407e8 --- /dev/null +++ b/docs/python-sdk/fastmcp-utilities-http.mdx @@ -0,0 +1,18 @@ +--- +title: http +sidebarTitle: http +--- + +# `fastmcp.utilities.http` + +## Functions + +### `find_available_port` + +```python +find_available_port() -> int +``` + + +Find an available port by letting the OS assign one. + diff --git a/docs/python-sdk/fastmcp-utilities-inspect.mdx b/docs/python-sdk/fastmcp-utilities-inspect.mdx new file mode 100644 index 000000000..b48e52a5c --- /dev/null +++ b/docs/python-sdk/fastmcp-utilities-inspect.mdx @@ -0,0 +1,143 @@ +--- +title: inspect +sidebarTitle: inspect +--- + +# `fastmcp.utilities.inspect` + + +Utilities for inspecting FastMCP instances. + +## Functions + +### `inspect_fastmcp_v2` + +```python +inspect_fastmcp_v2(mcp: FastMCP[Any]) -> FastMCPInfo +``` + + +Extract information from a FastMCP v2.x instance. + +**Args:** +- `mcp`: The FastMCP v2.x instance to inspect + +**Returns:** +- FastMCPInfo dataclass containing the extracted information + + +### `inspect_fastmcp_v1` + +```python +inspect_fastmcp_v1(mcp: FastMCP1x) -> FastMCPInfo +``` + + +Extract information from a FastMCP v1.x instance using a Client. + +**Args:** +- `mcp`: The FastMCP v1.x instance to inspect + +**Returns:** +- FastMCPInfo dataclass containing the extracted information + + +### `inspect_fastmcp` + +```python +inspect_fastmcp(mcp: FastMCP[Any] | FastMCP1x) -> FastMCPInfo +``` + + +Extract information from a FastMCP instance into a dataclass. + +This function automatically detects whether the instance is FastMCP v1.x or v2.x +and uses the appropriate extraction method. + +**Args:** +- `mcp`: The FastMCP instance to inspect (v1.x or v2.x) + +**Returns:** +- FastMCPInfo dataclass containing the extracted information + + +### `format_fastmcp_info` + +```python +format_fastmcp_info(info: FastMCPInfo) -> bytes +``` + + +Format FastMCPInfo as FastMCP-specific JSON. + +This includes FastMCP-specific fields like tags, enabled, annotations, etc. + + +### `format_mcp_info` + +```python +format_mcp_info(mcp: FastMCP[Any] | FastMCP1x) -> bytes +``` + + +Format server info as standard MCP protocol JSON. + +Uses Client to get the standard MCP protocol format with camelCase fields. +Includes version metadata at the top level. + + +### `format_info` + +```python +format_info(mcp: FastMCP[Any] | FastMCP1x, format: InspectFormat | Literal['fastmcp', 'mcp'], info: FastMCPInfo | None = None) -> bytes +``` + + +Format server information according to the specified format. + +**Args:** +- `mcp`: The FastMCP instance +- `format`: Output format ("fastmcp" or "mcp") +- `info`: Pre-extracted FastMCPInfo (optional, will be extracted if not provided) + +**Returns:** +- JSON bytes in the requested format + + +## Classes + +### `ToolInfo` + + +Information about a tool. + + +### `PromptInfo` + + +Information about a prompt. + + +### `ResourceInfo` + + +Information about a resource. + + +### `TemplateInfo` + + +Information about a resource template. + + +### `FastMCPInfo` + + +Information extracted from a FastMCP instance. + + +### `InspectFormat` + + +Output format for inspect command. + diff --git a/docs/python-sdk/fastmcp-utilities-json_schema.mdx b/docs/python-sdk/fastmcp-utilities-json_schema.mdx new file mode 100644 index 000000000..a436dda8e --- /dev/null +++ b/docs/python-sdk/fastmcp-utilities-json_schema.mdx @@ -0,0 +1,25 @@ +--- +title: json_schema +sidebarTitle: json_schema +--- + +# `fastmcp.utilities.json_schema` + +## Functions + +### `compress_schema` + +```python +compress_schema(schema: dict, prune_params: list[str] | None = None, prune_defs: bool = True, prune_additional_properties: bool = True, prune_titles: bool = False) -> dict +``` + + +Remove the given parameters from the schema. + +**Args:** +- `schema`: The schema to compress +- `prune_params`: List of parameter names to remove from properties +- `prune_defs`: Whether to remove unused definitions +- `prune_additional_properties`: Whether to remove additionalProperties\: false +- `prune_titles`: Whether to remove title fields from the schema + diff --git a/docs/python-sdk/fastmcp-utilities-json_schema_type.mdx b/docs/python-sdk/fastmcp-utilities-json_schema_type.mdx new file mode 100644 index 000000000..50fba7654 --- /dev/null +++ b/docs/python-sdk/fastmcp-utilities-json_schema_type.mdx @@ -0,0 +1,110 @@ +--- +title: json_schema_type +sidebarTitle: json_schema_type +--- + +# `fastmcp.utilities.json_schema_type` + + +Convert JSON Schema to Python types with validation. + +The json_schema_to_type function converts a JSON Schema into a Python type that can be used +for validation with Pydantic. It supports: + +- Basic types (string, number, integer, boolean, null) +- Complex types (arrays, objects) +- Format constraints (date-time, email, uri) +- Numeric constraints (minimum, maximum, multipleOf) +- String constraints (minLength, maxLength, pattern) +- Array constraints (minItems, maxItems, uniqueItems) +- Object properties with defaults +- References and recursive schemas +- Enums and constants +- Union types + +Example: + ```python + schema = { + "type": "object", + "properties": { + "name": {"type": "string", "minLength": 1}, + "age": {"type": "integer", "minimum": 0}, + "email": {"type": "string", "format": "email"} + }, + "required": ["name", "age"] + } + + # Name is optional and will be inferred from schema's "title" property if not provided + Person = json_schema_to_type(schema) + # Creates a validated dataclass with name, age, and optional email fields + ``` + + +## Functions + +### `json_schema_to_type` + +```python +json_schema_to_type(schema: Mapping[str, Any], name: str | None = None) -> type +``` + + +Convert JSON schema to appropriate Python type with validation. + +**Args:** +- `schema`: A JSON Schema dictionary defining the type structure and validation rules +- `name`: Optional name for object schemas. Only allowed when schema type is "object". +If not provided for objects, name will be inferred from schema's "title" +property or default to "Root". + +**Returns:** +- A Python type (typically a dataclass for objects) with Pydantic validation + +**Raises:** +- `ValueError`: If a name is provided for a non-object schema + +**Examples:** + +Create a dataclass from an object schema: +```python +schema = { + "type": "object", + "title": "Person", + "properties": { + "name": {"type": "string", "minLength": 1}, + "age": {"type": "integer", "minimum": 0}, + "email": {"type": "string", "format": "email"} + }, + "required": ["name", "age"] +} + +Person = json_schema_to_type(schema) +# Creates a dataclass with name, age, and optional email fields: +# @dataclass +# class Person: +# name: str +# age: int +# email: str | None = None +``` +Person(name="John", age=30) + +Create a scalar type with constraints: +```python +schema = { + "type": "string", + "minLength": 3, + "pattern": "^[A-Z][a-z]+$" +} + +NameType = json_schema_to_type(schema) +# Creates Annotated[str, StringConstraints(min_length=3, pattern="^[A-Z][a-z]+$")] + +@dataclass +class Name: + name: NameType +``` + + +## Classes + +### `JSONSchema` diff --git a/docs/python-sdk/fastmcp-utilities-logging.mdx b/docs/python-sdk/fastmcp-utilities-logging.mdx new file mode 100644 index 000000000..f139db07a --- /dev/null +++ b/docs/python-sdk/fastmcp-utilities-logging.mdx @@ -0,0 +1,58 @@ +--- +title: logging +sidebarTitle: logging +--- + +# `fastmcp.utilities.logging` + + +Logging utilities for FastMCP. + +## Functions + +### `get_logger` + +```python +get_logger(name: str) -> logging.Logger +``` + + +Get a logger nested under FastMCP namespace. + +**Args:** +- `name`: the name of the logger, which will be prefixed with 'FastMCP.' + +**Returns:** +- a configured logger instance + + +### `configure_logging` + +```python +configure_logging(level: Literal['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'] | int = 'INFO', logger: logging.Logger | None = None, enable_rich_tracebacks: bool | None = None, **rich_kwargs: Any) -> None +``` + + +Configure logging for FastMCP. + +**Args:** +- `logger`: the logger to configure +- `level`: the log level to use +- `rich_kwargs`: the parameters to use for creating RichHandler + + +### `temporary_log_level` + +```python +temporary_log_level(level: str | None, logger: logging.Logger | None = None, enable_rich_tracebacks: bool | None = None, **rich_kwargs: Any) +``` + + +Context manager to temporarily set log level and restore it afterwards. + +**Args:** +- `level`: The temporary log level to set (e.g., "DEBUG", "INFO") +- `logger`: Optional logger to configure (defaults to FastMCP logger) +- `enable_rich_tracebacks`: Whether to enable rich tracebacks +- `**rich_kwargs`: Additional parameters for RichHandler + diff --git a/docs/python-sdk/fastmcp-utilities-mcp_config.mdx b/docs/python-sdk/fastmcp-utilities-mcp_config.mdx new file mode 100644 index 000000000..403acd2be --- /dev/null +++ b/docs/python-sdk/fastmcp-utilities-mcp_config.mdx @@ -0,0 +1,28 @@ +--- +title: mcp_config +sidebarTitle: mcp_config +--- + +# `fastmcp.utilities.mcp_config` + +## Functions + +### `mcp_config_to_servers_and_transports` + +```python +mcp_config_to_servers_and_transports(config: MCPConfig) -> list[tuple[str, FastMCP[Any], ClientTransport]] +``` + + +A utility function to convert each entry of an MCP Config into a transport and server. + + +### `mcp_server_type_to_servers_and_transports` + +```python +mcp_server_type_to_servers_and_transports(name: str, mcp_server: MCPServerTypes) -> tuple[str, FastMCP[Any], ClientTransport] +``` + + +A utility function to convert each entry of an MCP Config into a transport and server. + diff --git a/docs/python-sdk/fastmcp-utilities-mcp_server_config-__init__.mdx b/docs/python-sdk/fastmcp-utilities-mcp_server_config-__init__.mdx new file mode 100644 index 000000000..bc8fc9500 --- /dev/null +++ b/docs/python-sdk/fastmcp-utilities-mcp_server_config-__init__.mdx @@ -0,0 +1,13 @@ +--- +title: __init__ +sidebarTitle: __init__ +--- + +# `fastmcp.utilities.mcp_server_config` + + +FastMCP Configuration module. + +This module provides versioned configuration support for FastMCP servers. +The current version is v1, which is re-exported here for convenience. + diff --git a/docs/python-sdk/fastmcp-utilities-mcp_server_config-v1-__init__.mdx b/docs/python-sdk/fastmcp-utilities-mcp_server_config-v1-__init__.mdx new file mode 100644 index 000000000..d29664280 --- /dev/null +++ b/docs/python-sdk/fastmcp-utilities-mcp_server_config-v1-__init__.mdx @@ -0,0 +1,8 @@ +--- +title: __init__ +sidebarTitle: __init__ +--- + +# `fastmcp.utilities.mcp_server_config.v1` + +*This module is empty or contains only private/internal implementations.* diff --git a/docs/python-sdk/fastmcp-utilities-mcp_server_config-v1-environments-__init__.mdx b/docs/python-sdk/fastmcp-utilities-mcp_server_config-v1-environments-__init__.mdx new file mode 100644 index 000000000..01f147482 --- /dev/null +++ b/docs/python-sdk/fastmcp-utilities-mcp_server_config-v1-environments-__init__.mdx @@ -0,0 +1,9 @@ +--- +title: __init__ +sidebarTitle: __init__ +--- + +# `fastmcp.utilities.mcp_server_config.v1.environments` + + +Environment configuration for MCP servers. diff --git a/docs/python-sdk/fastmcp-utilities-mcp_server_config-v1-environments-base.mdx b/docs/python-sdk/fastmcp-utilities-mcp_server_config-v1-environments-base.mdx new file mode 100644 index 000000000..3a97b5d9e --- /dev/null +++ b/docs/python-sdk/fastmcp-utilities-mcp_server_config-v1-environments-base.mdx @@ -0,0 +1,43 @@ +--- +title: base +sidebarTitle: base +--- + +# `fastmcp.utilities.mcp_server_config.v1.environments.base` + +## Classes + +### `Environment` + + +Base class for environment configuration. + + +**Methods:** + +#### `build_command` + +```python +build_command(self, command: list[str]) -> list[str] +``` + +Build the full command with environment setup. + +**Args:** +- `command`: Base command to wrap with environment setup + +**Returns:** +- Full command ready for subprocess execution + + +#### `prepare` + +```python +prepare(self, output_dir: Path | None = None) -> None +``` + +Prepare the environment (optional, can be no-op). + +**Args:** +- `output_dir`: Directory for persistent environment setup + diff --git a/docs/python-sdk/fastmcp-utilities-mcp_server_config-v1-environments-uv.mdx b/docs/python-sdk/fastmcp-utilities-mcp_server_config-v1-environments-uv.mdx new file mode 100644 index 000000000..cd738d0f5 --- /dev/null +++ b/docs/python-sdk/fastmcp-utilities-mcp_server_config-v1-environments-uv.mdx @@ -0,0 +1,45 @@ +--- +title: uv +sidebarTitle: uv +--- + +# `fastmcp.utilities.mcp_server_config.v1.environments.uv` + +## Classes + +### `UVEnvironment` + + +Configuration for Python environment setup. + + +**Methods:** + +#### `build_command` + +```python +build_command(self, command: list[str]) -> list[str] +``` + +Build complete uv run command with environment args and command to execute. + +**Args:** +- `command`: Command to execute (e.g., ["fastmcp", "run", "server.py"]) + +**Returns:** +- Complete command ready for subprocess.run, including "uv" prefix if needed. +- If no environment configuration is set, returns the command unchanged. + + +#### `prepare` + +```python +prepare(self, output_dir: Path | None = None) -> None +``` + +Prepare the Python environment using uv. + +**Args:** +- `output_dir`: Directory where the persistent uv project will be created. + If None, creates a temporary directory for ephemeral use. + diff --git a/docs/python-sdk/fastmcp-utilities-mcp_server_config-v1-mcp_server_config.mdx b/docs/python-sdk/fastmcp-utilities-mcp_server_config-v1-mcp_server_config.mdx new file mode 100644 index 000000000..391759bc0 --- /dev/null +++ b/docs/python-sdk/fastmcp-utilities-mcp_server_config-v1-mcp_server_config.mdx @@ -0,0 +1,235 @@ +--- +title: mcp_server_config +sidebarTitle: mcp_server_config +--- + +# `fastmcp.utilities.mcp_server_config.v1.mcp_server_config` + + +FastMCP Configuration File Support. + +This module provides support for fastmcp.json configuration files that allow +users to specify server settings in a declarative format instead of using +command-line arguments. + + +## Functions + +### `generate_schema` + +```python +generate_schema(output_path: Path | str | None = None) -> dict[str, Any] | None +``` + + +Generate JSON schema for fastmcp.json files. + +This is used to create the schema file that IDEs can use for +validation and auto-completion. + +**Args:** +- `output_path`: Optional path to write the schema to. If provided, + writes the schema and returns None. If not provided, + returns the schema as a dictionary. + +**Returns:** +- JSON schema as a dictionary if output_path is None, otherwise None + + +## Classes + +### `Deployment` + + +Configuration for server deployment and runtime settings. + + +**Methods:** + +#### `apply_runtime_settings` + +```python +apply_runtime_settings(self, config_path: Path | None = None) -> None +``` + +Apply runtime settings like environment variables and working directory. + +**Args:** +- `config_path`: Path to config file for resolving relative paths + +Environment variables support interpolation with ${VAR_NAME} syntax. +For example: "API_URL": "https://api.${ENVIRONMENT}.example.com" +will substitute the value of the ENVIRONMENT variable at runtime. + + +### `MCPServerConfig` + + +Configuration for a FastMCP server. + +This configuration file allows you to specify all settings needed to run +a FastMCP server in a declarative format. + + +**Methods:** + +#### `validate_source` + +```python +validate_source(cls, v: dict | Source) -> SourceType +``` + +Validate and convert source to proper format. + +Supports: +- Dict format: `{"path": "server.py", "entrypoint": "app"}` +- FileSystemSource instance (passed through) + +No string parsing happens here - that's only at CLI boundaries. +MCPServerConfig works only with properly typed objects. + + +#### `validate_environment` + +```python +validate_environment(cls, v: dict | Any) -> EnvironmentType +``` + +Ensure environment has a type field for discrimination. + +For backward compatibility, if no type is specified, default to "uv". + + +#### `validate_deployment` + +```python +validate_deployment(cls, v: dict | Deployment) -> Deployment +``` + +Validate and convert deployment to Deployment. + +Accepts: +- Deployment instance +- dict that can be converted to Deployment + + +#### `from_file` + +```python +from_file(cls, file_path: Path) -> MCPServerConfig +``` + +Load configuration from a JSON file. + +**Args:** +- `file_path`: Path to the configuration file + +**Returns:** +- MCPServerConfig instance + +**Raises:** +- `FileNotFoundError`: If the file doesn't exist +- `json.JSONDecodeError`: If the file is not valid JSON +- `pydantic.ValidationError`: If the configuration is invalid + + +#### `from_cli_args` + +```python +from_cli_args(cls, source: FileSystemSource, transport: Literal['stdio', 'http', 'sse', 'streamable-http'] | None = None, host: str | None = None, port: int | None = None, path: str | None = None, log_level: Literal['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'] | None = None, python: str | None = None, dependencies: list[str] | None = None, requirements: str | None = None, project: str | None = None, editable: str | None = None, env: dict[str, str] | None = None, cwd: str | None = None, args: list[str] | None = None) -> MCPServerConfig +``` + +Create a config from CLI arguments. + +This allows us to have a single code path where everything +goes through a config object. + +**Args:** +- `source`: Server source (FileSystemSource instance) +- `transport`: Transport protocol +- `host`: Host for HTTP transport +- `port`: Port for HTTP transport +- `path`: URL path for server +- `log_level`: Logging level +- `python`: Python version +- `dependencies`: Python packages to install +- `requirements`: Path to requirements file +- `project`: Path to project directory +- `editable`: Path to install in editable mode +- `env`: Environment variables +- `cwd`: Working directory +- `args`: Server arguments + +**Returns:** +- MCPServerConfig instance + + +#### `find_config` + +```python +find_config(cls, start_path: Path | None = None) -> Path | None +``` + +Find a fastmcp.json file in the specified directory. + +**Args:** +- `start_path`: Directory to look in (defaults to current directory) + +**Returns:** +- Path to the configuration file, or None if not found + + +#### `prepare` + +```python +prepare(self, skip_source: bool = False, output_dir: Path | None = None) -> None +``` + +Prepare environment and source for execution. + +When output_dir is provided, creates a persistent uv project. +When output_dir is None, does ephemeral caching (for backwards compatibility). + +**Args:** +- `skip_source`: Skip source preparation if True +- `output_dir`: Directory to create the persistent uv project in (optional) + + +#### `prepare_environment` + +```python +prepare_environment(self, output_dir: Path | None = None) -> None +``` + +Prepare the Python environment. + +**Args:** +- `output_dir`: If provided, creates a persistent uv project in this directory. + If None, just populates uv's cache for ephemeral use. + +Delegates to the environment's prepare() method + + +#### `prepare_source` + +```python +prepare_source(self) -> None +``` + +Prepare the source for loading. + +Delegates to the source's prepare() method. + + +#### `run_server` + +```python +run_server(self, **kwargs: Any) -> None +``` + +Load and run the server with this configuration. + +**Args:** +- `**kwargs`: Additional arguments to pass to server.run_async() + These override config settings + diff --git a/docs/python-sdk/fastmcp-utilities-mcp_server_config-v1-sources-__init__.mdx b/docs/python-sdk/fastmcp-utilities-mcp_server_config-v1-sources-__init__.mdx new file mode 100644 index 000000000..38d102b15 --- /dev/null +++ b/docs/python-sdk/fastmcp-utilities-mcp_server_config-v1-sources-__init__.mdx @@ -0,0 +1,8 @@ +--- +title: __init__ +sidebarTitle: __init__ +--- + +# `fastmcp.utilities.mcp_server_config.v1.sources` + +*This module is empty or contains only private/internal implementations.* diff --git a/docs/python-sdk/fastmcp-utilities-mcp_server_config-v1-sources-base.mdx b/docs/python-sdk/fastmcp-utilities-mcp_server_config-v1-sources-base.mdx new file mode 100644 index 000000000..0a2aa84ac --- /dev/null +++ b/docs/python-sdk/fastmcp-utilities-mcp_server_config-v1-sources-base.mdx @@ -0,0 +1,42 @@ +--- +title: base +sidebarTitle: base +--- + +# `fastmcp.utilities.mcp_server_config.v1.sources.base` + +## Classes + +### `Source` + + +Abstract base class for all source types. + + +**Methods:** + +#### `prepare` + +```python +prepare(self) -> None +``` + +Prepare the source (download, clone, install, etc). + +For sources that need preparation (e.g., git clone, download), +this method performs that preparation. For sources that don't +need preparation (e.g., local files), this is a no-op. + + +#### `load_server` + +```python +load_server(self) -> Any +``` + +Load and return the FastMCP server instance. + +Must be called after prepare() if the source requires preparation. +All information needed to load the server should be available +as attributes on the source instance. + diff --git a/docs/python-sdk/fastmcp-utilities-mcp_server_config-v1-sources-filesystem.mdx b/docs/python-sdk/fastmcp-utilities-mcp_server_config-v1-sources-filesystem.mdx new file mode 100644 index 000000000..b557613c8 --- /dev/null +++ b/docs/python-sdk/fastmcp-utilities-mcp_server_config-v1-sources-filesystem.mdx @@ -0,0 +1,37 @@ +--- +title: filesystem +sidebarTitle: filesystem +--- + +# `fastmcp.utilities.mcp_server_config.v1.sources.filesystem` + +## Classes + +### `FileSystemSource` + + +Source for local Python files. + + +**Methods:** + +#### `parse_path_with_object` + +```python +parse_path_with_object(cls, v: str) -> str +``` + +Parse path:object syntax and extract the object name. + +This validator runs before the model is created, allowing us to +handle the "file.py:object" syntax at the model boundary. + + +#### `load_server` + +```python +load_server(self) -> Any +``` + +Load server from filesystem. + diff --git a/docs/python-sdk/fastmcp-utilities-openapi.mdx b/docs/python-sdk/fastmcp-utilities-openapi.mdx new file mode 100644 index 000000000..745f6fd18 --- /dev/null +++ b/docs/python-sdk/fastmcp-utilities-openapi.mdx @@ -0,0 +1,180 @@ +--- +title: openapi +sidebarTitle: openapi +--- + +# `fastmcp.utilities.openapi` + +## Functions + +### `format_array_parameter` + +```python +format_array_parameter(values: list, parameter_name: str, is_query_parameter: bool = False) -> str | list +``` + + +Format an array parameter according to OpenAPI specifications. + +**Args:** +- `values`: List of values to format +- `parameter_name`: Name of the parameter (for error messages) +- `is_query_parameter`: If True, can return list for explode=True behavior + +**Returns:** +- String (comma-separated) or list (for query params with explode=True) + + +### `format_deep_object_parameter` + +```python +format_deep_object_parameter(param_value: dict, parameter_name: str) -> dict[str, str] +``` + + +Format a dictionary parameter for deepObject style serialization. + +According to OpenAPI 3.0 spec, deepObject style with explode=true serializes +object properties as separate query parameters with bracket notation. + +For example: `{"id": "123", "type": "user"}` becomes `param[id]=123¶m[type]=user`. + +**Args:** +- `param_value`: Dictionary value to format +- `parameter_name`: Name of the parameter + +**Returns:** +- Dictionary with bracketed parameter names as keys + + +### `parse_openapi_to_http_routes` + +```python +parse_openapi_to_http_routes(openapi_dict: dict[str, Any]) -> list[HTTPRoute] +``` + + +Parses an OpenAPI schema dictionary into a list of HTTPRoute objects +using the openapi-pydantic library. + +Supports both OpenAPI 3.0.x and 3.1.x versions. + + +### `clean_schema_for_display` + +```python +clean_schema_for_display(schema: JsonSchema | None) -> JsonSchema | None +``` + + +Clean up a schema dictionary for display by removing internal/complex fields. + + +### `generate_example_from_schema` + +```python +generate_example_from_schema(schema: JsonSchema | None) -> Any +``` + + +Generate a simple example value from a JSON schema dictionary. +Very basic implementation focusing on types. + + +### `format_json_for_description` + +```python +format_json_for_description(data: Any, indent: int = 2) -> str +``` + + +Formats Python data as a JSON string block for markdown. + + +### `format_description_with_responses` + +```python +format_description_with_responses(base_description: str, responses: dict[str, Any], parameters: list[ParameterInfo] | None = None, request_body: RequestBodyInfo | None = None) -> str +``` + + +Formats the base description string with response, parameter, and request body information. + +**Args:** +- `base_description`: The initial description to be formatted. +- `responses`: A dictionary of response information, keyed by status code. +- `parameters`: A list of parameter information, +including path and query parameters. Each parameter includes details such as name, +location, whether it is required, and a description. +- `request_body`: Information about the request body, +including its description, whether it is required, and its content schema. + +**Returns:** +- The formatted description string with additional details about responses, parameters, +- and the request body. + + +### `extract_output_schema_from_responses` + +```python +extract_output_schema_from_responses(responses: dict[str, ResponseInfo], schema_definitions: dict[str, Any] | None = None, openapi_version: str | None = None) -> dict[str, Any] | None +``` + + +Extract output schema from OpenAPI responses for use as MCP tool output schema. + +This function finds the first successful response (200, 201, 202, 204) with a +JSON-compatible content type and extracts its schema. If the schema is not an +object type, it wraps it to comply with MCP requirements. + +**Args:** +- `responses`: Dictionary of ResponseInfo objects keyed by status code +- `schema_definitions`: Optional schema definitions to include in the output schema +- `openapi_version`: OpenAPI version string, used to optimize nullable field handling + +**Returns:** +- MCP-compliant output schema with potential wrapping, or None if no suitable schema found + + +## Classes + +### `ParameterInfo` + + +Represents a single parameter for an HTTP operation in our IR. + + +### `RequestBodyInfo` + + +Represents the request body for an HTTP operation in our IR. + + +### `ResponseInfo` + + +Represents response information in our IR. + + +### `HTTPRoute` + + +Intermediate Representation for a single OpenAPI operation. + + +### `OpenAPIParser` + + +Unified parser for OpenAPI schemas with generic type parameters to handle both 3.0 and 3.1. + + +**Methods:** + +#### `parse` + +```python +parse(self) -> list[HTTPRoute] +``` + +Parse the OpenAPI schema into HTTP routes. + diff --git a/docs/python-sdk/fastmcp-utilities-storage.mdx b/docs/python-sdk/fastmcp-utilities-storage.mdx new file mode 100644 index 000000000..0d0ecd322 --- /dev/null +++ b/docs/python-sdk/fastmcp-utilities-storage.mdx @@ -0,0 +1,158 @@ +--- +title: storage +sidebarTitle: storage +--- + +# `fastmcp.utilities.storage` + + +Key-value storage utilities for persistent data management. + +## Classes + +### `KVStorage` + + +Protocol for key-value storage of JSON data. + + +**Methods:** + +#### `get` + +```python +get(self, key: str) -> dict[str, Any] | None +``` + +Get a JSON dict by key. + + +#### `set` + +```python +set(self, key: str, value: dict[str, Any]) -> None +``` + +Store a JSON dict by key. + + +#### `delete` + +```python +delete(self, key: str) -> None +``` + +Delete a value by key. + + +### `JSONFileStorage` + + +File-based key-value storage for JSON data with automatic metadata tracking. + +Each key-value pair is stored as a separate JSON file on disk. +Keys are sanitized to be filesystem-safe. + +The storage automatically wraps all data with metadata: +- timestamp: Timestamp when the entry was last written + +**Args:** +- `cache_dir`: Directory for storing JSON files + + +**Methods:** + +#### `get` + +```python +get(self, key: str) -> dict[str, Any] | None +``` + +Get a JSON dict from storage by key. + +**Args:** +- `key`: The key to retrieve + +**Returns:** +- The stored dict or None if not found + + +#### `set` + +```python +set(self, key: str, value: dict[str, Any]) -> None +``` + +Store a JSON dict with metadata. + +**Args:** +- `key`: The key to store under +- `value`: The dict to store + + +#### `delete` + +```python +delete(self, key: str) -> None +``` + +Delete a value from storage. + +**Args:** +- `key`: The key to delete + + +#### `cleanup_old_entries` + +```python +cleanup_old_entries(self, max_age_seconds: int = 30 * 24 * 60 * 60) -> int +``` + +Remove entries older than the specified age. + +Uses the timestamp field to determine age. + +**Args:** +- `max_age_seconds`: Maximum age in seconds (default 30 days) + +**Returns:** +- Number of entries removed + + +### `InMemoryStorage` + + +In-memory key-value storage for JSON data. + +Simple dict-based storage that doesn't persist across restarts. +Useful for testing or environments where file storage isn't available. + + +**Methods:** + +#### `get` + +```python +get(self, key: str) -> dict[str, Any] | None +``` + +Get a JSON dict from memory by key. + + +#### `set` + +```python +set(self, key: str, value: dict[str, Any]) -> None +``` + +Store a JSON dict in memory. + + +#### `delete` + +```python +delete(self, key: str) -> None +``` + +Delete a value from memory. + diff --git a/docs/python-sdk/fastmcp-utilities-tests.mdx b/docs/python-sdk/fastmcp-utilities-tests.mdx new file mode 100644 index 000000000..669755575 --- /dev/null +++ b/docs/python-sdk/fastmcp-utilities-tests.mdx @@ -0,0 +1,85 @@ +--- +title: tests +sidebarTitle: tests +--- + +# `fastmcp.utilities.tests` + +## Functions + +### `temporary_settings` + +```python +temporary_settings(**kwargs: Any) +``` + + +Temporarily override FastMCP setting values. + +**Args:** +- `**kwargs`: The settings to override, including nested settings. + + +### `run_server_in_process` + +```python +run_server_in_process(server_fn: Callable[..., None], *args, **kwargs) -> Generator[str, None, None] +``` + + +Context manager that runs a FastMCP server in a separate process and +returns the server URL. When the context manager is exited, the server process is killed. + +**Args:** +- `server_fn`: The function that runs a FastMCP server. FastMCP servers are +not pickleable, so we need a function that creates and runs one. +- `*args`: Arguments to pass to the server function. +- `provide_host_and_port`: Whether to provide the host and port to the server function as kwargs. +- `host`: Host to bind the server to (default\: "127.0.0.1"). +- `port`: Port to bind the server to (default\: find available port). +- `**kwargs`: Keyword arguments to pass to the server function. + +**Returns:** +- The server URL. + + +### `caplog_for_fastmcp` + +```python +caplog_for_fastmcp(caplog) +``` + + +Context manager to capture logs from FastMCP loggers even when propagation is disabled. + + +## Classes + +### `HeadlessOAuth` + + +OAuth provider that bypasses browser interaction for testing. + +This simulates the complete OAuth flow programmatically by making HTTP requests +instead of opening a browser and running a callback server. Useful for automated testing. + + +**Methods:** + +#### `redirect_handler` + +```python +redirect_handler(self, authorization_url: str) -> None +``` + +Make HTTP request to authorization URL and store response for callback handler. + + +#### `callback_handler` + +```python +callback_handler(self) -> tuple[str, str | None] +``` + +Parse stored response and return (auth_code, state). + diff --git a/docs/python-sdk/fastmcp-utilities-types.mdx b/docs/python-sdk/fastmcp-utilities-types.mdx new file mode 100644 index 000000000..288191eb9 --- /dev/null +++ b/docs/python-sdk/fastmcp-utilities-types.mdx @@ -0,0 +1,147 @@ +--- +title: types +sidebarTitle: types +--- + +# `fastmcp.utilities.types` + + +Common types used across FastMCP. + +## Functions + +### `get_fn_name` + +```python +get_fn_name(fn: Callable[..., Any]) -> str +``` + +### `get_cached_typeadapter` + +```python +get_cached_typeadapter(cls: T) -> TypeAdapter[T] +``` + + +TypeAdapters are heavy objects, and in an application context we'd typically +create them once in a global scope and reuse them as often as possible. +However, this isn't feasible for user-generated functions. Instead, we use a +cache to minimize the cost of creating them as much as possible. + + +### `issubclass_safe` + +```python +issubclass_safe(cls: type, base: type) -> bool +``` + + +Check if cls is a subclass of base, even if cls is a type variable. + + +### `is_class_member_of_type` + +```python +is_class_member_of_type(cls: Any, base: type) -> bool +``` + + +Check if cls is a member of base, even if cls is a type variable. + +Base can be a type, a UnionType, or an Annotated type. Generic types are not +considered members (e.g. T is not a member of list\[T]). + + +### `find_kwarg_by_type` + +```python +find_kwarg_by_type(fn: Callable, kwarg_type: type) -> str | None +``` + + +Find the name of the kwarg that is of type kwarg_type. + +Includes union types that contain the kwarg_type, as well as Annotated types. + + +### `replace_type` + +```python +replace_type(type_, type_map: dict[type, type]) +``` + + +Given a (possibly generic, nested, or otherwise complex) type, replaces all +instances of old_type with new_type. + +This is useful for transforming types when creating tools. + +**Args:** +- `type_`: The type to replace instances of old_type with new_type. +- `old_type`: The type to replace. +- `new_type`: The type to replace old_type with. + +Examples: +```python +>>> replace_type(list[int | bool], {int: str}) +list[str | bool] + +>>> replace_type(list[list[int]], {int: str}) +list[list[str]] +``` + + +## Classes + +### `FastMCPBaseModel` + + +Base model for FastMCP models. + + +### `Image` + + +Helper class for returning images from tools. + + +**Methods:** + +#### `to_image_content` + +```python +to_image_content(self, mime_type: str | None = None, annotations: Annotations | None = None) -> mcp.types.ImageContent +``` + +Convert to MCP ImageContent. + + +### `Audio` + + +Helper class for returning audio from tools. + + +**Methods:** + +#### `to_audio_content` + +```python +to_audio_content(self, mime_type: str | None = None, annotations: Annotations | None = None) -> mcp.types.AudioContent +``` + +### `File` + + +Helper class for returning audio from tools. + + +**Methods:** + +#### `to_resource_content` + +```python +to_resource_content(self, mime_type: str | None = None, annotations: Annotations | None = None) -> mcp.types.EmbeddedResource +``` + +### `ContextSamplingFallbackProtocol` diff --git a/docs/servers/auth/authentication.mdx b/docs/servers/auth/authentication.mdx new file mode 100644 index 000000000..9b5583002 --- /dev/null +++ b/docs/servers/auth/authentication.mdx @@ -0,0 +1,267 @@ +--- +title: Authentication +sidebarTitle: Overview +description: Secure your FastMCP server with flexible authentication patterns, from simple API keys to full OAuth 2.1 integration with external identity providers. +icon: user-shield +tag: NEW +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + + + +Authentication in MCP presents unique challenges that differ from traditional web applications. MCP clients need to discover authentication requirements automatically, negotiate OAuth flows without user intervention, and work seamlessly across different identity providers. FastMCP addresses these challenges by providing authentication patterns that integrate with the MCP protocol while remaining simple to implement and deploy. + + +Authentication applies only to FastMCP's HTTP-based transports (`http` and `sse`). The STDIO transport inherits security from its local execution environment. + + + +**Authentication is rapidly evolving in MCP.** The specification and best practices are changing quickly. FastMCP aims to provide stable, secure patterns that adapt to these changes while keeping your code simple and maintainable. + + +## MCP Authentication Challenges + +Traditional web authentication assumes a human user with a browser who can interact with login forms and consent screens. MCP clients are often automated systems that need to authenticate without human intervention. This creates several unique requirements: + +**Automatic Discovery**: MCP clients must discover authentication requirements by examining server metadata rather than encountering login redirects. + +**Programmatic OAuth**: OAuth flows must work without human interaction, relying on pre-configured credentials or Dynamic Client Registration. + +**Token Management**: Clients need to obtain, refresh, and manage tokens automatically across multiple MCP servers. + +**Protocol Integration**: Authentication must integrate cleanly with MCP's transport mechanisms and error handling. + +These challenges mean that not all authentication approaches work well with MCP. The patterns that do work fall into three categories based on the level of authentication responsibility your server assumes. + +## Authentication Responsibility + +Authentication responsibility exists on a spectrum. Your MCP server can validate tokens created elsewhere, coordinate with external identity providers, or handle the complete authentication lifecycle internally. Each approach involves different trade-offs between simplicity, security, and control. + +### Token Validation + +Your server validates tokens but delegates their creation to external systems. This approach treats your MCP server as a pure resource server that trusts tokens signed by known issuers. + +Token validation works well when you already have authentication infrastructure that can issue structured tokens like JWTs. Your existing API gateway, microservices platform, or enterprise SSO system becomes the source of truth for user identity, while your MCP server focuses on its core functionality. + +The key insight is that token validation separates authentication (proving who you are) from authorization (determining what you can do). Your MCP server receives proof of identity in the form of a signed token and makes access decisions based on the claims within that token. + +This pattern excels in microservices architectures where multiple services need to validate the same tokens, or when integrating MCP servers into existing systems that already handle user authentication. + +### External Identity Providers + +Your server coordinates with established identity providers to create seamless authentication experiences for MCP clients. This approach leverages OAuth 2.0 and OpenID Connect protocols to delegate user authentication while maintaining control over authorization decisions. + +External identity providers handle the complex aspects of authentication: user credential verification, multi-factor authentication, account recovery, and security monitoring. Your MCP server receives tokens from these trusted providers and validates them using the provider's public keys. + +The MCP protocol's support for Dynamic Client Registration makes this pattern particularly powerful. MCP clients can automatically discover your authentication requirements and register themselves with your identity provider without manual configuration. + +This approach works best for production applications that need enterprise-grade authentication features without the complexity of building them from scratch. It scales well across multiple applications and provides consistent user experiences. + +### Full OAuth Implementation + +Your server implements a complete OAuth 2.0 authorization server, handling everything from user credential verification to token lifecycle management. This approach provides maximum control at the cost of significant complexity. + +Full OAuth implementation means building user interfaces for login and consent, implementing secure credential storage, managing token lifecycles, and maintaining ongoing security updates. The complexity extends beyond initial implementation to include threat monitoring, compliance requirements, and keeping pace with evolving security best practices. + +This pattern makes sense only when you need complete control over the authentication process, operate in air-gapped environments, or have specialized requirements that external providers cannot meet. + +## FastMCP Authentication Providers + +FastMCP translates these authentication responsibility levels into a variety of concrete classes that handle the complexities of MCP protocol integration. You can build on these classes to handle the complexities of MCP protocol integration. + +### TokenVerifier + +`TokenVerifier` provides pure token validation without OAuth metadata endpoints. This class focuses on the essential task of determining whether a token is valid and extracting authorization information from its claims. + +The implementation handles JWT signature verification, expiration checking, and claim extraction. It validates tokens against known issuers and audiences, ensuring that tokens intended for your server are not accepted by other systems. + +```python +from fastmcp import FastMCP +from fastmcp.server.auth.providers.jwt import JWTVerifier + +auth = JWTVerifier( + jwks_uri="https://your-auth-system.com/.well-known/jwks.json", + issuer="https://your-auth-system.com", + audience="your-mcp-server" +) + +mcp = FastMCP(name="Protected Server", auth=auth) +``` + +This example configures token validation against a JWT issuer. The `JWTVerifier` will fetch public keys from the JWKS endpoint and validate incoming tokens against those keys. Only tokens with the correct issuer and audience claims will be accepted. + +`TokenVerifier` works well when you control both the token issuer and your MCP server, or when integrating with existing JWT-based infrastructure. + +โ†’ **Complete guide**: [Token Verification](/servers/auth/token-verification) + +### RemoteAuthProvider + +`RemoteAuthProvider` enables authentication with identity providers that **support Dynamic Client Registration (DCR)**, such as Descope and WorkOS AuthKit. With DCR, MCP clients can automatically register themselves with the identity provider and obtain credentials without any manual configuration. + +This class combines token validation with OAuth discovery metadata. It extends `TokenVerifier` functionality by adding OAuth 2.0 protected resource endpoints that advertise your authentication requirements. MCP clients examine these endpoints to understand which identity providers you trust and how to obtain valid tokens. + +The key requirement is that your identity provider must support DCR - the ability for clients to dynamically register and obtain credentials. This is what enables the seamless, automated authentication flow that MCP requires. + +For example, the built-in `AuthKitProvider` uses WorkOS AuthKit, which fully supports DCR: + +```python +from fastmcp import FastMCP +from fastmcp.server.auth.providers.workos import AuthKitProvider + +auth = AuthKitProvider( + authkit_domain="https://your-project.authkit.app", + base_url="https://your-fastmcp-server.com" +) + +mcp = FastMCP(name="Enterprise Server", auth=auth) +``` + +This example uses WorkOS AuthKit as the external identity provider. The `AuthKitProvider` automatically configures token validation against WorkOS and provides the OAuth metadata that MCP clients need for automatic authentication. + +`RemoteAuthProvider` is ideal for production applications when your identity provider supports Dynamic Client Registration (DCR). This enables fully automated authentication without manual client configuration. + +โ†’ **Complete guide**: [Remote OAuth](/servers/auth/remote-oauth) + +### OAuthProxy + + + +`OAuthProxy` enables authentication with OAuth providers that **don't support Dynamic Client Registration (DCR)**, such as GitHub, Google, Azure, AWS, and most traditional enterprise identity systems. + +When identity providers require manual app registration and fixed credentials, `OAuthProxy` bridges the gap. It presents a DCR-compliant interface to MCP clients (accepting any registration request) while using your pre-registered credentials with the upstream provider. The proxy handles the complexity of callback forwarding, enabling dynamic client callbacks to work with providers that require fixed redirect URIs. + +This class solves the fundamental incompatibility between MCP's expectation of dynamic registration and traditional OAuth providers' requirement for manual app registration. + +For example, the built-in `GitHubProvider` extends `OAuthProxy` to work with GitHub's OAuth system: + +```python +from fastmcp import FastMCP +from fastmcp.server.auth.providers.github import GitHubProvider + +auth = GitHubProvider( + client_id="Ov23li...", # Your GitHub OAuth App ID + client_secret="abc123...", # Your GitHub OAuth App Secret + base_url="https://your-server.com" +) + +mcp = FastMCP(name="GitHub-Protected Server", auth=auth) +``` + +This example uses the GitHub provider, which extends `OAuthProxy` with GitHub-specific token validation. The proxy handles the complete OAuth flow while making GitHub's non-DCR authentication work seamlessly with MCP clients. + +`OAuthProxy` is essential when integrating with OAuth providers that don't support DCR. This includes most established providers like GitHub, Google, and Azure, which require manual app registration through their developer consoles. + +โ†’ **Complete guide**: [OAuth Proxy](/servers/auth/oauth-proxy) + +### OAuthProvider + +`OAuthProvider` implements a complete OAuth 2.0 authorization server within your MCP server. This class handles the full authentication lifecycle from user credential verification to token management. + +The implementation provides all required OAuth endpoints including authorization, token, and discovery endpoints. It manages client registration, user consent, and token lifecycle while integrating with your user storage and authentication logic. + +```python +from fastmcp import FastMCP +from fastmcp.server.auth.providers.oauth import MyOAuthProvider + +auth = MyOAuthProvider( + user_store=your_user_database, + client_store=your_client_registry, + # Additional configuration... +) + +mcp = FastMCP(name="Auth Server", auth=auth) +``` + +This example shows the basic structure of a custom OAuth provider. The actual implementation requires significant additional configuration for user management, client registration, and security policies. + +`OAuthProvider` should be used only when you have specific requirements that external providers cannot meet and the expertise to implement OAuth securely. + +โ†’ **Complete guide**: [Full OAuth Server](/servers/auth/full-oauth-server) + +## Configuration Approaches + +FastMCP supports both programmatic configuration for maximum flexibility and environment-based configuration for deployment simplicity. + +### Programmatic Configuration + +Programmatic configuration provides complete control over authentication settings and allows for complex initialization logic. This approach works well during development and when you need to customize authentication behavior based on runtime conditions. + +Authentication providers are instantiated directly in your code with their required parameters. This makes dependencies explicit and allows your IDE to provide helpful autocompletion and type checking. + +### Environment Configuration + + + +Environment-based configuration separates authentication settings from application code, enabling the same codebase to work across different deployment environments without modification. + +FastMCP automatically detects authentication configuration from environment variables when no explicit `auth` parameter is provided. The configuration system supports all authentication providers and their various options. + +#### Provider Configuration + +Authentication providers are configured by specifying the full module path to the provider class: + + +The full module path to the authentication provider class. Examples: +- `fastmcp.server.auth.providers.github.GitHubProvider` - GitHub OAuth +- `fastmcp.server.auth.providers.google.GoogleProvider` - Google OAuth +- `fastmcp.server.auth.providers.jwt.JWTVerifier` - JWT token verification +- `fastmcp.server.auth.providers.workos.WorkOSProvider` - WorkOS OAuth +- `fastmcp.server.auth.providers.workos.AuthKitProvider` - WorkOS AuthKit +- `mycompany.auth.CustomProvider` - Your custom provider class + + +When using providers like GitHub or Google, you'll need to set provider-specific environment variables: + +```bash +# GitHub OAuth +export FASTMCP_SERVER_AUTH=fastmcp.server.auth.providers.github.GitHubProvider +export FASTMCP_SERVER_AUTH_GITHUB_CLIENT_ID="Ov23li..." +export FASTMCP_SERVER_AUTH_GITHUB_CLIENT_SECRET="github_pat_..." + +# Google OAuth +export FASTMCP_SERVER_AUTH=fastmcp.server.auth.providers.google.GoogleProvider +export FASTMCP_SERVER_AUTH_GOOGLE_CLIENT_ID="123456.apps.googleusercontent.com" +export FASTMCP_SERVER_AUTH_GOOGLE_CLIENT_SECRET="GOCSPX-..." +``` + +#### Provider-Specific Configuration + +Each provider has its own configuration options set through environment variables: + +```bash +# JWT Token Verification +export FASTMCP_SERVER_AUTH=fastmcp.server.auth.providers.jwt.JWTVerifier +export FASTMCP_SERVER_AUTH_JWT_JWKS_URI="https://auth.example.com/jwks" +export FASTMCP_SERVER_AUTH_JWT_ISSUER="https://auth.example.com" +export FASTMCP_SERVER_AUTH_JWT_AUDIENCE="mcp-server" + +# Custom Provider +export FASTMCP_SERVER_AUTH=mycompany.auth.CustomProvider +# Plus any environment variables your custom provider expects +``` + +With these environment variables set, creating an authenticated FastMCP server requires no additional configuration: + +```python +from fastmcp import FastMCP + +# Authentication automatically configured from environment +mcp = FastMCP(name="My Server") +``` + +This approach simplifies deployment pipelines and follows twelve-factor app principles for configuration management. + +## Choosing Your Implementation + +The authentication approach you choose depends on your existing infrastructure, security requirements, and operational constraints. + +**For OAuth providers without DCR support (GitHub, Google, Azure, AWS, most enterprise systems), use OAuth Proxy.** These providers require manual app registration through their developer consoles. OAuth Proxy bridges the gap by presenting a DCR-compliant interface to MCP clients while using your fixed credentials with the provider. The proxy's callback forwarding pattern enables dynamic client ports to work with providers that require fixed redirect URIs. + +**For identity providers with DCR support (Descope, WorkOS AuthKit, modern auth platforms), use RemoteAuthProvider.** These providers allow clients to dynamically register and obtain credentials without manual configuration. This enables the fully automated authentication flow that MCP is designed for, providing the best user experience and simplest implementation. + +**Token validation works well when you already have authentication infrastructure that issues structured tokens.** If your organization already uses JWT-based systems, API gateways, or enterprise SSO that can generate tokens, this approach integrates seamlessly while keeping your MCP server focused on its core functionality. The simplicity comes from leveraging existing investment in authentication infrastructure. + +**Full OAuth implementation should be avoided unless you have compelling reasons that external providers cannot address.** Air-gapped environments, specialized compliance requirements, or unique organizational constraints might justify this approach, but it requires significant security expertise and ongoing maintenance commitment. The complexity extends far beyond initial implementation to include threat monitoring, security updates, and keeping pace with evolving attack vectors. + +FastMCP's architecture supports migration between these approaches as your requirements evolve. You can integrate with existing token systems initially and migrate to external identity providers as your application scales, or implement custom solutions when your requirements outgrow standard patterns. \ No newline at end of file diff --git a/docs/servers/auth/full-oauth-server.mdx b/docs/servers/auth/full-oauth-server.mdx new file mode 100644 index 000000000..da6b81519 --- /dev/null +++ b/docs/servers/auth/full-oauth-server.mdx @@ -0,0 +1,229 @@ +--- +title: Full OAuth Server +sidebarTitle: Full OAuth Server +description: Build a self-contained authentication system where your FastMCP server manages users, issues tokens, and validates them. +icon: users-between-lines +tag: NEW +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + + + + +**This is an extremely advanced pattern that most users should avoid.** Building a secure OAuth 2.1 server requires deep expertise in authentication protocols, cryptography, and security best practices. The complexity extends far beyond initial implementation to include ongoing security monitoring, threat response, and compliance maintenance. + +**Use [Remote OAuth](/servers/auth/remote-oauth) instead** unless you have compelling requirements that external identity providers cannot meet, such as air-gapped environments or specialized compliance needs. + + +The Full OAuth Server pattern exists to support the MCP protocol specification's requirements. Your FastMCP server becomes both an Authorization Server and Resource Server, handling the complete authentication lifecycle from user login to token validation. + +This documentation exists for completeness - the vast majority of applications should use external identity providers instead. + +## OAuthProvider + +FastMCP provides the `OAuthProvider` abstract class that implements the OAuth 2.1 specification. To use this pattern, you must subclass `OAuthProvider` and implement all required abstract methods. + + +`OAuthProvider` handles OAuth endpoints, protocol flows, and security requirements, but delegates all storage, user management, and business logic to your implementation of the abstract methods. + + +## Required Implementation + +You must implement these abstract methods to create a functioning OAuth server: + +### Client Management + + + + Retrieve client information by ID from your database. + + + + Client identifier to look up + + + + + + Client information object or `None` if client not found + + + + + + Store new client registration information in your database. + + + + Complete client registration information to store + + + + + + No return value + + + + + +### Authorization Flow + + + + Handle authorization request and return redirect URL. Must implement user authentication and consent collection. + + + + OAuth client making the authorization request + + + Authorization request parameters from the client + + + + + + Redirect URL to send the client to + + + + + + Load authorization code from storage by code string. Return `None` if code is invalid or expired. + + + + OAuth client attempting to use the authorization code + + + Authorization code string to look up + + + + + + Authorization code object or `None` if not found + + + + + +### Token Management + + + + Exchange authorization code for access and refresh tokens. Must validate code and create new tokens. + + + + OAuth client exchanging the authorization code + + + Valid authorization code object to exchange + + + + + + New OAuth token containing access and refresh tokens + + + + + + Load refresh token from storage by token string. Return `None` if token is invalid or expired. + + + + OAuth client attempting to use the refresh token + + + Refresh token string to look up + + + + + + Refresh token object or `None` if not found + + + + + + Exchange refresh token for new access/refresh token pair. Must validate scopes and token. + + + + OAuth client using the refresh token + + + Valid refresh token object to exchange + + + Requested scopes for the new access token + + + + + + New OAuth token with updated access and refresh tokens + + + + + + Load an access token by its token string. + + + + The access token to verify + + + + + + The access token object, or `None` if the token is invalid + + + + + + Revoke access or refresh token, marking it as invalid in storage. + + + + Token object to revoke and mark invalid + + + + + + No return value + + + + + + Verify bearer token for incoming requests. Return `AccessToken` if valid, `None` if invalid. + + + + Bearer token string from incoming request + + + + + + Access token object if valid, `None` if invalid or expired + + + + + +Each method must handle storage, validation, security, and error cases according to the OAuth 2.1 specification. The implementation complexity is substantial and requires expertise in OAuth security considerations. + + +**Security Notice:** OAuth server implementation involves numerous security considerations including PKCE, state parameters, redirect URI validation, token binding, replay attack prevention, and secure storage requirements. Mistakes can lead to serious security vulnerabilities. + \ No newline at end of file diff --git a/docs/servers/auth/oauth-proxy.mdx b/docs/servers/auth/oauth-proxy.mdx new file mode 100644 index 000000000..a5109ea0a --- /dev/null +++ b/docs/servers/auth/oauth-proxy.mdx @@ -0,0 +1,418 @@ +--- +title: OAuth Proxy +sidebarTitle: OAuth Proxy +description: Bridge traditional OAuth providers to work seamlessly with MCP's authentication flow. +icon: share +tag: NEW +--- + +import { VersionBadge } from "/snippets/version-badge.mdx"; + + + +OAuth Proxy enables FastMCP servers to authenticate with OAuth providers that **don't support Dynamic Client Registration (DCR)**. This includes virtually all traditional OAuth providers: GitHub, Google, Azure, AWS, Discord, Facebook, and most enterprise identity systems. For providers that do support DCR (like Descope and WorkOS AuthKit), use [`RemoteAuthProvider`](/servers/auth/remote-oauth) instead. + +MCP clients expect to register automatically and obtain credentials on the fly, but traditional providers require manual app registration through their developer consoles. OAuth Proxy bridges this gap by presenting a DCR-compliant interface to MCP clients while using your pre-registered credentials with the upstream provider. When a client attempts to register, the proxy returns your fixed credentials. When a client initiates authorization, the proxy handles the complexity of callback forwardingโ€”storing the client's dynamic callback URL, using its own fixed callback with the provider, then forwarding back to the client after token exchange. + +This approach enables any MCP client (whether using random localhost ports or fixed URLs like Claude.ai) to authenticate with any traditional OAuth provider, all while maintaining full OAuth 2.1 and PKCE security. + + + For providers that support OIDC discovery (Auth0, Google with OIDC + configuration, Azure AD), consider using [`OIDC + Proxy`](/servers/auth/oidc-proxy) for automatic configuration. OIDC Proxy + extends OAuth Proxy to automatically discover endpoints from the provider's + `/.well-known/openid-configuration` URL, simplifying setup. + + +## Implementation + +### Provider Setup Requirements + +Before using OAuth Proxy, you need to register your application with your OAuth provider: + +1. **Register your application** in the provider's developer console (GitHub Settings, Google Cloud Console, Azure Portal, etc.) +2. **Configure the redirect URI** as your FastMCP server URL plus your chosen callback path: + - Default: `https://your-server.com/auth/callback` + - Custom: `https://your-server.com/your/custom/path` (if you set `redirect_path`) + - Development: `http://localhost:8000/auth/callback` +3. **Obtain your credentials**: Client ID and Client Secret +4. **Note the OAuth endpoints**: Authorization URL and Token URL (usually found in the provider's OAuth documentation) + + + The redirect URI you configure with your provider must exactly match your + FastMCP server's URL plus the callback path. If you customize `redirect_path` + in OAuth Proxy, update your provider's redirect URI accordingly. + + +### Basic Setup + +Here's how to implement OAuth Proxy with any provider: + +```python +from fastmcp import FastMCP +from fastmcp.server.auth import OAuthProxy +from fastmcp.server.auth.providers.jwt import JWTVerifier + +# Configure token verification for your provider +# See the Token Verification guide for provider-specific setups +token_verifier = JWTVerifier( + jwks_uri="https://your-provider.com/.well-known/jwks.json", + issuer="https://your-provider.com", + audience="your-app-id" +) + +# Create the OAuth proxy +auth = OAuthProxy( + # Provider's OAuth endpoints (from their documentation) + upstream_authorization_endpoint="https://provider.com/oauth/authorize", + upstream_token_endpoint="https://provider.com/oauth/token", + + # Your registered app credentials + upstream_client_id="your-client-id", + upstream_client_secret="your-client-secret", + + # Token validation (see Token Verification guide) + token_verifier=token_verifier, + + # Your FastMCP server's public URL + base_url="https://your-server.com", + + # Optional: customize the callback path (default is "/auth/callback") + # redirect_path="/custom/callback", +) + +mcp = FastMCP(name="My Server", auth=auth) +``` + +### Configuration Parameters + + + + URL of your OAuth provider's authorization endpoint (e.g., `https://github.com/login/oauth/authorize`) + + + + URL of your OAuth provider's token endpoint (e.g., + `https://github.com/login/oauth/access_token`) + + + + Client ID from your registered OAuth application + + + + Client secret from your registered OAuth application + + + + A [`TokenVerifier`](/servers/auth/token-verification) instance to validate the + provider's tokens + + + + Public URL of your FastMCP server (e.g., `https://your-server.com`) + + + + Path for OAuth callbacks. Must match the redirect URI configured in your OAuth + application + + + + Optional URL of provider's token revocation endpoint + + + + Issuer URL for OAuth metadata (defaults to base_url) + + + + Optional URL to your service documentation + + + + Whether to forward PKCE (Proof Key for Code Exchange) to the upstream OAuth + provider. When enabled and the client uses PKCE, the proxy generates its own + PKCE parameters to send upstream while separately validating the client's + PKCE. This ensures end-to-end PKCE security at both layers (client-to-proxy + and proxy-to-upstream). - `True` (default): Forward PKCE for providers that + support it (Google, Azure, AWS, GitHub, etc.) - `False`: Disable only if upstream + provider doesn't support PKCE + + + + Token endpoint authentication method for the upstream OAuth server. Controls + how the proxy authenticates when exchanging authorization codes and refresh + tokens with the upstream provider. - `"client_secret_basic"`: Send credentials + in Authorization header (most common) - `"client_secret_post"`: Send + credentials in request body (required by some providers) - `"none"`: No + authentication (for public clients) - `None` (default): Uses authlib's default + (typically `"client_secret_basic"`) Set this if your provider requires a + specific authentication method and the default doesn't work. + + + + List of allowed redirect URI patterns for MCP clients. Patterns support + wildcards (e.g., `"http://localhost:*"`, `"https://*.example.com/*"`). - + `None` (default): All redirect URIs allowed (for MCP/DCR compatibility) - + Empty list `[]`: No redirect URIs allowed - Custom list: Only matching + patterns allowed These patterns apply to MCP client loopback redirects, NOT + the upstream OAuth app redirect URI. + + + + List of all possible valid scopes for the OAuth provider. These are advertised + to clients through the `/.well-known` endpoints. Defaults to `required_scopes` + from your TokenVerifier if not specified. + + + + Additional parameters to forward to the upstream authorization endpoint. Useful for provider-specific parameters that aren't part of the standard OAuth2 flow. + + For example, Auth0 requires an `audience` parameter to issue JWT tokens: + ```python + extra_authorize_params={"audience": "https://api.example.com"} + ``` + + These parameters are added to every authorization request sent to the upstream provider. + + + + Additional parameters to forward to the upstream token endpoint during code exchange and token refresh. Useful for provider-specific requirements during token operations. + +For example, some providers require additional context during token exchange: + +```python +extra_token_params={"audience": "https://api.example.com"} +``` + +These parameters are included in all token requests to the upstream provider. + + + + + Storage backend for persisting OAuth client registrations. By default, clients are automatically persisted to disk in `~/.config/fastmcp/oauth-proxy-clients/`, allowing them to survive server restarts as long as the filesystem remains accessible. This means MCP clients only need to register once and can reconnect seamlessly after your server restarts. + +```python +from fastmcp.utilities.storage import InMemoryStorage + +# Use in-memory storage for testing (clients lost on restart) +auth = OAuthProxy(..., client_storage=InMemoryStorage()) +``` + + + + +### Provider-Specific Parameters + +Some OAuth providers require additional parameters beyond the standard OAuth2 flow. Use `extra_authorize_params` and `extra_token_params` to handle these requirements: + +#### Auth0 Example + +Auth0 requires an `audience` parameter to issue JWT tokens instead of opaque tokens: + +```python +auth = OAuthProxy( + upstream_authorization_endpoint="https://your-domain.auth0.com/authorize", + upstream_token_endpoint="https://your-domain.auth0.com/oauth/token", + upstream_client_id="your-auth0-client-id", + upstream_client_secret="your-auth0-client-secret", + + # Auth0 requires audience for JWT tokens + extra_authorize_params={ + "audience": "https://your-api-identifier.com" + }, + extra_token_params={ + "audience": "https://your-api-identifier.com" + }, + + token_verifier=JWTVerifier( + jwks_uri="https://your-domain.auth0.com/.well-known/jwks.json", + issuer="https://your-domain.auth0.com/", + audience="https://your-api-identifier.com" + ), + + base_url="https://your-server.com" +) +``` + +#### RFC 8707 Resource Indicators + +MCP clients can specify target resources using the standard `resource` parameter (RFC 8707). This is automatically forwarded when present: + +```python +# Client code (automatic - no server configuration needed) +# The resource parameter is passed through from AuthorizationParams +``` + +### Using Built-in Providers + +FastMCP includes pre-configured providers for common services: + +```python +from fastmcp.server.auth.providers.github import GitHubProvider + +auth = GitHubProvider( + client_id="your-github-app-id", + client_secret="your-github-app-secret", + base_url="https://your-server.com" +) + +mcp = FastMCP(name="My Server", auth=auth) +``` + +Available providers include `GitHubProvider`, `GoogleProvider`, and others. These handle token verification automatically. + +### Scope Configuration + +OAuth scopes are configured through your `TokenVerifier`. Set `required_scopes` to automatically request the permissions your application needs: + +```python +JWTVerifier(..., required_scopes = ["read:user", "write:data"]) +``` + +Dynamic clients created by the proxy will automatically include these scopes in their authorization requests. + +## How It Works + +```mermaid +sequenceDiagram + participant Client as MCP Client
(localhost:random) + participant Proxy as FastMCP OAuth Proxy
(server:8000) + participant Provider as OAuth Provider
(GitHub, etc.) + + Note over Client, Proxy: Dynamic Registration (Local) + Client->>Proxy: 1. POST /register
redirect_uri: localhost:54321/callback + Proxy-->>Client: 2. Returns fixed upstream credentials + + Note over Client, Proxy: Authorization with PKCE & Callback Forwarding + Client->>Proxy: 3. GET /authorize
redirect_uri=localhost:54321/callback
code_challenge=CLIENT_CHALLENGE + Note over Proxy: Store transaction with client PKCE
Generate proxy PKCE pair + Proxy->>Provider: 4. Redirect to provider
redirect_uri=server:8000/auth/callback
code_challenge=PROXY_CHALLENGE + + Note over Provider, Proxy: Provider Callback + Provider->>Proxy: 5. GET /auth/callback
with authorization code + Proxy->>Provider: 6. Exchange code for tokens
code_verifier=PROXY_VERIFIER + Provider-->>Proxy: 7. Access & refresh tokens + + Note over Proxy, Client: Client Callback Forwarding + Proxy->>Client: 8. Redirect to localhost:54321/callback
with new authorization code + + Note over Client, Proxy: Token Exchange + Client->>Proxy: 9. POST /token with code
code_verifier=CLIENT_VERIFIER + Proxy-->>Client: 10. Returns stored provider tokens +``` + +The flow diagram above illustrates the complete OAuth Proxy pattern. Let's understand each phase: + +### Registration Phase + +When an MCP client calls `/register` with its dynamic callback URL, the proxy responds with your pre-configured upstream credentials. The client stores these credentials believing it has registered a new app. Meanwhile, the proxy records the client's callback URL for later use. + +### Authorization Phase + +The client initiates OAuth by redirecting to the proxy's `/authorize` endpoint. The proxy: + +1. Stores the client's transaction with its PKCE challenge +2. Generates its own PKCE parameters for upstream security +3. Redirects to the upstream provider using the fixed callback URL + +This dual-PKCE approach maintains end-to-end security at both the client-to-proxy and proxy-to-provider layers. + +### Callback Phase + +After user authorization, the provider redirects back to the proxy's fixed callback URL. The proxy: + +1. Exchanges the authorization code for tokens with the provider +2. Stores these tokens temporarily +3. Generates a new authorization code for the client +4. Redirects to the client's original dynamic callback URL + +### Token Exchange Phase + +Finally, the client exchanges its authorization code with the proxy to receive the provider's tokens. The proxy validates the client's PKCE verifier before returning the stored tokens. + +This entire flow is transparent to the MCP clientโ€”it experiences a standard OAuth flow with dynamic registration, unaware that a proxy is managing the complexity behind the scenes. + +### PKCE Forwarding + +OAuth Proxy automatically handles PKCE (Proof Key for Code Exchange) when working with providers that support or require it. The proxy generates its own PKCE parameters to send upstream while separately validating the client's PKCE, ensuring end-to-end security at both layers. + +This is enabled by default via the `forward_pkce` parameter and works seamlessly with providers like Google, Azure AD, and GitHub. Only disable it for legacy providers that don't support PKCE: + +```python +# Disable PKCE forwarding only if upstream doesn't support it +auth = OAuthProxy( + ..., + forward_pkce=False # Default is True +) +``` + +### Redirect URI Validation + +While OAuth Proxy accepts all redirect URIs by default (for DCR compatibility), you can restrict which clients can connect by specifying allowed patterns: + +```python +# Allow only localhost clients (common for development) +auth = OAuthProxy( + # ... other parameters ... + allowed_client_redirect_uris=[ + "http://localhost:*", + "http://127.0.0.1:*" + ] +) + +# Allow specific known clients +auth = OAuthProxy( + # ... other parameters ... + allowed_client_redirect_uris=[ + "http://localhost:*", + "https://claude.ai/api/mcp/auth_callback", + "https://*.mycompany.com/auth/*" # Wildcard patterns supported + ] +) +``` + +Check your server logs for "Client registered with redirect_uri" messages to identify what URLs your clients use. + +## Token Verification + +OAuth Proxy requires a compatible `TokenVerifier` to validate tokens from your provider. Different providers use different token formats: + +- **JWT tokens** (Google, Azure): Use `JWTVerifier` with the provider's JWKS endpoint +- **Opaque tokens** (GitHub, Discord): Use provider-specific verifiers or implement custom validation + +See the [Token Verification guide](/servers/auth/token-verification) for detailed setup instructions for your provider. + +## Environment Configuration + + + +For production deployments, configure OAuth Proxy through environment variables instead of hardcoding credentials: + +```bash +# Specify the provider implementation +export FASTMCP_SERVER_AUTH=fastmcp.server.auth.providers.github.GitHubProvider + +# Provider-specific credentials +export FASTMCP_SERVER_AUTH_GITHUB_CLIENT_ID="Ov23li..." +export FASTMCP_SERVER_AUTH_GITHUB_CLIENT_SECRET="abc123..." +export FASTMCP_SERVER_AUTH_GITHUB_BASE_URL="https://your-production-server.com" +``` + +With environment configuration, your server code simplifies to: + +```python +from fastmcp import FastMCP + +# Authentication automatically configured from environment +mcp = FastMCP(name="My Server") + +@mcp.tool +def protected_tool(data: str) -> str: + """This tool is now protected by OAuth.""" + return f"Processed: {data}" + +if __name__ == "__main__": + mcp.run(transport="http", port=8000) +``` diff --git a/docs/servers/auth/oidc-proxy.mdx b/docs/servers/auth/oidc-proxy.mdx new file mode 100644 index 000000000..e08d0c358 --- /dev/null +++ b/docs/servers/auth/oidc-proxy.mdx @@ -0,0 +1,204 @@ +--- +title: OIDC Proxy +sidebarTitle: OIDC Proxy +description: Bridge OIDC providers to work seamlessly with MCP's authentication flow. +icon: share +tag: NEW +--- + +import { VersionBadge } from "/snippets/version-badge.mdx"; + + + +OIDC Proxy enables FastMCP servers to authenticate with OIDC providers that **don't support Dynamic Client Registration (DCR)** out of the box. This includes OAuth providers like: Auth0, Google, Azure, AWS, etc. For providers that do support DCR (like WorkOS AuthKit), use [`RemoteAuthProvider`](/servers/auth/remote-oauth) instead. + +The OIDC Proxy is built upon [`OAuthProxy`](/servers/auth/oauth-proxy) so it has all the same functionality under the covers. + +## Implementation + +### Provider Setup Requirements + +Before using OIDC Proxy, you need to register your application with your OAuth provider: + +1. **Register your application** in the provider's developer console (Auth0 Applications, Google Cloud Console, Azure Portal, etc.) +2. **Configure the redirect URI** as your FastMCP server URL plus your chosen callback path: + - Default: `https://your-server.com/auth/callback` + - Custom: `https://your-server.com/your/custom/path` (if you set `redirect_path`) + - Development: `http://localhost:8000/auth/callback` +3. **Obtain your credentials**: Client ID and Client Secret + + + The redirect URI you configure with your provider must exactly match your + FastMCP server's URL plus the callback path. If you customize `redirect_path` + in OAuth Proxy, update your provider's redirect URI accordingly. + + +### Basic Setup + +Here's how to implement OIDC Proxy with any provider: + +```python +from fastmcp import FastMCP +from fastmcp.server.auth.oidc_proxy import OIDCProxy + +# Create the OIDC proxy +auth = OIDCProxy( + # Provider's configuration URL + config_url="https://provider.com/.well-known/openid-configuration", + + # Your registered app credentials + client_id="your-client-id", + client_secret="your-client-secret", + + # Your FastMCP server's public URL + base_url="https://your-server.com", + + # Optional: customize the callback path (default is "/auth/callback") + # redirect_path="/custom/callback", +) + +mcp = FastMCP(name="My Server", auth=auth) +``` + +### Configuration Parameters + + + + URL of your OAuth provider's OIDC configuration + + + + Client ID from your registered OAuth application + + + + Client secret from your registered OAuth application + + + + Public URL of your FastMCP server (e.g., `https://your-server.com`) + + + + Strict flag for configuration validation. When True, requires all OIDC + mandatory fields. + + + + Audience parameter for OIDC providers that require it (e.g., Auth0). This is + typically your API identifier. + + + + HTTP request timeout in seconds for fetching OIDC configuration + + + + JWT algorithm to use for token verification (e.g., "RS256"). If not specified, + uses the provider's default. + + + + List of OAuth scopes to request from the provider. These are automatically + included in authorization requests. + + + + Path for OAuth callbacks. Must match the redirect URI configured in your OAuth + application + + + + List of allowed redirect URI patterns for MCP clients. Patterns support wildcards (e.g., `"http://localhost:*"`, `"https://*.example.com/*"`). + - `None` (default): All redirect URIs allowed (for MCP/DCR compatibility) + - Empty list `[]`: No redirect URIs allowed + - Custom list: Only matching patterns allowed + +These patterns apply to MCP client loopback redirects, NOT the upstream OAuth app redirect URI. + + + + + Token endpoint authentication method for the upstream OAuth server. Controls how the proxy authenticates when exchanging authorization codes and refresh tokens with the upstream provider. + - `"client_secret_basic"`: Send credentials in Authorization header (most common) + - `"client_secret_post"`: Send credentials in request body (required by some providers) + - `"none"`: No authentication (for public clients) + - `None` (default): Uses authlib's default (typically `"client_secret_basic"`) + +Set this if your provider requires a specific authentication method and the default doesn't work. + + + + + Storage backend for persisting OAuth client registrations. By default, clients are automatically persisted to disk in `~/.config/fastmcp/oidc-proxy-clients/`, allowing them to survive server restarts as long as the filesystem remains accessible. This means MCP clients only need to register once and can reconnect seamlessly after your server restarts. + +```python +from fastmcp.utilities.storage import InMemoryStorage + +# Use in-memory storage for testing (clients lost on restart) +auth = OIDCProxy(..., client_storage=InMemoryStorage()) +``` + + + + +### Using Built-in Providers + +FastMCP includes pre-configured OIDC providers for common services: + +```python +from fastmcp.server.auth.providers.auth0 import Auth0Provider + +auth = Auth0Provider( + config_url="https://.../.well-known/openid-configuration", + client_id="your-auth0-client-id", + client_secret="your-auth0-client-secret", + audience="https://...", + base_url="https://localhost:8000" +) + +mcp = FastMCP(name="My Server", auth=auth) +``` + +Available providers include `Auth0Provider` at present. + +### Scope Configuration + +OAuth scopes are configured with `required_scopes` to automatically request the permissions your application needs. + +Dynamic clients created by the proxy will automatically include these scopes in their authorization requests. + +## Environment Configuration + + + +For production deployments, configure OIDC Proxy through environment variables instead of hardcoding credentials: + +```bash +# Specify the provider implementation +export FASTMCP_SERVER_AUTH=fastmcp.server.auth.providers.auth0.Auth0Provider + +# Provider-specific credentials +export FASTMCP_SERVER_AUTH_AUTH0_CONFIG_URL=https://.../.well-known/openid-configuration +export FASTMCP_SERVER_AUTH_AUTH0_CLIENT_ID=tv2ObNgaZAWWhhycr7Bz1LU2mxlnsmsB +export FASTMCP_SERVER_AUTH_AUTH0_CLIENT_SECRET=vPYqbjemq... +export FASTMCP_SERVER_AUTH_AUTH0_AUDIENCE=https://... +export FASTMCP_SERVER_AUTH_AUTH0_BASE_URL=https://localhost:8000 +``` + +With environment configuration, your server code simplifies to: + +```python +from fastmcp import FastMCP + +# Authentication automatically configured from environment +mcp = FastMCP(name="My Server") + +@mcp.tool +def protected_tool(data: str) -> str: + """This tool is now protected by OAuth.""" + return f"Processed: {data}" + +if __name__ == "__main__": + mcp.run(transport="http", port=8000) +``` diff --git a/docs/servers/auth/remote-oauth.mdx b/docs/servers/auth/remote-oauth.mdx new file mode 100644 index 000000000..b01aef315 --- /dev/null +++ b/docs/servers/auth/remote-oauth.mdx @@ -0,0 +1,221 @@ +--- +title: Remote OAuth +sidebarTitle: Remote OAuth +description: Integrate your FastMCP server with external identity providers like Descope, WorkOS, Auth0, and corporate SSO systems. +icon: camera-cctv +tag: NEW +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + + + +Remote OAuth integration allows your FastMCP server to leverage external identity providers that **support Dynamic Client Registration (DCR)**. With DCR, MCP clients can automatically register themselves with the identity provider and obtain credentials without any manual configuration. This provides enterprise-grade authentication with fully automated flows, making it ideal for production applications with modern identity providers. + + +**When to use RemoteAuthProvider vs OAuth Proxy:** +- **RemoteAuthProvider**: For providers WITH Dynamic Client Registration (Descope, WorkOS AuthKit, modern OIDC providers) +- **OAuth Proxy**: For providers WITHOUT Dynamic Client Registration (GitHub, Google, Azure, AWS, Discord, etc.) + +RemoteAuthProvider requires DCR support for fully automated client registration and authentication. + + +## DCR-Enabled Providers + +RemoteAuthProvider works with identity providers that support **Dynamic Client Registration (DCR)** - a critical capability that enables automated authentication flows: + +| Feature | DCR Providers (RemoteAuth) | Non-DCR Providers (OAuth Proxy) | +|---------|---------------------------|--------------------------------| +| **Client Registration** | Automatic via API | Manual in provider console | +| **Credentials** | Dynamic per client | Fixed app credentials | +| **Configuration** | Zero client config | Pre-shared credentials | +| **Examples** | Descope, WorkOS AuthKit, modern OIDC | GitHub, Google, Azure | +| **FastMCP Class** | `RemoteAuthProvider` | [`OAuthProxy`](/servers/auth/oauth-proxy) | + +If your provider doesn't support DCR (most traditional OAuth providers), you'll need to use [`OAuth Proxy`](/servers/auth/oauth-proxy) instead, which bridges the gap between MCP's DCR expectations and fixed OAuth credentials. + +## The Remote OAuth Challenge + +Traditional OAuth flows assume human users with web browsers who can interact with login forms, consent screens, and redirects. MCP clients operate differently - they're often automated systems that need to authenticate programmatically without human intervention. + +This creates several unique requirements that standard OAuth implementations don't address well: + +**Automatic Discovery**: MCP clients must discover authentication requirements by examining server metadata rather than encountering HTTP redirects. They need to know which identity provider to use and how to reach it before making any authenticated requests. + +**Programmatic Registration**: Clients need to register themselves with identity providers automatically. Manual client registration doesn't work when clients might be dynamically created tools or services. + +**Seamless Token Management**: Clients must obtain, store, and refresh tokens without user interaction. The authentication flow needs to work in headless environments where no human is available to complete OAuth consent flows. + +**Protocol Integration**: The authentication process must integrate cleanly with MCP's JSON-RPC transport layer and error handling mechanisms. + +These requirements mean that your MCP server needs to do more than just validate tokens - it needs to provide discovery metadata that enables MCP clients to understand and navigate your authentication requirements automatically. + +## MCP Authentication Discovery + +MCP authentication discovery relies on well-known endpoints that clients can examine to understand your authentication requirements. Your server becomes a bridge between MCP clients and your chosen identity provider. + +The core discovery endpoint is `/.well-known/oauth-protected-resource`, which tells clients that your server requires OAuth authentication and identifies the authorization servers you trust. This endpoint contains static metadata that points clients to your identity provider without requiring any dynamic lookups. + +```mermaid +sequenceDiagram + participant Client + participant FastMCPServer as FastMCP Server + participant ExternalIdP as Identity Provider + + Client->>FastMCPServer: 1. GET /.well-known/oauth-protected-resource + FastMCPServer-->>Client: 2. "Use https://my-idp.com for auth" + + note over Client, ExternalIdP: Client goes directly to the IdP + Client->>ExternalIdP: 3. Authenticate & get token via DCR + ExternalIdP-->>Client: 4. Access token + + Client->>FastMCPServer: 5. MCP request with Bearer token + FastMCPServer->>FastMCPServer: 6. Verify token signature + FastMCPServer-->>Client: 7. MCP response +``` + +This flow separates concerns cleanly: your MCP server handles resource protection and token validation, while your identity provider handles user authentication and token issuance. The client coordinates between these systems using standardized OAuth discovery mechanisms. + +## FastMCP Remote Authentication + + + +FastMCP provides `RemoteAuthProvider` to handle the complexities of remote OAuth integration. This class combines token validation capabilities with the OAuth discovery metadata that MCP clients require. + +### RemoteAuthProvider + +`RemoteAuthProvider` works by composing a [`TokenVerifier`](/servers/auth/token-verification) with authorization server information. A `TokenVerifier` is another FastMCP authentication class that focuses solely on token validation - signature verification, expiration checking, and claim extraction. The `RemoteAuthProvider` takes that token validation capability and adds the OAuth discovery endpoints that enable MCP clients to automatically find and authenticate with your identity provider. + +This composition pattern means you can use any token validation strategy (JWT verification, introspection endpoints, custom validation logic) while maintaining consistent OAuth discovery behavior. The separation allows you to change token validation approaches without affecting the client discovery experience. + +The class automatically generates the required OAuth metadata endpoints using the MCP SDK's standardized route creation functions. This ensures compatibility with MCP clients while reducing the implementation complexity for server developers. + +### Basic Implementation + +Most applications can use `RemoteAuthProvider` directly without subclassing. The implementation requires a `TokenVerifier` instance, a list of trusted authorization servers, and your server's URL for metadata generation. + +```python +from fastmcp import FastMCP +from fastmcp.server.auth import RemoteAuthProvider +from fastmcp.server.auth.providers.jwt import JWTVerifier +from pydantic import AnyHttpUrl + +# Configure token validation for your identity provider +token_verifier = JWTVerifier( + jwks_uri="https://auth.yourcompany.com/.well-known/jwks.json", + issuer="https://auth.yourcompany.com", + audience="mcp-production-api" +) + +# Create the remote auth provider +auth = RemoteAuthProvider( + token_verifier=token_verifier, + authorization_servers=[AnyHttpUrl("https://auth.yourcompany.com")], + base_url="https://api.yourcompany.com", # Your server base URL + # Optional: customize allowed client redirect URIs (defaults to localhost only) + allowed_client_redirect_uris=["http://localhost:*", "http://127.0.0.1:*"] +) + +mcp = FastMCP(name="Company API", auth=auth) +``` + +This configuration creates a server that accepts tokens issued by `auth.yourcompany.com` and provides the OAuth discovery metadata that MCP clients need. The `JWTVerifier` handles token validation using your identity provider's public keys, while the `RemoteAuthProvider` generates the required OAuth endpoints. + +The `authorization_servers` list tells MCP clients which identity providers you trust. The `base_url` identifies your server in OAuth metadata, enabling proper token audience validation. **Important**: The `base_url` should point to your server base URL - for example, if your MCP server is accessible at `https://api.yourcompany.com/mcp`, use `https://api.yourcompany.com` as the base URL. + +### Custom Endpoints + +You can extend `RemoteAuthProvider` to add additional endpoints beyond the standard OAuth protected resource metadata. These don't have to be OAuth-specific - you can add any endpoints your authentication integration requires. + +```python +import httpx +from starlette.responses import JSONResponse +from starlette.routing import Route + +class CompanyAuthProvider(RemoteAuthProvider): + def __init__(self): + token_verifier = JWTVerifier( + jwks_uri="https://auth.yourcompany.com/.well-known/jwks.json", + issuer="https://auth.yourcompany.com", + audience="mcp-production-api" + ) + + super().__init__( + token_verifier=token_verifier, + authorization_servers=[AnyHttpUrl("https://auth.yourcompany.com")], + base_url="https://api.yourcompany.com" # Your server base URL + ) + + def get_routes(self) -> list[Route]: + """Add custom endpoints to the standard protected resource routes.""" + + # Get the standard OAuth protected resource routes + routes = super().get_routes() + + # Add authorization server metadata forwarding for client convenience + async def authorization_server_metadata(request): + async with httpx.AsyncClient() as client: + response = await client.get( + "https://auth.yourcompany.com/.well-known/oauth-authorization-server" + ) + response.raise_for_status() + return JSONResponse(response.json()) + + routes.append( + Route("/.well-known/oauth-authorization-server", authorization_server_metadata) + ) + + return routes + +mcp = FastMCP(name="Company API", auth=CompanyAuthProvider()) +``` + +This pattern uses `super().get_routes()` to get the standard protected resource routes, then adds additional endpoints as needed. A common use case is providing authorization server metadata forwarding, which allows MCP clients to discover your identity provider's capabilities through your MCP server rather than contacting the identity provider directly. + +## WorkOS AuthKit Integration + +WorkOS AuthKit provides an excellent example of remote OAuth integration. The `AuthKitProvider` demonstrates how to implement both token validation and OAuth metadata forwarding in a production-ready package. + +```python +from fastmcp import FastMCP +from fastmcp.server.auth.providers.workos import AuthKitProvider + +auth = AuthKitProvider( + authkit_domain="https://your-project.authkit.app", + base_url="https://your-mcp-server.com" +) + +mcp = FastMCP(name="Protected Application", auth=auth) +``` + +The `AuthKitProvider` automatically configures JWT validation against WorkOS's public keys and provides both protected resource metadata and authorization server metadata forwarding. This implementation handles the complete remote OAuth integration with minimal configuration. + +WorkOS's support for Dynamic Client Registration makes it particularly well-suited for MCP applications. Clients can automatically register themselves with your WorkOS project and obtain the credentials needed for authentication without manual intervention. + +โ†’ **Complete WorkOS tutorial**: [AuthKit Integration Guide](/integrations/authkit) + +## Client Redirect URI Security + + +`RemoteAuthProvider` also supports the `allowed_client_redirect_uris` parameter for controlling which redirect URIs are accepted from MCP clients during DCR: + +- `None` (default): Only localhost patterns allowed +- Custom list: Specify allowed patterns with wildcard support +- Empty list `[]`: Allow all (not recommended) + +This provides defense-in-depth even though DCR providers typically validate redirect URIs themselves. + + +## Implementation Considerations + +Remote OAuth integration requires careful attention to several technical details that affect reliability and security. + +**Token Validation Performance**: Your server validates every incoming token by checking signatures against your identity provider's public keys. Consider implementing key caching and rotation handling to minimize latency while maintaining security. + +**Error Handling**: Network issues with your identity provider can affect token validation. Implement appropriate timeouts, retry logic, and graceful degradation to maintain service availability during identity provider outages. + +**Audience Validation**: Ensure that tokens intended for your server are not accepted by other applications. Proper audience validation prevents token misuse across different services in your ecosystem. + +**Scope Management**: Map token scopes to your application's permission model consistently. Consider how scope changes affect existing tokens and plan for smooth permission updates. + +The complexity of these considerations reinforces why external identity providers are recommended over custom OAuth implementations. Established providers handle these technical details with extensive testing and operational experience. \ No newline at end of file diff --git a/docs/servers/auth/token-verification.mdx b/docs/servers/auth/token-verification.mdx new file mode 100644 index 000000000..d2523151c --- /dev/null +++ b/docs/servers/auth/token-verification.mdx @@ -0,0 +1,247 @@ +--- +title: Token Verification +sidebarTitle: Token Verification +description: Protect your server by validating bearer tokens issued by external systems. +icon: key +tag: NEW +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + + + +Token verification enables your FastMCP server to validate bearer tokens issued by external systems without participating in user authentication flows. Your server acts as a pure resource server, focusing on token validation and authorization decisions while delegating identity management to other systems in your infrastructure. + + +Token verification operates somewhat outside the formal MCP authentication flow, which expects OAuth-style discovery. It's best suited for internal systems, microservices architectures, or when you have full control over token generation and distribution. + + +## Understanding Token Verification + +Token verification addresses scenarios where authentication responsibility is distributed across multiple systems. Your MCP server receives structured tokens containing identity and authorization information, validates their authenticity, and makes access control decisions based on their contents. + +This pattern emerges naturally in microservices architectures where a central authentication service issues tokens that multiple downstream services validate independently. It also works well when integrating MCP servers into existing systems that already have established token-based authentication mechanisms. + +### The Token Verification Model + +Token verification treats your MCP server as a resource server in OAuth terminology. The key insight is that token validation and token issuance are separate concerns that can be handled by different systems. + +**Token Issuance**: Another system (API gateway, authentication service, or identity provider) handles user authentication and creates signed tokens containing identity and permission information. + +**Token Validation**: Your MCP server receives these tokens, verifies their authenticity using cryptographic signatures, and extracts authorization information from their claims. + +**Access Control**: Based on token contents, your server determines what resources, tools, and prompts the client can access. + +This separation allows your MCP server to focus on its core functionality while leveraging existing authentication infrastructure. The token acts as a portable proof of identity that travels with each request. + +### Token Security Considerations + +Token-based authentication relies on cryptographic signatures to ensure token integrity. Your MCP server validates tokens using public keys corresponding to the private keys used for token creation. This asymmetric approach means your server never needs access to signing secrets. + +Token validation must address several security requirements: signature verification ensures tokens haven't been tampered with, expiration checking prevents use of stale tokens, and audience validation ensures tokens intended for your server aren't accepted by other systems. + +The challenge in MCP environments is that clients need to obtain valid tokens before making requests, but the MCP protocol doesn't provide built-in discovery mechanisms for token endpoints. Clients must obtain tokens through separate channels or prior configuration. + +## FastMCP Token Verification + +FastMCP provides the `TokenVerifier` class to handle token validation complexity while remaining flexible about token sources and validation strategies. + +### TokenVerifier Design + +`TokenVerifier` focuses exclusively on token validation without providing OAuth discovery metadata. This makes it ideal for internal systems where clients already know how to obtain tokens, or for microservices that trust tokens from known issuers. + +The class validates token signatures, checks expiration timestamps, and extracts authorization information from token claims. It supports various token formats and validation strategies while maintaining a consistent interface for authorization decisions. + +You can subclass `TokenVerifier` to implement custom validation logic for specialized token formats or validation requirements. The base class handles common patterns while allowing extension for unique use cases. + +### JWT Token Verification + +JSON Web Tokens (JWTs) represent the most common token format for modern applications. FastMCP's `JWTVerifier` validates JWTs using industry-standard cryptographic techniques and claim validation. + +#### JWKS Endpoint Integration + +JWKS endpoint integration provides the most flexible approach for production systems. The verifier automatically fetches public keys from a JSON Web Key Set endpoint, enabling automatic key rotation without server configuration changes. + +```python +from fastmcp import FastMCP +from fastmcp.server.auth.providers.jwt import JWTVerifier + +# Configure JWT verification against your identity provider +verifier = JWTVerifier( + jwks_uri="https://auth.yourcompany.com/.well-known/jwks.json", + issuer="https://auth.yourcompany.com", + audience="mcp-production-api" +) + +mcp = FastMCP(name="Protected API", auth=verifier) +``` + +This configuration creates a server that validates JWTs issued by `auth.yourcompany.com`. The verifier periodically fetches public keys from the JWKS endpoint and validates incoming tokens against those keys. Only tokens with the correct issuer and audience claims will be accepted. + +The `issuer` parameter ensures tokens come from your trusted authentication system, while `audience` validation prevents tokens intended for other services from being accepted by your MCP server. + +#### Symmetric Key Verification (HMAC) + +Symmetric key verification uses a shared secret for both signing and validation, making it ideal for internal microservices and trusted environments where the same secret can be securely distributed to both token issuers and validators. + +This approach is commonly used in microservices architectures where services share a secret key, or when your authentication service and MCP server are both managed by the same organization. The HMAC algorithms (HS256, HS384, HS512) provide strong security when the shared secret is properly managed. + +```python +from fastmcp import FastMCP +from fastmcp.server.auth.providers.jwt import JWTVerifier + +# Use a shared secret for symmetric key verification +verifier = JWTVerifier( + public_key="your-shared-secret-key-minimum-32-chars", # Despite the name, this accepts symmetric secrets + issuer="internal-auth-service", + audience="mcp-internal-api", + algorithm="HS256" # or HS384, HS512 for stronger security +) + +mcp = FastMCP(name="Internal API", auth=verifier) +``` + +The verifier will validate tokens signed with the same secret using the specified HMAC algorithm. This approach offers several advantages for internal systems: + +- **Simplicity**: No key pair management or certificate distribution +- **Performance**: HMAC operations are typically faster than RSA +- **Compatibility**: Works well with existing microservice authentication patterns + + +The parameter is named `public_key` for backwards compatibility, but when using HMAC algorithms (HS256/384/512), it accepts the symmetric secret string. + + + +**Security Considerations for Symmetric Keys:** +- Use a strong, randomly generated secret (minimum 32 characters recommended) +- Never expose the secret in logs, error messages, or version control +- Implement secure key distribution and rotation mechanisms +- Consider using asymmetric keys (RSA/ECDSA) for external-facing APIs + + +#### Static Public Key Verification + +Static public key verification works when you have a fixed RSA or ECDSA signing key and don't need automatic key rotation. This approach is primarily useful for development environments or controlled deployments where JWKS endpoints aren't available. + +```python +from fastmcp import FastMCP +from fastmcp.server.auth.providers.jwt import JWTVerifier + +# Use a static public key for token verification +public_key_pem = """-----BEGIN PUBLIC KEY----- +MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA... +-----END PUBLIC KEY-----""" + +verifier = JWTVerifier( + public_key=public_key_pem, + issuer="https://auth.yourcompany.com", + audience="mcp-production-api" +) + +mcp = FastMCP(name="Protected API", auth=verifier) +``` + +This configuration validates tokens using a specific RSA or ECDSA public key. The key must correspond to the private key used by your token issuer. While less flexible than JWKS endpoints, this approach can be useful in development environments or when testing with fixed keys. + +### Development and Testing + +Development environments often need simpler token management without the complexity of full JWT infrastructure. FastMCP provides tools specifically designed for these scenarios. + +#### Static Token Verification + +Static token verification enables rapid development by accepting predefined tokens with associated claims. This approach eliminates the need for token generation infrastructure during development and testing. + +```python +from fastmcp import FastMCP +from fastmcp.server.auth.providers.jwt import StaticTokenVerifier + +# Define development tokens and their associated claims +verifier = StaticTokenVerifier( + tokens={ + "dev-alice-token": { + "client_id": "alice@company.com", + "scopes": ["read:data", "write:data", "admin:users"] + }, + "dev-guest-token": { + "client_id": "guest-user", + "scopes": ["read:data"] + } + }, + required_scopes=["read:data"] +) + +mcp = FastMCP(name="Development Server", auth=verifier) +``` + +Clients can now authenticate using `Authorization: Bearer dev-alice-token` headers. The server will recognize the token and load the associated claims for authorization decisions. This approach enables immediate development without external dependencies. + + +Static token verification stores tokens as plain text and should never be used in production environments. It's designed exclusively for development and testing scenarios. + + +#### Test Token Generation + +Test token generation helps when you need to test JWT verification without setting up complete identity infrastructure. FastMCP includes utilities for generating test key pairs and signed tokens. + +```python +from fastmcp.server.auth.providers.jwt import JWTVerifier, RSAKeyPair + +# Generate a key pair for testing +key_pair = RSAKeyPair.generate() + +# Configure your server with the public key +verifier = JWTVerifier( + public_key=key_pair.public_key, + issuer="https://test.yourcompany.com", + audience="test-mcp-server" +) + +# Generate a test token using the private key +test_token = key_pair.create_token( + subject="test-user-123", + issuer="https://test.yourcompany.com", + audience="test-mcp-server", + scopes=["read", "write", "admin"] +) + +print(f"Test token: {test_token}") +``` + +This pattern enables comprehensive testing of JWT validation logic without depending on external token issuers. The generated tokens are cryptographically valid and will pass all standard JWT validation checks. + +## Environment Configuration + + + +FastMCP supports both programmatic and environment-based configuration for token verification, enabling flexible deployment across different environments. + +Environment-based configuration separates authentication settings from application code, following twelve-factor app principles and simplifying deployment pipelines. + +```bash +# Enable JWT verification +export FASTMCP_SERVER_AUTH=fastmcp.server.auth.providers.jwt.JWTVerifier + +# For asymmetric verification with JWKS endpoint: +export FASTMCP_SERVER_AUTH_JWT_JWKS_URI="https://auth.company.com/.well-known/jwks.json" +export FASTMCP_SERVER_AUTH_JWT_ISSUER="https://auth.company.com" +export FASTMCP_SERVER_AUTH_JWT_AUDIENCE="mcp-production-api" +export FASTMCP_SERVER_AUTH_JWT_REQUIRED_SCOPES="read:data,write:data" + +# OR for symmetric key verification (HMAC): +export FASTMCP_SERVER_AUTH_JWT_PUBLIC_KEY="your-shared-secret-key-minimum-32-chars" +export FASTMCP_SERVER_AUTH_JWT_ALGORITHM="HS256" # or HS384, HS512 +export FASTMCP_SERVER_AUTH_JWT_ISSUER="internal-auth-service" +export FASTMCP_SERVER_AUTH_JWT_AUDIENCE="mcp-internal-api" +``` + +With these environment variables configured, your FastMCP server automatically enables JWT verification: + +```python +from fastmcp import FastMCP + +# Authentication automatically configured from environment +mcp = FastMCP(name="Production API") +``` + +This approach enables the same codebase to run across development, staging, and production environments with different authentication requirements. Development might use static tokens while production uses JWT verification, all controlled through environment configuration. + diff --git a/docs/servers/composition.mdx b/docs/servers/composition.mdx new file mode 100644 index 000000000..370f072de --- /dev/null +++ b/docs/servers/composition.mdx @@ -0,0 +1,321 @@ +--- +title: Server Composition +sidebarTitle: Server Composition +description: Combine multiple FastMCP servers into a single, larger application using mounting and importing. +icon: puzzle-piece +--- +import { VersionBadge } from '/snippets/version-badge.mdx' + + + +As your MCP applications grow, you might want to organize your tools, resources, and prompts into logical modules or reuse existing server components. FastMCP supports composition through two methods: + +- **`import_server`**: For a one-time copy of components with prefixing (static composition). +- **`mount`**: For creating a live link where the main server delegates requests to the subserver (dynamic composition). + +## Why Compose Servers? + +- **Modularity**: Break down large applications into smaller, focused servers (e.g., a `WeatherServer`, a `DatabaseServer`, a `CalendarServer`). +- **Reusability**: Create common utility servers (e.g., a `TextProcessingServer`) and mount them wherever needed. +- **Teamwork**: Different teams can work on separate FastMCP servers that are later combined. +- **Organization**: Keep related functionality grouped together logically. + +### Importing vs Mounting + +The choice of importing or mounting depends on your use case and requirements. + +| Feature | Importing | Mounting | +|---------|----------------|---------| +| **Method** | `FastMCP.import_server(server, prefix=None)` | `FastMCP.mount(server, prefix=None)` | +| **Composition Type** | One-time copy (static) | Live link (dynamic) | +| **Updates** | Changes to subserver NOT reflected | Changes to subserver immediately reflected | +| **Performance** | Fast - no runtime delegation | Slower - affected by slowest mounted server | +| **Prefix** | Optional - omit for original names | Optional - omit for original names | +| **Best For** | Bundling finalized components, performance-critical setups | Modular runtime composition | + +### Proxy Servers + +FastMCP supports [MCP proxying](/servers/proxy), which allows you to mirror a local or remote server in a local FastMCP instance. Proxies are fully compatible with both importing and mounting. + + + +You can also create proxies from configuration dictionaries that follow the MCPConfig schema, which is useful for quickly connecting to one or more remote servers. See the [Proxy Servers documentation](/servers/proxy#configuration-based-proxies) for details on configuration-based proxying. Note that MCPConfig follows an emerging standard and its format may evolve over time. + +## Importing (Static Composition) + +The `import_server()` method copies all components (tools, resources, templates, prompts) from one `FastMCP` instance (the *subserver*) into another (the *main server*). An optional `prefix` can be provided to avoid naming conflicts. If no prefix is provided, components are imported without modification. When multiple servers are imported with the same prefix (or no prefix), the most recently imported server's components take precedence. + +```python +from fastmcp import FastMCP +import asyncio + +# Define subservers +weather_mcp = FastMCP(name="WeatherService") + +@weather_mcp.tool +def get_forecast(city: str) -> dict: + """Get weather forecast.""" + return {"city": city, "forecast": "Sunny"} + +@weather_mcp.resource("data://cities/supported") +def list_supported_cities() -> list[str]: + """List cities with weather support.""" + return ["London", "Paris", "Tokyo"] + +# Define main server +main_mcp = FastMCP(name="MainApp") + +# Import subserver +async def setup(): + await main_mcp.import_server(weather_mcp, prefix="weather") + +# Result: main_mcp now contains prefixed components: +# - Tool: "weather_get_forecast" +# - Resource: "data://weather/cities/supported" + +if __name__ == "__main__": + asyncio.run(setup()) + main_mcp.run() +``` + +### How Importing Works + +When you call `await main_mcp.import_server(subserver, prefix={whatever})`: + +1. **Tools**: All tools from `subserver` are added to `main_mcp` with names prefixed using `{prefix}_`. + - `subserver.tool(name="my_tool")` becomes `main_mcp.tool(name="{prefix}_my_tool")`. +2. **Resources**: All resources are added with both URIs and names prefixed. + - URI: `subserver.resource(uri="data://info")` becomes `main_mcp.resource(uri="data://{prefix}/info")`. + - Name: `resource.name` becomes `"{prefix}_{resource.name}"`. +3. **Resource Templates**: Templates are prefixed similarly to resources. + - URI: `subserver.resource(uri="data://{id}")` becomes `main_mcp.resource(uri="data://{prefix}/{id}")`. + - Name: `template.name` becomes `"{prefix}_{template.name}"`. +4. **Prompts**: All prompts are added with names prefixed using `{prefix}_`. + - `subserver.prompt(name="my_prompt")` becomes `main_mcp.prompt(name="{prefix}_my_prompt")`. + +Note that `import_server` performs a **one-time copy** of components. Changes made to the `subserver` *after* importing **will not** be reflected in `main_mcp`. The `subserver`'s `lifespan` context is also **not** executed by the main server. + + +The `prefix` parameter is optional. If omitted, components are imported without modification. + + +#### Importing Without Prefixes + + + +You can also import servers without specifying a prefix, which copies components using their original names: + +```python + +from fastmcp import FastMCP +import asyncio + +# Define subservers +weather_mcp = FastMCP(name="WeatherService") + +@weather_mcp.tool +def get_forecast(city: str) -> dict: + """Get weather forecast.""" + return {"city": city, "forecast": "Sunny"} + +@weather_mcp.resource("data://cities/supported") +def list_supported_cities() -> list[str]: + """List cities with weather support.""" + return ["London", "Paris", "Tokyo"] + +# Define main server +main_mcp = FastMCP(name="MainApp") + +# Import subserver +async def setup(): + # Import without prefix - components keep original names + await main_mcp.import_server(weather_mcp) + +# Result: main_mcp now contains: +# - Tool: "get_forecast" (original name preserved) +# - Resource: "data://cities/supported" (original URI preserved) + +if __name__ == "__main__": + asyncio.run(setup()) + main_mcp.run() +``` + +#### Conflict Resolution + + + +When importing multiple servers with the same prefix, or no prefix, components from the **most recently imported** server take precedence. + + + + +## Mounting (Live Linking) + +The `mount()` method creates a **live link** between the `main_mcp` server and the `subserver`. Instead of copying components, requests for components matching the optional `prefix` are **delegated** to the `subserver` at runtime. If no prefix is provided, the subserver's components are accessible without prefixing. When multiple servers are mounted with the same prefix (or no prefix), the most recently mounted server takes precedence for conflicting component names. + +```python +import asyncio +from fastmcp import FastMCP, Client + +# Define subserver +dynamic_mcp = FastMCP(name="DynamicService") + +@dynamic_mcp.tool +def initial_tool(): + """Initial tool demonstration.""" + return "Initial Tool Exists" + +# Mount subserver (synchronous operation) +main_mcp = FastMCP(name="MainAppLive") +main_mcp.mount(dynamic_mcp, prefix="dynamic") + +# Add a tool AFTER mounting - it will be accessible through main_mcp +@dynamic_mcp.tool +def added_later(): + """Tool added after mounting.""" + return "Tool Added Dynamically!" + +# Testing access to mounted tools +async def test_dynamic_mount(): + tools = await main_mcp.get_tools() + print("Available tools:", list(tools.keys())) + # Shows: ['dynamic_initial_tool', 'dynamic_added_later'] + + async with Client(main_mcp) as client: + result = await client.call_tool("dynamic_added_later") + print("Result:", result.data) + # Shows: "Tool Added Dynamically!" + +if __name__ == "__main__": + asyncio.run(test_dynamic_mount()) +``` + +### How Mounting Works + +When mounting is configured: + +1. **Live Link**: The parent server establishes a connection to the mounted server. +2. **Dynamic Updates**: Changes to the mounted server are immediately reflected when accessed through the parent. +3. **Prefixed Access**: The parent server uses prefixes to route requests to the mounted server. +4. **Delegation**: Requests for components matching the prefix are delegated to the mounted server at runtime. + +The same prefixing rules apply as with `import_server` for naming tools, resources, templates, and prompts. This includes prefixing both the URIs/keys and the names of resources and templates for better identification in multi-server configurations. + + + The `prefix` parameter is optional. If omitted, components are mounted without modification. + + +#### Performance Considerations + +Due to the "live link", operations like `list_tools()` on the parent server will be impacted by the speed of the slowest mounted server. In particular, HTTP-based mounted servers can introduce significant latency (300-400ms vs 1-2ms for local tools), and this slowdown affects the whole server, not just interactions with the HTTP-proxied tools. If performance is important, importing tools via [`import_server()`](#importing-static-composition) may be a more appropriate solution as it copies components once at startup rather than delegating requests at runtime. + +#### Mounting Without Prefixes + + + +You can also mount servers without specifying a prefix, which makes components accessible without prefixing. This works identically to [importing without prefixes](#importing-without-prefixes), including [conflict resolution](#conflict-resolution). + + + + +### Direct vs. Proxy Mounting + + + +FastMCP supports two mounting modes: + +1. **Direct Mounting** (default): The parent server directly accesses the mounted server's objects in memory. + - No client lifecycle events occur on the mounted server + - The mounted server's lifespan context is not executed + - Communication is handled through direct method calls + +2. **Proxy Mounting**: The parent server treats the mounted server as a separate entity and communicates with it through a client interface. + - Full client lifecycle events occur on the mounted server + - The mounted server's lifespan is executed when a client connects + - Communication happens via an in-memory Client transport + +```python +# Direct mounting (default when no custom lifespan) +main_mcp.mount(api_server, prefix="api") + +# Proxy mounting (preserves full client lifecycle) +main_mcp.mount(api_server, prefix="api", as_proxy=True) + +# Mounting without a prefix (components accessible without prefixing) +main_mcp.mount(api_server) +``` + +FastMCP automatically uses proxy mounting when the mounted server has a custom lifespan, but you can override this behavior with the `as_proxy` parameter. + +#### Interaction with Proxy Servers + +When using `FastMCP.as_proxy()` to create a proxy server, mounting that server will always use proxy mounting: + +```python +# Create a proxy for a remote server +remote_proxy = FastMCP.as_proxy(Client("http://example.com/mcp")) + +# Mount the proxy (always uses proxy mounting) +main_server.mount(remote_proxy, prefix="remote") +``` + + + +## Resource Prefix Formats + + + +When mounting or importing servers, resource URIs are usually prefixed to avoid naming conflicts. FastMCP supports two different formats for resource prefixes: + +### Path Format (Default) + +In path format, prefixes are added to the path component of the URI: + +``` +resource://prefix/path/to/resource +``` + +This is the default format since FastMCP 2.4. This format is recommended because it avoids issues with URI protocol restrictions (like underscores not being allowed in protocol names). + +### Protocol Format (Legacy) + +In protocol format, prefixes are added as part of the protocol: + +``` +prefix+resource://path/to/resource +``` + +This was the default format in FastMCP before 2.4. While still supported, it's not recommended for new code as it can cause problems with prefix names that aren't valid in URI protocols. + +### Configuring the Prefix Format + +You can configure the prefix format globally in code: + +```python +import fastmcp +fastmcp.settings.resource_prefix_format = "protocol" +``` + +Or via environment variable: + +```bash +FASTMCP_RESOURCE_PREFIX_FORMAT=protocol +``` + +Or per-server: + +```python +from fastmcp import FastMCP + +# Create a server that uses legacy protocol format +server = FastMCP("LegacyServer", resource_prefix_format="protocol") + +# Create a server that uses new path format +server = FastMCP("NewServer", resource_prefix_format="path") +``` + +When mounting or importing servers, the prefix format of the parent server is used. + + +When mounting servers, custom HTTP routes defined with `@server.custom_route()` are also forwarded to the parent server, making them accessible through the parent's HTTP application. + diff --git a/docs/servers/context.mdx b/docs/servers/context.mdx new file mode 100644 index 000000000..c6dd8d10a --- /dev/null +++ b/docs/servers/context.mdx @@ -0,0 +1,428 @@ +--- +title: MCP Context +sidebarTitle: Context +description: Access MCP capabilities like logging, progress, and resources within your MCP objects. +icon: rectangle-code +--- +import { VersionBadge } from '/snippets/version-badge.mdx' + +When defining FastMCP [tools](/servers/tools), [resources](/servers/resources), resource templates, or [prompts](/servers/prompts), your functions might need to interact with the underlying MCP session or access advanced server capabilities. FastMCP provides the `Context` object for this purpose. + +## What Is Context? + +The `Context` object provides a clean interface to access MCP features within your functions, including: + +- **Logging**: Send debug, info, warning, and error messages back to the client +- **Progress Reporting**: Update the client on the progress of long-running operations +- **Resource Access**: Read data from resources registered with the server +- **LLM Sampling**: Request the client's LLM to generate text based on provided messages +- **User Elicitation**: Request structured input from users during tool execution +- **State Management**: Store and share data across middleware and tool calls within a request +- **Request Information**: Access metadata about the current request +- **Server Access**: When needed, access the underlying FastMCP server instance + +## Accessing the Context + +### Via Dependency Injection + +To use the context object within any of your functions, simply add a parameter to your function signature and type-hint it as `Context`. FastMCP will automatically inject the context instance when your function is called. + +**Key Points:** + +- The parameter name (e.g., `ctx`, `context`) doesn't matter, only the type hint `Context` is important. +- The context parameter can be placed anywhere in your function's signature; it will not be exposed to MCP clients as a valid parameter. +- The context is optional - functions that don't need it can omit the parameter entirely. +- Context methods are async, so your function usually needs to be async as well. +- The type hint can be a union (`Context | None`) or use `Annotated[]` and it will still work properly. +- Context is only available during a request; attempting to use context methods outside a request will raise errors. If you need to debug or call your context methods outside of a request, you can type your variable as `Context | None=None` to avoid missing argument errors. + +#### Tools + +```python {1, 6} +from fastmcp import FastMCP, Context + +mcp = FastMCP(name="Context Demo") + +@mcp.tool +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 + return "Processed file" +``` + +#### Resources and Templates + + + +```python {1, 6, 12} +from fastmcp import FastMCP, Context + +mcp = FastMCP(name="Context Demo") + +@mcp.resource("resource://user-data") +async def get_user_data(ctx: Context) -> dict: + """Fetch personalized user data based on the request context.""" + # Context is available as the ctx parameter + return {"user_id": "example"} + +@mcp.resource("resource://users/{user_id}/profile") +async def get_user_profile(user_id: str, ctx: Context) -> dict: + """Fetch user profile with context-aware logging.""" + # Context is available as the ctx parameter + return {"id": user_id} +``` + +#### Prompts + + + +```python {1, 6} +from fastmcp import FastMCP, Context + +mcp = FastMCP(name="Context Demo") + +@mcp.prompt +async def data_analysis_request(dataset: str, ctx: Context) -> str: + """Generate a request to analyze data with contextual information.""" + # Context is available as the ctx parameter + return f"Please analyze the following dataset: {dataset}" +``` + + +### Via Runtime Dependency Function + + + +While the simplest way to access context is through function parameter injection as shown above, there are cases where you need to access the context in code that may not be easy to modify to accept a context parameter, or that is nested deeper within your function calls. + +FastMCP provides dependency functions that allow you to retrieve the active context from anywhere within a server request's execution flow: + +```python {2,9} +from fastmcp import FastMCP +from fastmcp.server.dependencies import get_context + +mcp = FastMCP(name="Dependency Demo") + +# Utility function that needs context but doesn't receive it as a parameter +async def process_data(data: list[float]) -> dict: + # Get the active context - only works when called within a request + ctx = get_context() + await ctx.info(f"Processing {len(data)} data points") + +@mcp.tool +async def analyze_dataset(dataset_name: str) -> dict: + # Call utility function that uses context internally + data = load_data(dataset_name) + await process_data(data) +``` + +**Important Notes:** + +- The `get_context` function should only be used within the context of a server request. Calling it outside of a request will raise a `RuntimeError`. +- The `get_context` function is server-only and should not be used in client code. + +## Context Capabilities + +FastMCP provides several advanced capabilities through the context object. Each capability has dedicated documentation with comprehensive examples and best practices: + +### Logging + +Send debug, info, warning, and error messages back to the MCP client for visibility into function execution. + +```python +await ctx.debug("Starting analysis") +await ctx.info(f"Processing {len(data)} items") +await ctx.warning("Deprecated parameter used") +await ctx.error("Processing failed") +``` + +See [Server Logging](/servers/logging) for complete documentation and examples. +### Client Elicitation + + + +Request structured input from clients during tool execution, enabling interactive workflows and progressive disclosure. This is a new feature in the 6/18/2025 MCP spec. + +```python +result = await ctx.elicit("Enter your name:", response_type=str) +if result.action == "accept": + name = result.data +``` + +See [User Elicitation](/servers/elicitation) for detailed examples and supported response types. + +### LLM Sampling + + + +Request the client's LLM to generate text based on provided messages, useful for leveraging AI capabilities within your tools. + +```python +response = await ctx.sample("Analyze this data", temperature=0.7) +``` + +See [LLM Sampling](/servers/sampling) for comprehensive usage and advanced techniques. + + +### Progress Reporting + +Update clients on the progress of long-running operations, enabling progress indicators and better user experience. + +```python +await ctx.report_progress(progress=50, total=100) # 50% complete +``` + +See [Progress Reporting](/servers/progress) for detailed patterns and examples. + +### Resource Access + +Read data from resources registered with your FastMCP server, allowing access to files, configuration, or dynamic content. + +```python +content_list = await ctx.read_resource("resource://config") +content = content_list[0].content +``` + +**Method signature:** +- **`ctx.read_resource(uri: str | AnyUrl) -> list[ReadResourceContents]`**: Returns a list of resource content parts + +### State Management + + + +Store and share data across middleware and tool calls within a request. Context objects maintain a state dictionary that's especially useful for passing information from [middleware](/servers/middleware) to your tools. + +To store a value in the context state, use `ctx.set_state(key, value)`. To retrieve a value, use `ctx.get_state(key)`. + +This simplified example shows how to use MCP middleware to store user info in the context state, and how to access that state in a tool: + +```python {7-8, 16-17} +from fastmcp.server.middleware import Middleware, MiddlewareContext + +class UserAuthMiddleware(Middleware): + async def on_call_tool(self, context: MiddlewareContext, call_next): + + # Middleware stores user info in context state + context.fastmcp_context.set_state("user_id", "user_123") + context.fastmcp_context.set_state("permissions", ["read", "write"]) + + return await call_next() + +@mcp.tool +async def secure_operation(data: str, ctx: Context) -> str: + """Tool can access state set by middleware.""" + + user_id = ctx.get_state("user_id") # "user_123" + permissions = ctx.get_state("permissions") # ["read", "write"] + + if "write" not in permissions: + return "Access denied" + + return f"Processing {data} for user {user_id}" +``` + +**Method signatures:** +- **`ctx.set_state(key: str, value: Any) -> None`**: Store a value in the context state +- **`ctx.get_state(key: str) -> Any`**: Retrieve a value from the context state (returns None if not found) + +**State Inheritance:** +When a new context is created (nested contexts), it inherits a copy of its parent's state. This ensures that: +- State set on a child context never affects the parent context +- State set on a parent context after the child context is initialized is not propagated to the child context + +This makes state management predictable and prevents unexpected side effects between nested operations. + +### Change Notifications + + + +FastMCP automatically sends list change notifications when components (such as tools, resources, or prompts) are added, removed, enabled, or disabled. In rare cases where you need to manually trigger these notifications, you can use the context methods: + +```python +@mcp.tool +async def custom_tool_management(ctx: Context) -> str: + """Example of manual notification after custom tool changes.""" + # After making custom changes to tools + await ctx.send_tool_list_changed() + await ctx.send_resource_list_changed() + await ctx.send_prompt_list_changed() + return "Notifications sent" +``` + +These methods are primarily used internally by FastMCP's automatic notification system and most users will not need to invoke them directly. + +### FastMCP Server + +To access the underlying FastMCP server instance, you can use the `ctx.fastmcp` property: + +```python +@mcp.tool +async def my_tool(ctx: Context) -> None: + # Access the FastMCP server instance + server_name = ctx.fastmcp.name + ... +``` + +### MCP Request + +Access metadata about the current request and client. + +```python +@mcp.tool +async def request_info(ctx: Context) -> dict: + """Return information about the current request.""" + return { + "request_id": ctx.request_id, + "client_id": ctx.client_id or "Unknown client" + } +``` + +**Available Properties:** + +- **`ctx.request_id -> str`**: Get the unique ID for the current MCP request +- **`ctx.client_id -> str | None`**: Get the ID of the client making the request, if provided during initialization +- **`ctx.session_id -> str | None`**: Get the MCP session ID for session-based data sharing (HTTP transports only) + + +The MCP request is part of the low-level MCP SDK and intended for advanced use cases. Most users will not need to use it directly. + + +## Runtime Dependencies + +### HTTP Requests + + + +The recommended way to access the current HTTP request is through the `get_http_request()` dependency function: + +```python {2, 3, 11} +from fastmcp import FastMCP +from fastmcp.server.dependencies import get_http_request +from starlette.requests import Request + +mcp = FastMCP(name="HTTP Request Demo") + +@mcp.tool +async def user_agent_info() -> dict: + """Return information about the user agent.""" + # Get the HTTP request + request: Request = get_http_request() + + # Access request data + user_agent = request.headers.get("user-agent", "Unknown") + client_ip = request.client.host if request.client else "Unknown" + + return { + "user_agent": user_agent, + "client_ip": client_ip, + "path": request.url.path, + } +``` + +This approach works anywhere within a request's execution flow, not just within your MCP function. It's useful when: + +1. You need access to HTTP information in helper functions +2. You're calling nested functions that need HTTP request data +3. You're working with middleware or other request processing code + +### HTTP Headers + + +If you only need request headers and want to avoid potential errors, you can use the `get_http_headers()` helper: + +```python {2, 10} +from fastmcp import FastMCP +from fastmcp.server.dependencies import get_http_headers + +mcp = FastMCP(name="Headers Demo") + +@mcp.tool +async def safe_header_info() -> dict: + """Safely get header information without raising errors.""" + # Get headers (returns empty dict if no request context) + headers = get_http_headers() + + # Get authorization header + auth_header = headers.get("authorization", "") + is_bearer = auth_header.startswith("Bearer ") + + return { + "user_agent": headers.get("user-agent", "Unknown"), + "content_type": headers.get("content-type", "Unknown"), + "has_auth": bool(auth_header), + "auth_type": "Bearer" if is_bearer else "Other" if auth_header else "None", + "headers_count": len(headers) + } +``` + +By default, `get_http_headers()` excludes problematic headers like `host` and `content-length`. To include all headers, use `get_http_headers(include_all=True)`. + +### Access Tokens + + + +When using authentication with your FastMCP server, you can access the authenticated user's access token information using the `get_access_token()` dependency function: + +```python {2, 10} +from fastmcp import FastMCP +from fastmcp.server.dependencies import get_access_token, AccessToken + +mcp = FastMCP(name="Auth Token Demo") + +@mcp.tool +async def get_user_info() -> dict: + """Get information about the authenticated user.""" + # Get the access token (None if not authenticated) + token: AccessToken | None = get_access_token() + + if token is None: + return {"authenticated": False} + + return { + "authenticated": True, + "client_id": token.client_id, + "scopes": token.scopes, + "expires_at": token.expires_at, + "token_claims": token.claims, # JWT claims or custom token data + } +``` + +This is particularly useful when you need to: + +1. **Access user identification** - Get the `client_id` or subject from token claims +2. **Check permissions** - Verify scopes or custom claims before performing operations +3. **Multi-tenant applications** - Extract tenant information from token claims +4. **Audit logging** - Track which user performed which actions + +#### Working with Token Claims + +The `claims` field contains all the data from the original token (JWT claims for JWT tokens, or custom data for other token types): + +```python {2, 3, 9, 12, 15} +from fastmcp import FastMCP +from fastmcp.server.dependencies import get_access_token + +mcp = FastMCP(name="Multi-tenant Demo") + +@mcp.tool +async def get_tenant_data(resource_id: str) -> dict: + """Get tenant-specific data using token claims.""" + token: AccessToken | None = get_access_token() + + # Extract tenant ID from token claims + tenant_id = token.claims.get("tenant_id") if token else None + + # Extract user ID from standard JWT subject claim + user_id = token.claims.get("sub") if token else None + + # Use tenant and user info to authorize and filter data + if not tenant_id: + raise ValueError("No tenant information in token") + + return { + "resource_id": resource_id, + "tenant_id": tenant_id, + "user_id": user_id, + "data": f"Tenant-specific data for {tenant_id}", + } +``` diff --git a/docs/servers/elicitation.mdx b/docs/servers/elicitation.mdx new file mode 100644 index 000000000..88e426f40 --- /dev/null +++ b/docs/servers/elicitation.mdx @@ -0,0 +1,320 @@ +--- +title: User Elicitation +sidebarTitle: Elicitation +description: Request structured input from users during tool execution through the MCP context. +icon: message-question +tag: NEW +--- + +import { VersionBadge } from '/snippets/version-badge.mdx' + + + +User elicitation allows MCP servers to request structured input from users during tool execution. Instead of requiring all inputs upfront, tools can interactively ask for missing parameters, clarification, or additional context as needed. + + +Most of the examples in this document assume you have a FastMCP server instance named `mcp` and show how to use the `ctx.elicit` method to request user input from an `@mcp.tool`-decorated function. + + +## What is Elicitation? + +Elicitation enables tools to pause execution and request specific information from users. This is particularly useful for: + +- **Missing parameters**: Ask for required information not provided initially +- **Clarification requests**: Get user confirmation or choices for ambiguous scenarios +- **Progressive disclosure**: Collect complex information step-by-step +- **Dynamic workflows**: Adapt tool behavior based on user responses + +For example, a file management tool might ask "Which directory should I create?" or a data analysis tool might request "What date range should I analyze?" + +### Basic Usage + +Use the `ctx.elicit()` method within any tool function to request user input: + +```python {14-17} +from fastmcp import FastMCP, Context +from dataclasses import dataclass + +mcp = FastMCP("Elicitation Server") + +@dataclass +class UserInfo: + name: str + age: int + +@mcp.tool +async def collect_user_info(ctx: Context) -> str: + """Collect user information through interactive prompts.""" + result = await ctx.elicit( + message="Please provide your information", + response_type=UserInfo + ) + + if result.action == "accept": + user = result.data + return f"Hello {user.name}, you are {user.age} years old" + elif result.action == "decline": + return "Information not provided" + else: # cancel + return "Operation cancelled" +``` + +## Method Signature + + + + + + The prompt message to display to the user + + + + The Python type defining the expected response structure (dataclass, primitive type, etc.) Note that elicitation responses are subject to a restricted subset of JSON Schema types. See [Supported Response Types](#supported-response-types) for more details. + + + + + + Result object containing the user's response + + + + How the user responded to the request + + + + The user's input data (only present when action is "accept") + + + + + + + +## Elicitation Actions + +The elicitation result contains an `action` field indicating how the user responded: + +- **`accept`**: User provided valid input - data is available in the `data` field +- **`decline`**: User chose not to provide the requested information and the data field is `None` +- **`cancel`**: User cancelled the entire operation and the data field is `None` + +```python {5, 7} +@mcp.tool +async def my_tool(ctx: Context) -> str: + result = await ctx.elicit("Choose an action") + + if result.action == "accept": + return "Accepted!" + elif result.action == "decline": + return "Declined!" + else: + return "Cancelled!" +``` + +FastMCP also provides typed result classes for pattern matching on the `action` field: + +```python {1-5, 12, 14, 16} +from fastmcp.server.elicitation import ( + AcceptedElicitation, + DeclinedElicitation, + CancelledElicitation, +) + +@mcp.tool +async def pattern_example(ctx: Context) -> str: + result = await ctx.elicit("Enter your name:", response_type=str) + + match result: + case AcceptedElicitation(data=name): + return f"Hello {name}!" + case DeclinedElicitation(): + return "No name provided" + case CancelledElicitation(): + return "Operation cancelled" +``` + +## Response Types + +The server must send a schema to the client indicating the type of data it expects in response to the elicitation request. If the request is `accept`-ed, the client must send a response that matches the schema. + +The MCP spec only supports a limited subset of JSON Schema types for elicitation responses. Specifically, it only supports JSON **objects** with **primitive** properties including `string`, `number` (or `integer`), `boolean` and `enum` fields. + +FastMCP makes it easy to request a broader range of types, including scalars (e.g. `str`) or no response at all, by automatically wrapping them in MCP-compatible object schemas. + + +### Scalar Types + +You can request simple scalar data types for basic input, such as a string, integer, or boolean. + +When you request a scalar type, FastMCP automatically wraps it in an object schema for MCP spec compatibility. Clients will see a corresponding schema requesting a single "value" field of the requested type. Once clients respond, the provided object is "unwrapped" and the scalar value is returned to your tool function as the `data` field of the `ElicitationResult` object. + +As a developer, this means you do not have to worry about creating or accessing a structured object when you only need a scalar value. + + +```python {4} title="Request a string" +@mcp.tool +async def get_user_name(ctx: Context) -> str: + """Get the user's name.""" + result = await ctx.elicit("What's your name?", response_type=str) + + if result.action == "accept": + return f"Hello, {result.data}!" + return "No name provided" +``` +```python {4} title="Request an integer" +@mcp.tool +async def pick_a_number(ctx: Context) -> str: + """Pick a number.""" + result = await ctx.elicit("Pick a number!", response_type=int) + + if result.action == "accept": + return f"You picked {result.data}" + return "No number provided" +``` +```python {4} title="Request a boolean" +@mcp.tool +async def pick_a_boolean(ctx: Context) -> str: + """Pick a boolean.""" + result = await ctx.elicit("True or false?", response_type=bool) + + if result.action == "accept": + return f"You picked {result.data}" + return "No boolean provided" +``` + + +### No Response + +Sometimes, the goal of an elicitation is to simply get a user to approve or reject an action. In this case, you can pass `None` as the response type to indicate that no response is expected. In order to comply with the MCP spec, the client will see a schema requesting an empty object in response. In this case, the `data` field of the `ElicitationResult` object will be `None` when the user accepts the elicitation. + +```python {4} title="No response" +@mcp.tool +async def approve_action(ctx: Context) -> str: + """Approve an action.""" + result = await ctx.elicit("Approve this action?", response_type=None) + + if result.action == "accept": + return do_action() + else: + raise ValueError("Action rejected") +``` + +### Constrained Options + +Often you'll want to constrain the user's response to a specific set of values. You can do this by using a `Literal` type or a Python enum as the response type, or by passing a list of strings to the `response_type` parameter as a convenient shortcut. + + +```python {6} title="Using a list of strings" +@mcp.tool +async def set_priority(ctx: Context) -> str: + """Set task priority level.""" + result = await ctx.elicit( + "What priority level?", + response_type=["low", "medium", "high"], + ) + + if result.action == "accept": + return f"Priority set to: {result.data}" +``` +```python {1, 8} title="Using a Literal type" +from typing import Literal + +@mcp.tool +async def set_priority(ctx: Context) -> str: + """Set task priority level.""" + result = await ctx.elicit( + "What priority level?", + response_type=Literal["low", "medium", "high"] + ) + + if result.action == "accept": + return f"Priority set to: {result.data}" + return "No priority set" +``` +```python {1, 11} title="Using a Python enum" +from enum import Enum + +class Priority(Enum): + LOW = "low" + MEDIUM = "medium" + HIGH = "high" + +@mcp.tool +async def set_priority(ctx: Context) -> str: + """Set task priority level.""" + result = await ctx.elicit("What priority level?", response_type=Priority) + + if result.action == "accept": + return f"Priority set to: {result.data.value}" + return "No priority set" +``` + + + +### Structured Responses + +You can request structured data with multiple fields by using a dataclass, typed dict, or Pydantic model as the response type. Note that the MCP spec only supports shallow objects with scalar (string, number, boolean) or enum properties. + +```python {1, 16, 20} +from dataclasses import dataclass +from typing import Literal + +@dataclass +class TaskDetails: + title: str + description: str + priority: Literal["low", "medium", "high"] + due_date: str + +@mcp.tool +async def create_task(ctx: Context) -> str: + """Create a new task with user-provided details.""" + result = await ctx.elicit( + "Please provide task details", + response_type=TaskDetails + ) + + if result.action == "accept": + task = result.data + return f"Created task: {task.title} (Priority: {task.priority})" + return "Task creation cancelled" +``` + +## Multi-Turn Elicitation + +Tools can make multiple elicitation calls to gather information progressively: + +```python {6, 11, 16-19} +@mcp.tool +async def plan_meeting(ctx: Context) -> str: + """Plan a meeting by gathering details step by step.""" + + # Get meeting title + title_result = await ctx.elicit("What's the meeting title?", response_type=str) + if title_result.action != "accept": + return "Meeting planning cancelled" + + # Get duration + duration_result = await ctx.elicit("Duration in minutes?", response_type=int) + if duration_result.action != "accept": + return "Meeting planning cancelled" + + # Get priority + priority_result = await ctx.elicit( + "Is this urgent?", + response_type=Literal["yes", "no"] + ) + if priority_result.action != "accept": + return "Meeting planning cancelled" + + urgent = priority_result.data == "yes" + return f"Meeting '{title_result.data}' planned for {duration_result.data} minutes (Urgent: {urgent})" +``` + + +## Client Requirements + +Elicitation requires the client to implement an elicitation handler. See [Client Elicitation](/clients/elicitation) for details on how clients can handle these requests. + +If a client doesn't support elicitation, calls to `ctx.elicit()` will raise an error indicating that elicitation is not supported. \ No newline at end of file diff --git a/docs/servers/logging.mdx b/docs/servers/logging.mdx new file mode 100644 index 000000000..6e27aa72d --- /dev/null +++ b/docs/servers/logging.mdx @@ -0,0 +1,236 @@ +--- +title: Server Logging +sidebarTitle: Logging +description: Send log messages back to MCP clients through the context. +icon: receipt +--- + +import { VersionBadge } from '/snippets/version-badge.mdx' + + +This documentation covers **MCP client logging** - sending messages from your server to MCP clients. For standard server-side logging (e.g., writing to files, console), use `fastmcp.utilities.logging.get_logger()` or Python's built-in `logging` module. + + +Server logging allows MCP tools to send debug, info, warning, and error messages back to the client. This provides visibility into function execution and helps with debugging during development and operation. + +## Why Use Server Logging? + +Server logging is essential for: + +- **Debugging**: Send detailed execution information to help diagnose issues +- **Progress visibility**: Keep users informed about what the tool is doing +- **Error reporting**: Communicate problems and their context to clients +- **Audit trails**: Create records of tool execution for compliance or analysis + +Unlike standard Python logging, MCP server logging sends messages directly to the client, making them visible in the client's interface or logs. + +### Basic Usage + +Use the context logging methods within any tool function: + +```python {8-9, 13, 17, 21} +from fastmcp import FastMCP, Context + +mcp = FastMCP("LoggingDemo") + +@mcp.tool +async def analyze_data(data: list[float], ctx: Context) -> dict: + """Analyze numerical data with comprehensive logging.""" + await ctx.debug("Starting analysis of numerical data") + await ctx.info(f"Analyzing {len(data)} data points") + + try: + if not data: + await ctx.warning("Empty data list provided") + return {"error": "Empty data list"} + + result = sum(data) / len(data) + await ctx.info(f"Analysis complete, average: {result}") + return {"average": result, "count": len(data)} + + except Exception as e: + await ctx.error(f"Analysis failed: {str(e)}") + raise +``` + +## Structured Logging with `extra` + +All logging methods (`debug`, `info`, `warning`, `error`, `log`) now accept an `extra` parameter, which is a dictionary of arbitrary data. This allows you to send structured data to the client, which is useful for creating rich, queryable logs. + +```python +@mcp.tool +async def process_transaction(transaction_id: str, amount: float, ctx: Context): + await ctx.info( + f"Processing transaction {transaction_id}", + extra={ + "transaction_id": transaction_id, + "amount": amount, + "currency": "USD" + } + ) + # ... processing logic ... +``` + +## Logging Methods + + + + Send debug-level messages for detailed execution information + + + + The debug message to send to the client + + + Optional dictionary for structured logging data + + + + + + Send informational messages about normal execution + + + + The information message to send to the client + + + Optional dictionary for structured logging data + + + + + + Send warning messages for potential issues that didn't prevent execution + + + + The warning message to send to the client + + + Optional dictionary for structured logging data + + + + + + Send error messages for problems that occurred during execution + + + + The error message to send to the client + + + Optional dictionary for structured logging data + + + + + + Generic logging method with custom level and logger name + + + + The log level for the message + + + + The message to send to the client + + + + Optional custom logger name for categorizing messages + + + Optional dictionary for structured logging data + + + + + +## Log Levels + +### Debug +Use for detailed information that's typically only useful when diagnosing problems: + +```python +@mcp.tool +async def process_file(file_path: str, ctx: Context) -> str: + """Process a file with detailed debug logging.""" + await ctx.debug(f"Starting to process file: {file_path}") + await ctx.debug("Checking file permissions") + + # File processing logic + await ctx.debug("File processing completed successfully") + return "File processed" +``` + +### Info +Use for general information about normal program execution: + +```python +@mcp.tool +async def backup_database(ctx: Context) -> str: + """Backup database with progress information.""" + await ctx.info("Starting database backup") + await ctx.info("Connecting to database") + await ctx.info("Backup completed successfully") + return "Database backed up" +``` + +### Warning +Use for potentially harmful situations that don't prevent execution: + +```python +@mcp.tool +async def validate_config(config: dict, ctx: Context) -> dict: + """Validate configuration with warnings for deprecated options.""" + if "old_api_key" in config: + await ctx.warning( + "Using deprecated 'old_api_key' field. Please use 'api_key' instead", + extra={"deprecated_field": "old_api_key"} + ) + + if config.get("timeout", 30) > 300: + await ctx.warning( + "Timeout value is very high (>5 minutes), this may cause issues", + extra={"timeout_value": config.get("timeout")} + ) + + return {"status": "valid", "warnings": "see logs"} +``` + +### Error +Use for error events that might still allow the application to continue: + +```python +@mcp.tool +async def batch_process(items: list[str], ctx: Context) -> dict: + """Process multiple items, logging errors for failed items.""" + successful = 0 + failed = 0 + + for item in items: + try: + # Process item + successful += 1 + except Exception as e: + await ctx.error( + f"Failed to process item '{item}': {str(e)}", + extra={"failed_item": item} + ) + failed += 1 + + return {"successful": successful, "failed": failed} +``` + + +## Client Handling + +Log messages are sent to the client through the MCP protocol. How clients handle these messages depends on their implementation: + +- **Development clients**: May display logs in real-time for debugging +- **Production clients**: May store logs for later analysis or display to users +- **Integration clients**: May forward logs to external logging systems + +See [Client Logging](/clients/logging) for details on how clients can handle server log messages. diff --git a/docs/servers/middleware.mdx b/docs/servers/middleware.mdx new file mode 100644 index 000000000..5ef8d8fe1 --- /dev/null +++ b/docs/servers/middleware.mdx @@ -0,0 +1,641 @@ +--- +title: MCP Middleware +sidebarTitle: Middleware +description: Add cross-cutting functionality to your MCP server with middleware that can inspect, modify, and respond to all MCP requests and responses. +icon: layer-group +tag: NEW +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + + + +MCP middleware is a powerful concept that allows you to add cross-cutting functionality to your FastMCP server. Unlike traditional web middleware, MCP middleware is designed specifically for the Model Context Protocol, providing hooks for different types of MCP operations like tool calls, resource reads, and prompt requests. + + +MCP middleware is a FastMCP-specific concept and is not part of the official MCP protocol specification. This middleware system is designed to work with FastMCP servers and may not be compatible with other MCP implementations. + + + +MCP middleware is a brand new concept and may be subject to breaking changes in future versions. + + +## What is MCP Middleware? + +MCP middleware lets you intercept and modify MCP requests and responses as they flow through your server. Think of it as a pipeline where each piece of middleware can inspect what's happening, make changes, and then pass control to the next middleware in the chain. + +Common use cases for MCP middleware include: +- **Authentication and Authorization**: Verify client permissions before executing operations +- **Logging and Monitoring**: Track usage patterns and performance metrics +- **Rate Limiting**: Control request frequency per client or operation type +- **Request/Response Transformation**: Modify data before it reaches tools or after it leaves +- **Caching**: Store frequently requested data to improve performance +- **Error Handling**: Provide consistent error responses across your server + +## How Middleware Works + +FastMCP middleware operates on a pipeline model. When a request comes in, it flows through your middleware in the order they were added to the server. Each middleware can: + +1. **Inspect the incoming request** and its context +2. **Modify the request** before passing it to the next middleware or handler +3. **Execute the next middleware/handler** in the chain by calling `call_next()` +4. **Inspect and modify the response** before returning it +5. **Handle errors** that occur during processing + +The key insight is that middleware forms a chain where each piece decides whether to continue processing or stop the chain entirely. + +If you're familiar with ASGI middleware, the basic structure of FastMCP middleware will feel familiar. At its core, middleware is a callable class that receives a context object containing information about the current JSON-RPC message and a handler function to continue the middleware chain. + +It's important to understand that MCP operates on the [JSON-RPC specification](https://spec.modelcontextprotocol.io/specification/basic/transports/). While FastMCP presents requests and responses in a familiar way, these are fundamentally JSON-RPC messages, not HTTP request/response pairs like you might be used to in web applications. FastMCP middleware works with all [transport types](/clients/transports), including local stdio transport and HTTP transports, though not all middleware implementations are compatible across all transports (e.g., middleware that inspects HTTP headers won't work with stdio transport). + +The most fundamental way to implement middleware is by overriding the `__call__` method on the `Middleware` base class: + +```python +from fastmcp.server.middleware import Middleware, MiddlewareContext + +class RawMiddleware(Middleware): + async def __call__(self, context: MiddlewareContext, call_next): + # This method receives ALL messages regardless of type + print(f"Raw middleware processing: {context.method}") + result = await call_next(context) + print(f"Raw middleware completed: {context.method}") + return result +``` + +This gives you complete control over every message that flows through your server, but requires you to handle all message types manually. + +## Middleware Hooks + +To make it easier for users to target specific types of messages, FastMCP middleware provides a variety of specialized hooks. Instead of implementing the raw `__call__` method, you can override specific hook methods that are called only for certain types of operations, allowing you to target exactly the level of specificity you need for your middleware logic. + +### Hook Hierarchy and Execution Order + +FastMCP provides multiple hooks that are called with varying levels of specificity. Understanding this hierarchy is crucial for effective middleware design. + +When a request comes in, **multiple hooks may be called for the same request**, going from general to specific: + +1. **`on_message`** - Called for ALL MCP messages (both requests and notifications) +2. **`on_request` or `on_notification`** - Called based on the message type +3. **Operation-specific hooks** - Called for specific MCP operations like `on_call_tool` + +For example, when a client calls a tool, your middleware will receive **multiple hook calls**: +1. `on_message` and `on_request` for any initial tool discovery operations (list_tools) +2. `on_message` (because it's any MCP message) for the tool call itself +3. `on_request` (because tool calls expect responses) for the tool call itself +4. `on_call_tool` (because it's specifically a tool execution) for the tool call itself + +Note that the MCP SDK may perform additional operations like listing tools for caching purposes, which will trigger additional middleware calls beyond just the direct tool execution. + +This hierarchy allows you to target your middleware logic with the right level of specificity. Use `on_message` for broad concerns like logging, `on_request` for authentication, and `on_call_tool` for tool-specific logic like performance monitoring. + +### Available Hooks + +- `on_message`: Called for all MCP messages (requests and notifications) +- `on_request`: Called specifically for MCP requests (that expect responses) +- `on_notification`: Called specifically for MCP notifications (fire-and-forget) +- `on_call_tool`: Called when tools are being executed +- `on_read_resource`: Called when resources are being read +- `on_get_prompt`: Called when prompts are being retrieved +- `on_list_tools`: Called when listing available tools +- `on_list_resources`: Called when listing available resources +- `on_list_resource_templates`: Called when listing resource templates +- `on_list_prompts`: Called when listing available prompts + +## Component Access in Middleware + +Understanding how to access component information (tools, resources, prompts) in middleware is crucial for building powerful middleware functionality. The access patterns differ significantly between listing operations and execution operations. + +### Listing Operations vs Execution Operations + +FastMCP middleware handles two types of operations differently: + +**Listing Operations** (`on_list_tools`, `on_list_resources`, `on_list_prompts`, etc.): +- Middleware receives **FastMCP component objects** with full metadata +- These objects include FastMCP-specific properties like `tags` that can be accessed directly from the component +- The result contains complete component information before it's converted to MCP format +- Tags are included in the component's `meta` field in the listing response returned to MCP clients + +**Execution Operations** (`on_call_tool`, `on_read_resource`, `on_get_prompt`): +- Middleware runs **before** the component is executed +- The middleware result is either the execution result or an error if the component wasn't found +- Component metadata isn't directly available in the hook parameters + +### Accessing Component Metadata During Execution + +If you need to check component properties (like tags) during execution operations, use the FastMCP server instance available through the context: + +```python +from fastmcp.server.middleware import Middleware, MiddlewareContext +from fastmcp.exceptions import ToolError + +class TagBasedMiddleware(Middleware): + async def on_call_tool(self, context: MiddlewareContext, call_next): + # Access the tool object to check its metadata + if context.fastmcp_context: + try: + tool = await context.fastmcp_context.fastmcp.get_tool(context.message.name) + + # Check if this tool has a "private" tag + if "private" in tool.tags: + raise ToolError("Access denied: private tool") + + # Check if tool is enabled + if not tool.enabled: + raise ToolError("Tool is currently disabled") + + except Exception: + # Tool not found or other error - let execution continue + # and handle the error naturally + pass + + return await call_next(context) +``` + +The same pattern works for resources and prompts: + +```python +from fastmcp.server.middleware import Middleware, MiddlewareContext +from fastmcp.exceptions import ResourceError, PromptError + +class ComponentAccessMiddleware(Middleware): + async def on_read_resource(self, context: MiddlewareContext, call_next): + if context.fastmcp_context: + try: + resource = await context.fastmcp_context.fastmcp.get_resource(context.message.uri) + if "restricted" in resource.tags: + raise ResourceError("Access denied: restricted resource") + except Exception: + pass + return await call_next(context) + + async def on_get_prompt(self, context: MiddlewareContext, call_next): + if context.fastmcp_context: + try: + prompt = await context.fastmcp_context.fastmcp.get_prompt(context.message.name) + if not prompt.enabled: + raise PromptError("Prompt is currently disabled") + except Exception: + pass + return await call_next(context) +``` + +### Working with Listing Results + +For listing operations, the middleware `call_next` function returns a list of FastMCP components prior to being converted to MCP format. You can filter or modify this list and return it to the client. For example: + +```python +from fastmcp.server.middleware import Middleware, MiddlewareContext + +class ListingFilterMiddleware(Middleware): + async def on_list_tools(self, context: MiddlewareContext, call_next): + result = await call_next(context) + + # Filter out tools with "private" tag + filtered_tools = [ + tool for tool in result + if "private" not in tool.tags + ] + + # Return modified list + return filtered_tools +``` + +This filtering happens before the components are converted to MCP format and returned to the client. Tags are accessible both during filtering and are included in the component's `meta` field in the final listing response. + + +When filtering components in listing operations, ensure you also prevent execution of filtered components in the corresponding execution hooks (`on_call_tool`, `on_read_resource`, `on_get_prompt`) to maintain consistency. + + +### Tool Call Denial + +You can deny access to specific tools by raising a `ToolError` in your middleware. This is the correct way to block tool execution, as it integrates properly with the FastMCP error handling system. + +```python +from fastmcp.server.middleware import Middleware, MiddlewareContext +from fastmcp.exceptions import ToolError + +class AuthMiddleware(Middleware): + async def on_call_tool(self, context: MiddlewareContext, call_next): + tool_name = context.message.name + + # Deny access to restricted tools + if tool_name.lower() in ["delete", "admin_config"]: + raise ToolError("Access denied: tool requires admin privileges") + + # Allow other tools to proceed + return await call_next(context) +``` + + +When denying tool calls, always raise `ToolError` rather than returning `ToolResult` objects or other values. `ToolError` ensures proper error propagation through the middleware chain and converts to the correct MCP error response format. + + +### Tool Call Modification + +For execution operations like tool calls, you can modify arguments before execution or transform results afterward: + +```python +from fastmcp.server.middleware import Middleware, MiddlewareContext + +class ToolCallMiddleware(Middleware): + async def on_call_tool(self, context: MiddlewareContext, call_next): + # Modify arguments before execution + if context.message.name == "calculate": + # Ensure positive inputs + if context.message.arguments.get("value", 0) < 0: + context.message.arguments["value"] = abs(context.message.arguments["value"]) + + result = await call_next(context) + + # Transform result after execution + if context.message.name == "get_data": + # Add metadata to result + if result.structured_content: + result.structured_content["processed_at"] = "2024-01-01T00:00:00Z" + + return result +``` + + +For more complex tool rewriting scenarios, consider using [Tool Transformation](/patterns/tool-transformation) patterns which provide a more structured approach to creating modified tool variants. + + +### Anatomy of a Hook + +Every middleware hook follows the same pattern. Let's examine the `on_message` hook to understand the structure: + +```python +async def on_message(self, context: MiddlewareContext, call_next): + # 1. Pre-processing: Inspect and optionally modify the request + print(f"Processing {context.method}") + + # 2. Chain continuation: Call the next middleware/handler + result = await call_next(context) + + # 3. Post-processing: Inspect and optionally modify the response + print(f"Completed {context.method}") + + # 4. Return the result (potentially modified) + return result +``` + +### Hook Parameters + +Every hook receives two parameters: + +1. **`context: MiddlewareContext`** - Contains information about the current request: + - `context.method` - The MCP method name (e.g., "tools/call") + - `context.source` - Where the request came from ("client" or "server") + - `context.type` - Message type ("request" or "notification") + - `context.message` - The MCP message data + - `context.timestamp` - When the request was received + - `context.fastmcp_context` - FastMCP Context object (if available) + +2. **`call_next`** - A function that continues the middleware chain. You **must** call this to proceed, unless you want to stop processing entirely. + +### Control Flow + +You have complete control over the request flow: +- **Continue processing**: Call `await call_next(context)` to proceed +- **Modify the request**: Change the context before calling `call_next` +- **Modify the response**: Change the result after calling `call_next` +- **Stop the chain**: Don't call `call_next` (rarely needed) +- **Handle errors**: Wrap `call_next` in try/catch blocks + +#### State Management + + + +In addition to modifying the request and response, you can also store state data that your tools can (optionally) access later. To do so, use the FastMCP Context to either `set_state` or `get_state` as appropriate. For more information, see the [Context State Management](/servers/context#state-management) docs. + +## Creating Middleware + +FastMCP middleware is implemented by subclassing the `Middleware` base class and overriding the hooks you need. You only need to implement the hooks that are relevant to your use case. + +```python +from fastmcp import FastMCP +from fastmcp.server.middleware import Middleware, MiddlewareContext + +class LoggingMiddleware(Middleware): + """Middleware that logs all MCP operations.""" + + async def on_message(self, context: MiddlewareContext, call_next): + """Called for all MCP messages.""" + print(f"Processing {context.method} from {context.source}") + + result = await call_next(context) + + print(f"Completed {context.method}") + return result + +# Add middleware to your server +mcp = FastMCP("MyServer") +mcp.add_middleware(LoggingMiddleware()) +``` + +This creates a basic logging middleware that will print information about every request that flows through your server. + +## Adding Middleware to Your Server + +### Single Middleware + +Adding middleware to your server is straightforward: + +```python +mcp = FastMCP("MyServer") +mcp.add_middleware(LoggingMiddleware()) +``` + +### Multiple Middleware + +Middleware executes in the order it's added to the server. The first middleware added runs first on the way in, and last on the way out: + +```python +mcp = FastMCP("MyServer") + +mcp.add_middleware(AuthenticationMiddleware("secret-token")) +mcp.add_middleware(PerformanceMiddleware()) +mcp.add_middleware(LoggingMiddleware()) +``` + +This creates the following execution flow: +1. AuthenticationMiddleware (pre-processing) +2. PerformanceMiddleware (pre-processing) +3. LoggingMiddleware (pre-processing) +4. Actual tool/resource handler +5. LoggingMiddleware (post-processing) +6. PerformanceMiddleware (post-processing) +7. AuthenticationMiddleware (post-processing) + +## Server Composition and Middleware + +When using [Server Composition](/servers/composition) with `mount` or `import_server`, middleware behavior follows these rules: + +1. **Parent server middleware** runs for all requests, including those routed to mounted servers +2. **Mounted server middleware** only runs for requests handled by that specific server +3. **Middleware order** is preserved within each server + +This allows you to create layered middleware architectures where parent servers handle cross-cutting concerns like authentication, while child servers focus on domain-specific middleware. + +```python +# Parent server with middleware +parent = FastMCP("Parent") +parent.add_middleware(AuthenticationMiddleware("token")) + +# Child server with its own middleware +child = FastMCP("Child") +child.add_middleware(LoggingMiddleware()) + +@child.tool +def child_tool() -> str: + return "from child" + +# Mount the child server +parent.mount(child, prefix="child") +``` + +When a client calls "child_tool", the request will flow through the parent's authentication middleware first, then route to the child server where it will go through the child's logging middleware. + +## Built-in Middleware Examples + +FastMCP includes several middleware implementations that demonstrate best practices and provide immediately useful functionality. Let's explore how each type works by building simplified versions, then see how to use the full implementations. + +### Timing Middleware + +Performance monitoring is essential for understanding your server's behavior and identifying bottlenecks. FastMCP includes timing middleware at `fastmcp.server.middleware.timing`. + +Here's an example of how it works: + +```python +import time +from fastmcp.server.middleware import Middleware, MiddlewareContext + +class SimpleTimingMiddleware(Middleware): + async def on_request(self, context: MiddlewareContext, call_next): + start_time = time.perf_counter() + + try: + result = await call_next(context) + duration_ms = (time.perf_counter() - start_time) * 1000 + print(f"Request {context.method} completed in {duration_ms:.2f}ms") + return result + except Exception as e: + duration_ms = (time.perf_counter() - start_time) * 1000 + print(f"Request {context.method} failed after {duration_ms:.2f}ms: {e}") + raise +``` + +To use the full version with proper logging and configuration: + +```python +from fastmcp.server.middleware.timing import ( + TimingMiddleware, + DetailedTimingMiddleware +) + +# Basic timing for all requests +mcp.add_middleware(TimingMiddleware()) + +# Detailed per-operation timing (tools, resources, prompts) +mcp.add_middleware(DetailedTimingMiddleware()) +``` + +The built-in versions include custom logger support, proper formatting, and **DetailedTimingMiddleware** provides operation-specific hooks like `on_call_tool` and `on_read_resource` for granular timing. + +### Logging Middleware + +Request and response logging is crucial for debugging, monitoring, and understanding usage patterns in your MCP server. FastMCP provides comprehensive logging middleware at `fastmcp.server.middleware.logging`. + +Here's an example of how it works: + +```python +from fastmcp.server.middleware import Middleware, MiddlewareContext + +class SimpleLoggingMiddleware(Middleware): + async def on_message(self, context: MiddlewareContext, call_next): + print(f"Processing {context.method} from {context.source}") + + try: + result = await call_next(context) + print(f"Completed {context.method}") + return result + except Exception as e: + print(f"Failed {context.method}: {e}") + raise +``` + +To use the full versions with advanced features: + +```python +from fastmcp.server.middleware.logging import ( + LoggingMiddleware, + StructuredLoggingMiddleware +) + +# Human-readable logging with payload support +mcp.add_middleware(LoggingMiddleware( + include_payloads=True, + max_payload_length=1000 +)) + +# JSON-structured logging for log aggregation tools +mcp.add_middleware(StructuredLoggingMiddleware(include_payloads=True)) +``` + +The built-in versions include payload logging, structured JSON output, custom logger support, payload size limits, and operation-specific hooks for granular control. + +### Rate Limiting Middleware + +Rate limiting is essential for protecting your server from abuse, ensuring fair resource usage, and maintaining performance under load. FastMCP includes sophisticated rate limiting middleware at `fastmcp.server.middleware.rate_limiting`. + +Here's an example of how it works: + +```python +import time +from collections import defaultdict +from fastmcp.server.middleware import Middleware, MiddlewareContext +from mcp import McpError +from mcp.types import ErrorData + +class SimpleRateLimitMiddleware(Middleware): + def __init__(self, requests_per_minute: int = 60): + self.requests_per_minute = requests_per_minute + self.client_requests = defaultdict(list) + + async def on_request(self, context: MiddlewareContext, call_next): + current_time = time.time() + client_id = "default" # In practice, extract from headers or context + + # Clean old requests and check limit + cutoff_time = current_time - 60 + self.client_requests[client_id] = [ + req_time for req_time in self.client_requests[client_id] + if req_time > cutoff_time + ] + + if len(self.client_requests[client_id]) >= self.requests_per_minute: + raise McpError(ErrorData(code=-32000, message="Rate limit exceeded")) + + self.client_requests[client_id].append(current_time) + return await call_next(context) +``` + +To use the full versions with advanced algorithms: + +```python +from fastmcp.server.middleware.rate_limiting import ( + RateLimitingMiddleware, + SlidingWindowRateLimitingMiddleware +) + +# Token bucket rate limiting (allows controlled bursts) +mcp.add_middleware(RateLimitingMiddleware( + max_requests_per_second=10.0, + burst_capacity=20 +)) + +# Sliding window rate limiting (precise time-based control) +mcp.add_middleware(SlidingWindowRateLimitingMiddleware( + max_requests=100, + window_minutes=1 +)) +``` + +The built-in versions include token bucket algorithms, per-client identification, global rate limiting, and async-safe implementations with configurable client identification functions. + +### Error Handling Middleware + +Consistent error handling and recovery is critical for robust MCP servers. FastMCP provides comprehensive error handling middleware at `fastmcp.server.middleware.error_handling`. + +Here's an example of how it works: + +```python +import logging +from fastmcp.server.middleware import Middleware, MiddlewareContext + +class SimpleErrorHandlingMiddleware(Middleware): + def __init__(self): + self.logger = logging.getLogger("errors") + self.error_counts = {} + + async def on_message(self, context: MiddlewareContext, call_next): + try: + return await call_next(context) + except Exception as error: + # Log the error and track statistics + error_key = f"{type(error).__name__}:{context.method}" + self.error_counts[error_key] = self.error_counts.get(error_key, 0) + 1 + + self.logger.error(f"Error in {context.method}: {type(error).__name__}: {error}") + raise +``` + +To use the full versions with advanced features: + +```python +from fastmcp.server.middleware.error_handling import ( + ErrorHandlingMiddleware, + RetryMiddleware +) + +# Comprehensive error logging and transformation +mcp.add_middleware(ErrorHandlingMiddleware( + include_traceback=True, + transform_errors=True, + error_callback=my_error_callback +)) + +# Automatic retry with exponential backoff +mcp.add_middleware(RetryMiddleware( + max_retries=3, + retry_exceptions=(ConnectionError, TimeoutError) +)) +``` + +The built-in versions include error transformation, custom callbacks, configurable retry logic, and proper MCP error formatting. + +### Combining Middleware + +These middleware work together seamlessly: + +```python +from fastmcp import FastMCP +from fastmcp.server.middleware.timing import TimingMiddleware +from fastmcp.server.middleware.logging import LoggingMiddleware +from fastmcp.server.middleware.rate_limiting import RateLimitingMiddleware +from fastmcp.server.middleware.error_handling import ErrorHandlingMiddleware + +mcp = FastMCP("Production Server") + +# Add middleware in logical order +mcp.add_middleware(ErrorHandlingMiddleware()) # Handle errors first +mcp.add_middleware(RateLimitingMiddleware(max_requests_per_second=50)) +mcp.add_middleware(TimingMiddleware()) # Time actual execution +mcp.add_middleware(LoggingMiddleware()) # Log everything + +@mcp.tool +def my_tool(data: str) -> str: + return f"Processed: {data}" +``` + +This configuration provides comprehensive monitoring, protection, and observability for your MCP server. + +### Custom Middleware Example + +You can also create custom middleware by extending the base class: + +```python +from fastmcp.server.middleware import Middleware, MiddlewareContext + +class CustomHeaderMiddleware(Middleware): + async def on_request(self, context: MiddlewareContext, call_next): + # Add custom logic here + print(f"Processing {context.method}") + + result = await call_next(context) + + print(f"Completed {context.method}") + return result + +mcp.add_middleware(CustomHeaderMiddleware()) +``` \ No newline at end of file diff --git a/docs/servers/progress.mdx b/docs/servers/progress.mdx new file mode 100644 index 000000000..b5625864b --- /dev/null +++ b/docs/servers/progress.mdx @@ -0,0 +1,189 @@ +--- +title: Progress Reporting +sidebarTitle: Progress +description: Update clients on the progress of long-running operations through the MCP context. +icon: chart-line +--- + +import { VersionBadge } from '/snippets/version-badge.mdx' + +Progress reporting allows MCP tools to notify clients about the progress of long-running operations. This enables clients to display progress indicators and provide better user experience during time-consuming tasks. + +## Why Use Progress Reporting? + +Progress reporting is valuable for: + +- **User experience**: Keep users informed about long-running operations +- **Progress indicators**: Enable clients to show progress bars or percentages +- **Timeout prevention**: Demonstrate that operations are actively progressing +- **Debugging**: Track execution progress for performance analysis + +### Basic Usage + +Use `ctx.report_progress()` to send progress updates to the client: + +```python {14, 21} +from fastmcp import FastMCP, Context +import asyncio + +mcp = FastMCP("ProgressDemo") + +@mcp.tool +async def process_items(items: list[str], ctx: Context) -> dict: + """Process a list of items with progress updates.""" + total = len(items) + results = [] + + for i, item in enumerate(items): + # Report progress as we process each item + await ctx.report_progress(progress=i, total=total) + + # Simulate processing time + await asyncio.sleep(0.1) + results.append(item.upper()) + + # Report 100% completion + await ctx.report_progress(progress=total, total=total) + + return {"processed": len(results), "results": results} +``` + +## Method Signature + + + + Report progress to the client for long-running operations + + + + Current progress value (e.g., 24, 0.75, 1500) + + + + Optional total value (e.g., 100, 1.0, 2000). When provided, clients may interpret this as enabling percentage calculation. + + + + + +## Progress Patterns + +### Percentage-Based Progress + +Report progress as a percentage (0-100): + +```python {13-14} +@mcp.tool +async def download_file(url: str, ctx: Context) -> str: + """Download a file with percentage progress.""" + total_size = 1000 # KB + downloaded = 0 + + while downloaded < total_size: + # Download chunk + chunk_size = min(50, total_size - downloaded) + downloaded += chunk_size + + # Report percentage progress + percentage = (downloaded / total_size) * 100 + await ctx.report_progress(progress=percentage, total=100) + + await asyncio.sleep(0.1) # Simulate download time + + return f"Downloaded file from {url}" +``` + +### Absolute Progress + +Report progress with absolute values: + +```python {10} +@mcp.tool +async def backup_database(ctx: Context) -> str: + """Backup database tables with absolute progress.""" + tables = ["users", "orders", "products", "inventory", "logs"] + + for i, table in enumerate(tables): + await ctx.info(f"Backing up table: {table}") + + # Report absolute progress + await ctx.report_progress(progress=i + 1, total=len(tables)) + + # Simulate backup time + await asyncio.sleep(0.5) + + return "Database backup completed" +``` + +### Indeterminate Progress + +Report progress without a known total for operations where the endpoint is unknown: + +```python {11} +@mcp.tool +async def scan_directory(directory: str, ctx: Context) -> dict: + """Scan directory with indeterminate progress.""" + files_found = 0 + + # Simulate directory scanning + for i in range(10): # Unknown number of files + files_found += 1 + + # Report progress without total for indeterminate operations + await ctx.report_progress(progress=files_found) + + await asyncio.sleep(0.2) + + return {"files_found": files_found, "directory": directory} +``` + +### Multi-Stage Operations + +Break complex operations into stages with progress for each: + +```python +@mcp.tool +async def data_migration(source: str, destination: str, ctx: Context) -> str: + """Migrate data with multi-stage progress reporting.""" + + # Stage 1: Validation (0-25%) + await ctx.info("Validating source data") + for i in range(5): + await ctx.report_progress(progress=i * 5, total=100) + await asyncio.sleep(0.1) + + # Stage 2: Export (25-60%) + await ctx.info("Exporting data from source") + for i in range(7): + progress = 25 + (i * 5) + await ctx.report_progress(progress=progress, total=100) + await asyncio.sleep(0.1) + + # Stage 3: Transform (60-80%) + await ctx.info("Transforming data format") + for i in range(4): + progress = 60 + (i * 5) + await ctx.report_progress(progress=progress, total=100) + await asyncio.sleep(0.1) + + # Stage 4: Import (80-100%) + await ctx.info("Importing to destination") + for i in range(4): + progress = 80 + (i * 5) + await ctx.report_progress(progress=progress, total=100) + await asyncio.sleep(0.1) + + # Final completion + await ctx.report_progress(progress=100, total=100) + + return f"Migration from {source} to {destination} completed" +``` + + +## Client Requirements + +Progress reporting requires clients to support progress handling: + +- Clients must send a `progressToken` in the initial request to receive progress updates +- If no progress token is provided, progress calls will have no effect (they won't error) +- See [Client Progress](/clients/progress) for details on implementing client-side progress handling diff --git a/docs/servers/prompts.mdx b/docs/servers/prompts.mdx new file mode 100644 index 000000000..c57ed571b --- /dev/null +++ b/docs/servers/prompts.mdx @@ -0,0 +1,343 @@ +--- +title: Prompts +sidebarTitle: Prompts +description: Create reusable, parameterized prompt templates for MCP clients. +icon: message-lines +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + +Prompts are reusable message templates that help LLMs generate structured, purposeful responses. FastMCP simplifies defining these templates, primarily using the `@mcp.prompt` decorator. + +## What Are Prompts? + +Prompts provide parameterized message templates for LLMs. When a client requests a prompt: + +1. FastMCP finds the corresponding prompt definition. +2. If it has parameters, they are validated against your function signature. +3. Your function executes with the validated inputs. +4. The generated message(s) are returned to the LLM to guide its response. + +This allows you to define consistent, reusable templates that LLMs can use across different clients and contexts. + +## Prompts + +### The `@prompt` Decorator + +The most common way to define a prompt is by decorating a Python function. The decorator uses the function name as the prompt's identifier. + +```python +from fastmcp import FastMCP +from fastmcp.prompts.prompt import Message, PromptMessage, TextContent + +mcp = FastMCP(name="PromptServer") + +# Basic prompt returning a string (converted to user message automatically) +@mcp.prompt +def ask_about_topic(topic: str) -> str: + """Generates a user message asking for an explanation of a topic.""" + return f"Can you please explain the concept of '{topic}'?" + +# Prompt returning a specific message type +@mcp.prompt +def generate_code_request(language: str, task_description: str) -> PromptMessage: + """Generates a user message requesting code generation.""" + content = f"Write a {language} function that performs the following task: {task_description}" + return PromptMessage(role="user", content=TextContent(type="text", text=content)) +``` + +**Key Concepts:** + +* **Name:** By default, the prompt name is taken from the function name. +* **Parameters:** The function parameters define the inputs needed to generate the prompt. +* **Inferred Metadata:** By default: + * Prompt Name: Taken from the function name (`ask_about_topic`). + * Prompt Description: Taken from the function's docstring. + +Functions with `*args` or `**kwargs` are not supported as prompts. This restriction exists because FastMCP needs to generate a complete parameter schema for the MCP protocol, which isn't possible with variable argument lists. + + +#### Decorator Arguments + +While FastMCP infers the name and description from your function, you can override these and add additional metadata using arguments to the `@mcp.prompt` decorator: + +```python +@mcp.prompt( + name="analyze_data_request", # Custom prompt name + description="Creates a request to analyze data with specific parameters", # Custom description + tags={"analysis", "data"}, # Optional categorization tags + meta={"version": "1.1", "author": "data-team"} # Custom metadata +) +def data_analysis_prompt( + data_uri: str = Field(description="The URI of the resource containing the data."), + analysis_type: str = Field(default="summary", description="Type of analysis.") +) -> str: + """This docstring is ignored when description is provided.""" + return f"Please perform a '{analysis_type}' analysis on the data found at {data_uri}." +``` + + + + Sets the explicit prompt name exposed via MCP. If not provided, uses the function name + + + + Provides the description exposed via MCP. If set, the function's docstring is ignored for this purpose + + + + A set of strings used to categorize the prompt. These can be used by the server and, in some cases, by clients to filter or group available prompts. + + + + A boolean to enable or disable the prompt. See [Disabling Prompts](#disabling-prompts) for more information + + + + + + Optional meta information about the prompt. This data is passed through to the MCP client as the `_meta` field of the client-side prompt object and can be used for custom metadata, versioning, or other application-specific purposes. + + + +### Argument Types + + + +The MCP specification requires that all prompt arguments be passed as strings, but FastMCP allows you to use typed annotations for better developer experience. When you use complex types like `list[int]` or `dict[str, str]`, FastMCP: + +1. **Automatically converts** string arguments from MCP clients to the expected types +2. **Generates helpful descriptions** showing the exact JSON string format needed +3. **Preserves direct usage** - you can still call prompts with properly typed arguments + +Since the MCP specification only allows string arguments, clients need to know what string format to use for complex types. FastMCP solves this by automatically enhancing the argument descriptions with JSON schema information, making it clear to both humans and LLMs how to format their arguments. + + + +```python Python Code +@mcp.prompt +def analyze_data( + numbers: list[int], + metadata: dict[str, str], + threshold: float +) -> str: + """Analyze numerical data.""" + avg = sum(numbers) / len(numbers) + return f"Average: {avg}, above threshold: {avg > threshold}" +``` + +```json Resulting MCP Prompt +{ + "name": "analyze_data", + "description": "Analyze numerical data.", + "arguments": [ + { + "name": "numbers", + "description": "Provide as a JSON string matching the following schema: {\"items\":{\"type\":\"integer\"},\"type\":\"array\"}", + "required": true + }, + { + "name": "metadata", + "description": "Provide as a JSON string matching the following schema: {\"additionalProperties\":{\"type\":\"string\"},\"type\":\"object\"}", + "required": true + }, + { + "name": "threshold", + "description": "Provide as a JSON string matching the following schema: {\"type\":\"number\"}", + "required": true + } + ] +} +``` + + + +**MCP clients will call this prompt with string arguments:** +```json +{ + "numbers": "[1, 2, 3, 4, 5]", + "metadata": "{\"source\": \"api\", \"version\": \"1.0\"}", + "threshold": "2.5" +} +``` + +**But you can still call it directly with proper types:** +```python +# This also works for direct calls +result = await prompt.render({ + "numbers": [1, 2, 3, 4, 5], + "metadata": {"source": "api", "version": "1.0"}, + "threshold": 2.5 +}) +``` + + +Keep your type annotations simple when using this feature. Complex nested types or custom classes may not convert reliably from JSON strings. The automatically generated schema descriptions are the only guidance users receive about the expected format. + +Good choices: `list[int]`, `dict[str, str]`, `float`, `bool` +Avoid: Complex Pydantic models, deeply nested structures, custom classes + + +### Return Values + +FastMCP intelligently handles different return types from your prompt function: + +- **`str`**: Automatically converted to a single `PromptMessage`. +- **`PromptMessage`**: Used directly as provided. (Note a more user-friendly `Message` constructor is available that can accept raw strings instead of `TextContent` objects.) +- **`list[PromptMessage | str]`**: Used as a sequence of messages (a conversation). +- **`Any`**: If the return type is not one of the above, the return value is attempted to be converted to a string and used as a `PromptMessage`. + +```python +from fastmcp.prompts.prompt import Message, PromptResult + +@mcp.prompt +def roleplay_scenario(character: str, situation: str) -> PromptResult: + """Sets up a roleplaying scenario with initial messages.""" + return [ + Message(f"Let's roleplay. You are {character}. The situation is: {situation}"), + Message("Okay, I understand. I am ready. What happens next?", role="assistant") + ] +``` + + +### Required vs. Optional Parameters + +Parameters in your function signature are considered **required** unless they have a default value. + +```python +@mcp.prompt +def data_analysis_prompt( + data_uri: str, # Required - no default value + analysis_type: str = "summary", # Optional - has default value + include_charts: bool = False # Optional - has default value +) -> str: + """Creates a request to analyze data with specific parameters.""" + prompt = f"Please perform a '{analysis_type}' analysis on the data found at {data_uri}." + if include_charts: + prompt += " Include relevant charts and visualizations." + return prompt +``` + +In this example, the client *must* provide `data_uri`. If `analysis_type` or `include_charts` are omitted, their default values will be used. + +### Disabling Prompts + + + +You can control the visibility and availability of prompts by enabling or disabling them. Disabled prompts will not appear in the list of available prompts, and attempting to call a disabled prompt will result in an "Unknown prompt" error. + +By default, all prompts are enabled. You can disable a prompt upon creation using the `enabled` parameter in the decorator: + +```python +@mcp.prompt(enabled=False) +def experimental_prompt(): + """This prompt is not ready for use.""" + return "This is an experimental prompt." +``` + +You can also toggle a prompt's state programmatically after it has been created: + +```python +@mcp.prompt +def seasonal_prompt(): return "Happy Holidays!" + +# Disable and re-enable the prompt +seasonal_prompt.disable() +seasonal_prompt.enable() +``` + +### Async Prompts + +FastMCP seamlessly supports both standard (`def`) and asynchronous (`async def`) functions as prompts. + +```python +# Synchronous prompt +@mcp.prompt +def simple_question(question: str) -> str: + """Generates a simple question to ask the LLM.""" + return f"Question: {question}" + +# Asynchronous prompt +@mcp.prompt +async def data_based_prompt(data_id: str) -> str: + """Generates a prompt based on data that needs to be fetched.""" + # In a real scenario, you might fetch data from a database or API + async with aiohttp.ClientSession() as session: + async with session.get(f"https://api.example.com/data/{data_id}") as response: + data = await response.json() + return f"Analyze this data: {data['content']}" +``` + +Use `async def` when your prompt function performs I/O operations like network requests, database queries, file I/O, or external service calls. + +### Accessing MCP Context + + + +Prompts can access additional MCP information and features through the `Context` object. To access it, add a parameter to your prompt function with a type annotation of `Context`: + +```python {6} +from fastmcp import FastMCP, Context + +mcp = FastMCP(name="PromptServer") + +@mcp.prompt +async def generate_report_request(report_type: str, ctx: Context) -> str: + """Generates a request for a report.""" + return f"Please create a {report_type} report. Request ID: {ctx.request_id}" +``` + +For full documentation on the Context object and all its capabilities, see the [Context documentation](/servers/context). + +### Notifications + + + +FastMCP automatically sends `notifications/prompts/list_changed` notifications to connected clients when prompts are added, enabled, or disabled. This allows clients to stay up-to-date with the current prompt set without manually polling for changes. + +```python +@mcp.prompt +def example_prompt() -> str: + return "Hello!" + +# These operations trigger notifications: +mcp.add_prompt(example_prompt) # Sends prompts/list_changed notification +example_prompt.disable() # Sends prompts/list_changed notification +example_prompt.enable() # Sends prompts/list_changed notification +``` + +Notifications are only sent when these operations occur within an active MCP request context (e.g., when called from within a tool or other MCP operation). Operations performed during server initialization do not trigger notifications. + +Clients can handle these notifications using a [message handler](/clients/messages) to automatically refresh their prompt lists or update their interfaces. + +## Server Behavior + +### Duplicate Prompts + + + +You can configure how the FastMCP server handles attempts to register multiple prompts with the same name. Use the `on_duplicate_prompts` setting during `FastMCP` initialization. + +```python +from fastmcp import FastMCP + +mcp = FastMCP( + name="PromptServer", + on_duplicate_prompts="error" # Raise an error if a prompt name is duplicated +) + +@mcp.prompt +def greeting(): return "Hello, how can I help you today?" + +# This registration attempt will raise a ValueError because +# "greeting" is already registered and the behavior is "error". +# @mcp.prompt +# def greeting(): return "Hi there! What can I do for you?" +``` + +The duplicate behavior options are: + +- `"warn"` (default): Logs a warning, and the new prompt replaces the old one. +- `"error"`: Raises a `ValueError`, preventing the duplicate registration. +- `"replace"`: Silently replaces the existing prompt with the new one. +- `"ignore"`: Keeps the original prompt and ignores the new registration attempt. \ No newline at end of file diff --git a/docs/servers/proxy.mdx b/docs/servers/proxy.mdx new file mode 100644 index 000000000..82daccb5d --- /dev/null +++ b/docs/servers/proxy.mdx @@ -0,0 +1,335 @@ +--- +title: Proxy Servers +sidebarTitle: Proxy Servers +description: Use FastMCP to act as an intermediary or change transport for other MCP servers. +icon: arrows-retweet +--- +import { VersionBadge } from '/snippets/version-badge.mdx' + + + +FastMCP provides a powerful proxying capability that allows one FastMCP server instance to act as a frontend for another MCP server (which could be remote, running on a different transport, or even another FastMCP instance). This is achieved using the `FastMCP.as_proxy()` class method. + +## What is Proxying? + +Proxying means setting up a FastMCP server that doesn't implement its own tools or resources directly. Instead, when it receives a request (like `tools/call` or `resources/read`), it forwards that request to a *backend* MCP server, receives the response, and then relays that response back to the original client. + +```mermaid +sequenceDiagram + participant ClientApp as Your Client (e.g., Claude Desktop) + participant FastMCPProxy as FastMCP Proxy Server + participant BackendServer as Backend MCP Server (e.g., remote SSE) + + ClientApp->>FastMCPProxy: MCP Request (e.g. stdio) + Note over FastMCPProxy, BackendServer: Proxy forwards the request + FastMCPProxy->>BackendServer: MCP Request (e.g. sse) + BackendServer-->>FastMCPProxy: MCP Response (e.g. sse) + Note over ClientApp, FastMCPProxy: Proxy relays the response + FastMCPProxy-->>ClientApp: MCP Response (e.g. stdio) +``` + +### Key Benefits + + + +- **Session Isolation**: Each request gets its own isolated session, ensuring safe concurrent operations +- **Transport Bridging**: Expose servers running on one transport via a different transport +- **Advanced MCP Features**: Automatic forwarding of sampling, elicitation, logging, and progress +- **Security**: Acts as a controlled gateway to backend servers +- **Simplicity**: Single endpoint even if backend location or transport changes + +### Performance Considerations + +When using proxy servers, especially those connecting to HTTP-based backend servers, be aware that latency can be significant. Operations like `list_tools()` may take hundreds of milliseconds compared to 1-2ms for local tools. When mounting proxy servers, this latency affects all operations on the parent server, not just interactions with the proxied tools. + +If low latency is a requirement for your use-case, consider using [`import_server()`](/servers/composition#importing-static-composition) to copy tools at startup rather than proxying them at runtime. + +## Quick Start + + + +The recommended way to create a proxy is using `ProxyClient`, which provides full MCP feature support with automatic session isolation: + +```python +from fastmcp import FastMCP +from fastmcp.server.proxy import ProxyClient + +# Create a proxy with full MCP feature support +proxy = FastMCP.as_proxy( + ProxyClient("backend_server.py"), + name="MyProxy" +) + +# Run the proxy (e.g., via stdio for Claude Desktop) +if __name__ == "__main__": + proxy.run() +``` + +This single setup gives you: +- Safe concurrent request handling +- Automatic forwarding of advanced MCP features (sampling, elicitation, etc.) +- Session isolation to prevent context mixing +- Full compatibility with all MCP clients + +You can also pass a FastMCP [client transport](/clients/transports) (or parameter that can be inferred to a transport) to `as_proxy()`. This will automatically create a `ProxyClient` instance for you. + +Finally, you can pass a regular FastMCP `Client` instance to `as_proxy()`. This will work for many use cases, but may break if advanced MCP features like sampling or elicitation are invoked by the server. + +## Session Isolation & Concurrency + + + +FastMCP proxies provide session isolation to ensure safe concurrent operations. The session strategy depends on how the proxy is configured: + +### Fresh Sessions + +When you pass a disconnected client (which is the normal case), each request gets its own isolated backend session: + +```python +from fastmcp.server.proxy import ProxyClient + +# Each request creates a fresh backend session (recommended) +proxy = FastMCP.as_proxy(ProxyClient("backend_server.py")) + +# Multiple clients can use this proxy simultaneously without interference: +# - Client A calls a tool -> gets isolated backend session +# - Client B calls a tool -> gets different isolated backend session +# - No context mixing between requests +``` + +### Session Reuse with Connected Clients + +When you pass an already-connected client, the proxy will reuse that session for all requests: + +```python +from fastmcp import Client + +# Create and connect a client +async with Client("backend_server.py") as connected_client: + # This proxy will reuse the connected session for all requests + proxy = FastMCP.as_proxy(connected_client) + + # โš ๏ธ Warning: All requests share the same backend session + # This may cause context mixing in concurrent scenarios +``` + +**Important**: Using shared sessions with concurrent requests from multiple clients may lead to context mixing and race conditions. This approach should only be used in single-threaded scenarios or when you have explicit synchronization. + +## Transport Bridging + +A common use case is bridging transports - exposing a server running on one transport via a different transport. For example, making a remote SSE server available locally via stdio: + +```python +from fastmcp import FastMCP +from fastmcp.server.proxy import ProxyClient + +# Bridge remote SSE server to local stdio +remote_proxy = FastMCP.as_proxy( + ProxyClient("http://example.com/mcp/sse"), + name="Remote-to-Local Bridge" +) + +# Run locally via stdio for Claude Desktop +if __name__ == "__main__": + remote_proxy.run() # Defaults to stdio transport +``` + +Or expose a local server via HTTP for remote access: + +```python +# Bridge local server to HTTP +local_proxy = FastMCP.as_proxy( + ProxyClient("local_server.py"), + name="Local-to-HTTP Bridge" +) + +# Run via HTTP for remote clients +if __name__ == "__main__": + local_proxy.run(transport="http", host="0.0.0.0", port=8080) +``` + + +## Advanced MCP Features + + + +`ProxyClient` automatically forwards advanced MCP protocol features between the backend server and clients connected to the proxy, ensuring full MCP compatibility. + +### Supported Features + +- **Roots**: Forwards filesystem root access requests to the client +- **Sampling**: Forwards LLM completion requests from backend to client +- **Elicitation**: Forwards user input requests to the client +- **Logging**: Forwards log messages from backend through to client +- **Progress**: Forwards progress notifications during long operations + +```python +from fastmcp.server.proxy import ProxyClient + +# ProxyClient automatically handles all these features +backend = ProxyClient("advanced_backend.py") +proxy = FastMCP.as_proxy(backend) + +# When the backend server: +# - Requests LLM sampling -> forwarded to your client +# - Logs messages -> appear in your client +# - Reports progress -> shown in your client +# - Needs user input -> prompts your client +``` + +### Customizing Feature Support + +You can selectively disable forwarding by passing `None` for specific handlers: + +```python +# Disable sampling but keep other features +backend = ProxyClient( + "backend_server.py", + sampling_handler=None, # Disable LLM sampling forwarding + log_handler=None # Disable log forwarding +) +``` + +When you use a transport string directly with `FastMCP.as_proxy()`, it automatically creates a `ProxyClient` internally to ensure full feature support. + +## Configuration-Based Proxies + + + +You can create a proxy directly from a configuration dictionary that follows the MCPConfig schema. This is useful for quickly setting up proxies to remote servers without manually configuring each connection detail. + +```python +from fastmcp import FastMCP + +# Create a proxy directly from a config dictionary +config = { + "mcpServers": { + "default": { # For single server configs, 'default' is commonly used + "url": "https://example.com/mcp", + "transport": "http" + } + } +} + +# Create a proxy to the configured server (auto-creates ProxyClient) +proxy = FastMCP.as_proxy(config, name="Config-Based Proxy") + +# Run the proxy with stdio transport for local access +if __name__ == "__main__": + proxy.run() +``` + + +The MCPConfig format follows an emerging standard for MCP server configuration and may evolve as the specification matures. While FastMCP aims to maintain compatibility with future versions, be aware that field names or structure might change. + + +### Multi-Server Configurations + +You can create a proxy to multiple servers by specifying multiple entries in the config. They are automatically mounted with their config names as prefixes: + +```python +# Multi-server configuration +config = { + "mcpServers": { + "weather": { + "url": "https://weather-api.example.com/mcp", + "transport": "http" + }, + "calendar": { + "url": "https://calendar-api.example.com/mcp", + "transport": "http" + } + } +} + +# Create a unified proxy to multiple servers +composite_proxy = FastMCP.as_proxy(config, name="Composite Proxy") + +# Tools and resources are accessible with prefixes: +# - weather_get_forecast, calendar_add_event +# - weather://weather/icons/sunny, calendar://calendar/events/today +``` + +## Mirrored Components + + + +When you access tools, resources, or prompts from a proxy server, they are "mirrored" from the remote server. Mirrored components cannot be modified directly since they reflect the state of the remote server. For example, you can not simply "disable" a mirrored component. + +However, you can create a copy of a mirrored component and store it as a new locally-defined component. Local components always take precedence over mirrored ones because the proxy server will check its own registry before it attempts to engage the remote server. + +Therefore, to enable or disable a proxy tool, resource, or prompt, you should first create a local copy and add it to your own server. Here's an example of how to do that for a tool: + +```python +# Create your own server +my_server = FastMCP("MyServer") + +# Get a proxy server +proxy = FastMCP.as_proxy("backend_server.py") + +# Get mirrored components from proxy +mirrored_tool = await proxy.get_tool("useful_tool") + +# Create a local copy that you can modify +local_tool = mirrored_tool.copy() + +# Add the local copy to your server +my_server.add_tool(local_tool) + +# Now you can disable YOUR copy +local_tool.disable() +``` + + +## `FastMCPProxy` Class + +Internally, `FastMCP.as_proxy()` uses the `FastMCPProxy` class. You generally don't need to interact with this class directly, but it's available if needed for advanced scenarios. + +### Direct Usage + +```python +from fastmcp.server.proxy import FastMCPProxy, ProxyClient + +# Provide a client factory for explicit session control +def create_client(): + return ProxyClient("backend_server.py") + +proxy = FastMCPProxy(client_factory=create_client) +``` + +### Parameters + +- **`client`**: **[DEPRECATED]** A `Client` instance. Use `client_factory` instead for explicit session management. +- **`client_factory`**: A callable that returns a `Client` instance when called. This gives you full control over session creation and reuse strategies. + +### Explicit Session Management + +`FastMCPProxy` requires explicit session management - no automatic detection is performed. You must choose your session strategy: + +```python +# Share session across all requests (be careful with concurrency) +shared_client = ProxyClient("backend_server.py") +def shared_session_factory(): + return shared_client + +proxy = FastMCPProxy(client_factory=shared_session_factory) + +# Create fresh sessions per request (recommended) +def fresh_session_factory(): + return ProxyClient("backend_server.py") + +proxy = FastMCPProxy(client_factory=fresh_session_factory) +``` + +For automatic session strategy selection, use the convenience method `FastMCP.as_proxy()` instead. + +```python +# Custom factory with specific configuration +def custom_client_factory(): + client = ProxyClient("backend_server.py") + # Add any custom configuration here + return client + +proxy = FastMCPProxy(client_factory=custom_client_factory) +``` + diff --git a/docs/servers/resources.mdx b/docs/servers/resources.mdx new file mode 100644 index 000000000..60d03a6c4 --- /dev/null +++ b/docs/servers/resources.mdx @@ -0,0 +1,657 @@ +--- +title: Resources & Templates +sidebarTitle: Resources +description: Expose data sources and dynamic content generators to your MCP client. +icon: folder-open +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + +Resources represent data or files that an MCP client can read, and resource templates extend this concept by allowing clients to request dynamically generated resources based on parameters passed in the URI. + +FastMCP simplifies defining both static and dynamic resources, primarily using the `@mcp.resource` decorator. + +## What Are Resources? + +Resources provide read-only access to data for the LLM or client application. When a client requests a resource URI: + +1. FastMCP finds the corresponding resource definition. +2. If it's dynamic (defined by a function), the function is executed. +3. The content (text, JSON, binary data) is returned to the client. + +This allows LLMs to access files, database content, configuration, or dynamically generated information relevant to the conversation. + +## Resources + +### The `@resource` Decorator + +The most common way to define a resource is by decorating a Python function. The decorator requires the resource's unique URI. + +```python +import json +from fastmcp import FastMCP + +mcp = FastMCP(name="DataServer") + +# Basic dynamic resource returning a string +@mcp.resource("resource://greeting") +def get_greeting() -> str: + """Provides a simple greeting message.""" + return "Hello from FastMCP Resources!" + +# Resource returning JSON data (dict is auto-serialized) +@mcp.resource("data://config") +def get_config() -> dict: + """Provides application configuration as JSON.""" + return { + "theme": "dark", + "version": "1.2.0", + "features": ["tools", "resources"], + } +``` + +**Key Concepts:** + +* **URI:** The first argument to `@resource` is the unique URI (e.g., `"resource://greeting"`) clients use to request this data. +* **Lazy Loading:** The decorated function (`get_greeting`, `get_config`) is only executed when a client specifically requests that resource URI via `resources/read`. +* **Inferred Metadata:** By default: + * Resource Name: Taken from the function name (`get_greeting`). + * Resource Description: Taken from the function's docstring. + +#### Decorator Arguments + +You can customize the resource's properties using arguments in the `@mcp.resource` decorator: + +```python +from fastmcp import FastMCP + +mcp = FastMCP(name="DataServer") + +# Example specifying metadata +@mcp.resource( + uri="data://app-status", # Explicit URI (required) + name="ApplicationStatus", # Custom name + description="Provides the current status of the application.", # Custom description + mime_type="application/json", # Explicit MIME type + tags={"monitoring", "status"}, # Categorization tags + meta={"version": "2.1", "team": "infrastructure"} # Custom metadata +) +def get_application_status() -> dict: + """Internal function description (ignored if description is provided above).""" + return {"status": "ok", "uptime": 12345, "version": mcp.settings.version} # Example usage +``` + + + + The unique identifier for the resource + + + + A human-readable name. If not provided, defaults to function name + + + + Explanation of the resource. If not provided, defaults to docstring + + + + Specifies the content type. FastMCP often infers a default like `text/plain` or `application/json`, but explicit is better for non-text types + + + + A set of strings used to categorize the resource. These can be used by the server and, in some cases, by clients to filter or group available resources. + + + + A boolean to enable or disable the resource. See [Disabling Resources](#disabling-resources) for more information + + + + An optional `Annotations` object or dictionary to add additional metadata about the resource. + + + If true, the resource is read-only and does not modify its environment. + + + If true, reading the resource repeatedly will have no additional effect on its environment. + + + + + + + + Optional meta information about the resource. This data is passed through to the MCP client as the `_meta` field of the client-side resource object and can be used for custom metadata, versioning, or other application-specific purposes. + + + +### Return Values + +FastMCP automatically converts your function's return value into the appropriate MCP resource content: + +- **`str`**: Sent as `TextResourceContents` (with `mime_type="text/plain"` by default). +- **`dict`, `list`, `pydantic.BaseModel`**: Automatically serialized to a JSON string and sent as `TextResourceContents` (with `mime_type="application/json"` by default). +- **`bytes`**: Base64 encoded and sent as `BlobResourceContents`. You should specify an appropriate `mime_type` (e.g., `"image/png"`, `"application/octet-stream"`). +- **`None`**: Results in an empty resource content list being returned. + +### Disabling Resources + + + +You can control the visibility and availability of resources and templates by enabling or disabling them. Disabled resources will not appear in the list of available resources or templates, and attempting to read a disabled resource will result in an "Unknown resource" error. + +By default, all resources are enabled. You can disable a resource upon creation using the `enabled` parameter in the decorator: + +```python +@mcp.resource("data://secret", enabled=False) +def get_secret_data(): + """This resource is currently disabled.""" + return "Secret data" +``` + +You can also toggle a resource's state programmatically after it has been created: + +```python +@mcp.resource("data://config") +def get_config(): return {"version": 1} + +# Disable and re-enable the resource +get_config.disable() +get_config.enable() +``` + + +### Accessing MCP Context + + + +Resources and resource templates can access additional MCP information and features through the `Context` object. To access it, add a parameter to your resource function with a type annotation of `Context`: + +```python {6, 14} +from fastmcp import FastMCP, Context + +mcp = FastMCP(name="DataServer") + +@mcp.resource("resource://system-status") +async def get_system_status(ctx: Context) -> dict: + """Provides system status information.""" + return { + "status": "operational", + "request_id": ctx.request_id + } + +@mcp.resource("resource://{name}/details") +async def get_details(name: str, ctx: Context) -> dict: + """Get details for a specific name.""" + return { + "name": name, + "accessed_at": ctx.request_id + } +``` + +For full documentation on the Context object and all its capabilities, see the [Context documentation](/servers/context). + + +### Async Resources + +Use `async def` for resource functions that perform I/O operations (e.g., reading from a database or network) to avoid blocking the server. + +```python +import aiofiles +from fastmcp import FastMCP + +mcp = FastMCP(name="DataServer") + +@mcp.resource("file:///app/data/important_log.txt", mime_type="text/plain") +async def read_important_log() -> str: + """Reads content from a specific log file asynchronously.""" + try: + async with aiofiles.open("/app/data/important_log.txt", mode="r") as f: + content = await f.read() + return content + except FileNotFoundError: + return "Log file not found." +``` + + +### Resource Classes + +While `@mcp.resource` is ideal for dynamic content, you can directly register pre-defined resources (like static files or simple text) using `mcp.add_resource()` and concrete `Resource` subclasses. + +```python +from pathlib import Path +from fastmcp import FastMCP +from fastmcp.resources import FileResource, TextResource, DirectoryResource + +mcp = FastMCP(name="DataServer") + +# 1. Exposing a static file directly +readme_path = Path("./README.md").resolve() +if readme_path.exists(): + # Use a file:// URI scheme + readme_resource = FileResource( + uri=f"file://{readme_path.as_posix()}", + path=readme_path, # Path to the actual file + name="README File", + description="The project's README.", + mime_type="text/markdown", + tags={"documentation"} + ) + mcp.add_resource(readme_resource) + +# 2. Exposing simple, predefined text +notice_resource = TextResource( + uri="resource://notice", + name="Important Notice", + text="System maintenance scheduled for Sunday.", + tags={"notification"} +) +mcp.add_resource(notice_resource) + +# 3. Using a custom key different from the URI +special_resource = TextResource( + uri="resource://common-notice", + name="Special Notice", + text="This is a special notice with a custom storage key.", +) +mcp.add_resource(special_resource, key="resource://custom-key") + +# 4. Exposing a directory listing +data_dir_path = Path("./app_data").resolve() +if data_dir_path.is_dir(): + data_listing_resource = DirectoryResource( + uri="resource://data-files", + path=data_dir_path, # Path to the directory + name="Data Directory Listing", + description="Lists files available in the data directory.", + recursive=False # Set to True to list subdirectories + ) + mcp.add_resource(data_listing_resource) # Returns JSON list of files +``` + +**Common Resource Classes:** + +- `TextResource`: For simple string content. +- `BinaryResource`: For raw `bytes` content. +- `FileResource`: Reads content from a local file path. Handles text/binary modes and lazy reading. +- `HttpResource`: Fetches content from an HTTP(S) URL (requires `httpx`). +- `DirectoryResource`: Lists files in a local directory (returns JSON). +- (`FunctionResource`: Internal class used by `@mcp.resource`). + +Use these when the content is static or sourced directly from a file/URL, bypassing the need for a dedicated Python function. + +#### Custom Resource Keys + + + +When adding resources directly with `mcp.add_resource()`, you can optionally provide a custom storage key: + +```python +# Creating a resource with standard URI as the key +resource = TextResource(uri="resource://data") +mcp.add_resource(resource) # Will be stored and accessed using "resource://data" + +# Creating a resource with a custom key +special_resource = TextResource(uri="resource://special-data") +mcp.add_resource(special_resource, key="internal://data-v2") # Will be stored and accessed using "internal://data-v2" +``` + +Note that this parameter is only available when using `add_resource()` directly and not through the `@resource` decorator, as URIs are provided explicitly when using the decorator. + +### Notifications + + + +FastMCP automatically sends `notifications/resources/list_changed` notifications to connected clients when resources or templates are added, enabled, or disabled. This allows clients to stay up-to-date with the current resource set without manually polling for changes. + +```python +@mcp.resource("data://example") +def example_resource() -> str: + return "Hello!" + +# These operations trigger notifications: +mcp.add_resource(example_resource) # Sends resources/list_changed notification +example_resource.disable() # Sends resources/list_changed notification +example_resource.enable() # Sends resources/list_changed notification +``` + +Notifications are only sent when these operations occur within an active MCP request context (e.g., when called from within a tool or other MCP operation). Operations performed during server initialization do not trigger notifications. + +Clients can handle these notifications using a [message handler](/clients/messages) to automatically refresh their resource lists or update their interfaces. + +### Annotations + + + +FastMCP allows you to add specialized metadata to your resources through annotations. These annotations communicate how resources behave to client applications without consuming token context in LLM prompts. + +Annotations serve several purposes in client applications: +- Indicating whether resources are read-only or may have side effects +- Describing the safety profile of resources (idempotent vs. non-idempotent) +- Helping clients optimize caching and access patterns + +You can add annotations to a resource using the `annotations` parameter in the `@mcp.resource` decorator: + +```python +@mcp.resource( + "data://config", + annotations={ + "readOnlyHint": True, + "idempotentHint": True + } +) +def get_config() -> dict: + """Get application configuration.""" + return {"version": "1.0", "debug": False} +``` + +FastMCP supports these standard annotations: + +| Annotation | Type | Default | Purpose | +| :--------- | :--- | :------ | :------ | +| `readOnlyHint` | boolean | true | Indicates if the resource only provides data without side effects | +| `idempotentHint` | boolean | true | Indicates if repeated reads have the same effect as a single read | + +Remember that annotations help make better user experiences but should be treated as advisory hints. They help client applications present appropriate UI elements and optimize access patterns, but won't enforce behavior on their own. Always focus on making your annotations accurately represent what your resource actually does. + +## Resource Templates + +Resource Templates allow clients to request resources whose content depends on parameters embedded in the URI. Define a template using the **same `@mcp.resource` decorator**, but include `{parameter_name}` placeholders in the URI string and add corresponding arguments to your function signature. + +Resource templates share most configuration options with regular resources (name, description, mime_type, tags, annotations), but add the ability to define URI parameters that map to function parameters. + +Resource templates generate a new resource for each unique set of parameters, which means that resources can be dynamically created on-demand. For example, if the resource template `"user://profile/{name}"` is registered, MCP clients could request `"user://profile/ford"` or `"user://profile/marvin"` to retrieve either of those two user profiles as resources, without having to register each resource individually. + + +Functions with `*args` are not supported as resource templates. However, unlike tools and prompts, resource templates do support `**kwargs` because the URI template defines specific parameter names that will be collected and passed as keyword arguments. + + +Here is a complete example that shows how to define two resource templates: + +```python +from fastmcp import FastMCP + +mcp = FastMCP(name="DataServer") + +# Template URI includes {city} placeholder +@mcp.resource("weather://{city}/current") +def get_weather(city: str) -> dict: + """Provides weather information for a specific city.""" + # In a real implementation, this would call a weather API + # Here we're using simplified logic for example purposes + return { + "city": city.capitalize(), + "temperature": 22, + "condition": "Sunny", + "unit": "celsius" + } + +# Template with multiple parameters and annotations +@mcp.resource( + "repos://{owner}/{repo}/info", + annotations={ + "readOnlyHint": True, + "idempotentHint": True + } +) +def get_repo_info(owner: str, repo: str) -> dict: + """Retrieves information about a GitHub repository.""" + # In a real implementation, this would call the GitHub API + return { + "owner": owner, + "name": repo, + "full_name": f"{owner}/{repo}", + "stars": 120, + "forks": 48 + } +``` + +With these two templates defined, clients can request a variety of resources: +- `weather://london/current` โ†’ Returns weather for London +- `weather://paris/current` โ†’ Returns weather for Paris +- `repos://jlowin/fastmcp/info` โ†’ Returns info about the jlowin/fastmcp repository +- `repos://prefecthq/prefect/info` โ†’ Returns info about the prefecthq/prefect repository + +### RFC 6570 URI Templates + + +FastMCP implements [RFC 6570 URI Templates](https://datatracker.ietf.org/doc/html/rfc6570) for resource templates, providing a standardized way to define parameterized URIs. This includes support for simple expansion, wildcard path parameters, and form-style query parameters. + +#### Wildcard Parameters + + + +Resource templates support wildcard parameters that can match multiple path segments. While standard parameters (`{param}`) only match a single path segment and don't cross "/" boundaries, wildcard parameters (`{param*}`) can capture multiple segments including slashes. Wildcards capture all subsequent path segments *up until* the defined part of the URI template (whether literal or another parameter). This allows you to have multiple wildcard parameters in a single URI template. + +```python {15, 23} +from fastmcp import FastMCP + +mcp = FastMCP(name="DataServer") + + +# Standard parameter only matches one segment +@mcp.resource("files://{filename}") +def get_file(filename: str) -> str: + """Retrieves a file by name.""" + # Will only match files:// + return f"File content for: {filename}" + + +# Wildcard parameter can match multiple segments +@mcp.resource("path://{filepath*}") +def get_path_content(filepath: str) -> str: + """Retrieves content at a specific path.""" + # Can match path://docs/server/resources.mdx + return f"Content at path: {filepath}" + + +# Mixing standard and wildcard parameters +@mcp.resource("repo://{owner}/{path*}/template.py") +def get_template_file(owner: str, path: str) -> dict: + """Retrieves a file from a specific repository and path, but + only if the resource ends with `template.py`""" + # Can match repo://jlowin/fastmcp/src/resources/template.py + return { + "owner": owner, + "path": path + "/template.py", + "content": f"File at {path}/template.py in {owner}'s repository" + } +``` + +Wildcard parameters are useful when: + +- Working with file paths or hierarchical data +- Creating APIs that need to capture variable-length path segments +- Building URL-like patterns similar to REST APIs + +Note that like regular parameters, each wildcard parameter must still be a named parameter in your function signature, and all required function parameters must appear in the URI template. + +#### Query Parameters + + + +FastMCP supports RFC 6570 form-style query parameters using the `{?param1,param2}` syntax. Query parameters provide a clean way to pass optional configuration to resources without cluttering the path. + +Query parameters must be optional function parameters (have default values), while path parameters map to required function parameters. This enforces a clear separation: required data goes in the path, optional configuration in query params. + +```python +from fastmcp import FastMCP + +mcp = FastMCP(name="DataServer") + +# Basic query parameters +@mcp.resource("data://{id}{?format}") +def get_data(id: str, format: str = "json") -> str: + """Retrieve data in specified format.""" + if format == "xml": + return f"" + return f'{{"id": "{id}"}}' + +# Multiple query parameters with type coercion +@mcp.resource("api://{endpoint}{?version,limit,offset}") +def call_api(endpoint: str, version: int = 1, limit: int = 10, offset: int = 0) -> dict: + """Call API endpoint with pagination.""" + return { + "endpoint": endpoint, + "version": version, + "limit": limit, + "offset": offset, + "results": fetch_results(endpoint, version, limit, offset) + } + +# Query parameters with wildcards +@mcp.resource("files://{path*}{?encoding,lines}") +def read_file(path: str, encoding: str = "utf-8", lines: int = 100) -> str: + """Read file with optional encoding and line limit.""" + return read_file_content(path, encoding, lines) +``` + +**Example requests:** +- `data://123` โ†’ Uses default format `"json"` +- `data://123?format=xml` โ†’ Uses format `"xml"` +- `api://users?version=2&limit=50` โ†’ `version=2, limit=50, offset=0` +- `files://src/main.py?encoding=ascii&lines=50` โ†’ Custom encoding and line limit + +FastMCP automatically coerces query parameter string values to the correct types based on your function's type hints (`int`, `float`, `bool`, `str`). + +**Query parameters vs. hidden defaults:** + +Query parameters expose optional configuration to clients. To hide optional parameters from clients entirely (always use defaults), simply omit them from the URI template: + +```python +# Clients CAN override max_results via query string +@mcp.resource("search://{query}{?max_results}") +def search_configurable(query: str, max_results: int = 10) -> dict: + return {"query": query, "limit": max_results} + +# Clients CANNOT override max_results (not in URI template) +@mcp.resource("search://{query}") +def search_fixed(query: str, max_results: int = 10) -> dict: + return {"query": query, "limit": max_results} +``` + +### Template Parameter Rules + + + +FastMCP enforces these validation rules when creating resource templates: + +1. **Required function parameters** (no default values) must appear in the URI path template +2. **Query parameters** (specified with `{?param}` syntax) must be optional function parameters with default values +3. **All URI template parameters** (path and query) must exist as function parameters + +Optional function parameters (those with default values) can be: +- Included as query parameters (`{?param}`) - clients can override via query string +- Omitted from URI template - always uses default value, not exposed to clients +- Used in alternative path templates - enables multiple ways to access the same resource + +**Multiple templates for one function:** + +Create multiple resource templates that expose the same function through different URI patterns by manually applying decorators: + +```python +from fastmcp import FastMCP + +mcp = FastMCP(name="DataServer") + +# Define a user lookup function that can be accessed by different identifiers +def lookup_user(name: str | None = None, email: str | None = None) -> dict: + """Look up a user by either name or email.""" + if email: + return find_user_by_email(email) # pseudocode + elif name: + return find_user_by_name(name) # pseudocode + else: + return {"error": "No lookup parameters provided"} + +# Manually apply multiple decorators to the same function +mcp.resource("users://email/{email}")(lookup_user) +mcp.resource("users://name/{name}")(lookup_user) +``` + +Now an LLM or client can retrieve user information in two different ways: +- `users://email/alice@example.com` โ†’ Looks up user by email (with name=None) +- `users://name/Bob` โ†’ Looks up user by name (with email=None) + +This approach allows a single function to be registered with multiple URI patterns while keeping the implementation clean and straightforward. + +Templates provide a powerful way to expose parameterized data access points following REST-like principles. + +## Error Handling + + + +If your resource function encounters an error, you can raise a standard Python exception (`ValueError`, `TypeError`, `FileNotFoundError`, custom exceptions, etc.) or a FastMCP `ResourceError`. + +By default, all exceptions (including their details) are logged and converted into an MCP error response to be sent back to the client LLM. This helps the LLM understand failures and react appropriately. + +If you want to mask internal error details for security reasons, you can: + +1. Use the `mask_error_details=True` parameter when creating your `FastMCP` instance: +```python +mcp = FastMCP(name="SecureServer", mask_error_details=True) +``` + +2. Or use `ResourceError` to explicitly control what error information is sent to clients: +```python +from fastmcp import FastMCP +from fastmcp.exceptions import ResourceError + +mcp = FastMCP(name="DataServer") + +@mcp.resource("resource://safe-error") +def fail_with_details() -> str: + """This resource provides detailed error information.""" + # ResourceError contents are always sent back to clients, + # regardless of mask_error_details setting + raise ResourceError("Unable to retrieve data: file not found") + +@mcp.resource("resource://masked-error") +def fail_with_masked_details() -> str: + """This resource masks internal error details when mask_error_details=True.""" + # This message would be masked if mask_error_details=True + raise ValueError("Sensitive internal file path: /etc/secrets.conf") + +@mcp.resource("data://{id}") +def get_data_by_id(id: str) -> dict: + """Template resources also support the same error handling pattern.""" + if id == "secure": + raise ValueError("Cannot access secure data") + elif id == "missing": + raise ResourceError("Data ID 'missing' not found in database") + return {"id": id, "value": "data"} +``` + +When `mask_error_details=True`, only error messages from `ResourceError` will include details, other exceptions will be converted to a generic message. + +## Server Behavior + +### Duplicate Resources + + + +You can configure how the FastMCP server handles attempts to register multiple resources or templates with the same URI. Use the `on_duplicate_resources` setting during `FastMCP` initialization. + +```python +from fastmcp import FastMCP + +mcp = FastMCP( + name="ResourceServer", + on_duplicate_resources="error" # Raise error on duplicates +) + +@mcp.resource("data://config") +def get_config_v1(): return {"version": 1} + +# This registration attempt will raise a ValueError because +# "data://config" is already registered and the behavior is "error". +# @mcp.resource("data://config") +# def get_config_v2(): return {"version": 2} +``` + +The duplicate behavior options are: + +- `"warn"` (default): Logs a warning, and the new resource/template replaces the old one. +- `"error"`: Raises a `ValueError`, preventing the duplicate registration. +- `"replace"`: Silently replaces the existing resource/template with the new one. +- `"ignore"`: Keeps the original resource/template and ignores the new registration attempt. \ No newline at end of file diff --git a/docs/servers/sampling.mdx b/docs/servers/sampling.mdx new file mode 100644 index 000000000..7682d8087 --- /dev/null +++ b/docs/servers/sampling.mdx @@ -0,0 +1,191 @@ +--- +title: LLM Sampling +sidebarTitle: Sampling +description: Request the client's LLM to generate text based on provided messages through the MCP context. +icon: robot +--- + +import { VersionBadge } from '/snippets/version-badge.mdx' + + + +LLM sampling allows MCP tools to request the client's LLM to generate text based on provided messages. This is useful when tools need to leverage the LLM's capabilities to process data, generate responses, or perform text-based analysis. + +## Why Use LLM Sampling? + +LLM sampling enables tools to: + +- **Leverage AI capabilities**: Use the client's LLM for text generation and analysis +- **Offload complex reasoning**: Let the LLM handle tasks requiring natural language understanding +- **Generate dynamic content**: Create responses, summaries, or transformations based on data +- **Maintain context**: Use the same LLM instance that the user is already interacting with + +### Basic Usage + +Use `ctx.sample()` to request text generation from the client's LLM: + +```python {14} +from fastmcp import FastMCP, Context + +mcp = FastMCP("SamplingDemo") + +@mcp.tool +async def analyze_sentiment(text: str, ctx: Context) -> dict: + """Analyze the sentiment of text using the client's LLM.""" + prompt = f"""Analyze the sentiment of the following text as positive, negative, or neutral. + Just output a single word - 'positive', 'negative', or 'neutral'. + + Text to analyze: {text}""" + + # Request LLM analysis + response = await ctx.sample(prompt) + + # Process the LLM's response + sentiment = response.text.strip().lower() + + # Map to standard sentiment values + if "positive" in sentiment: + sentiment = "positive" + elif "negative" in sentiment: + sentiment = "negative" + else: + sentiment = "neutral" + + return {"text": text, "sentiment": sentiment} +``` + +## Method Signature + + + + Request text generation from the client's LLM + + + + A string or list of strings/message objects to send to the LLM + + + + Optional system prompt to guide the LLM's behavior + + + + Optional sampling temperature (controls randomness, typically 0.0-1.0) + + + + Optional maximum number of tokens to generate + + + + Optional model selection preferences (e.g., model hint string, list of hints, or ModelPreferences object) + + + + + + The LLM's response content (typically TextContent with a .text attribute) + + + + + +## Simple Text Generation + +### Basic Prompting + +Generate text with simple string prompts: + +```python {6} +@mcp.tool +async def generate_summary(content: str, ctx: Context) -> str: + """Generate a summary of the provided content.""" + prompt = f"Please provide a concise summary of the following content:\n\n{content}" + + response = await ctx.sample(prompt) + return response.text +``` + +### System Prompt + +Use system prompts to guide the LLM's behavior: + +```python {4-9} +@mcp.tool +async def generate_code_example(concept: str, ctx: Context) -> str: + """Generate a Python code example for a given concept.""" + response = await ctx.sample( + messages=f"Write a simple Python code example demonstrating '{concept}'.", + system_prompt="You are an expert Python programmer. Provide concise, working code examples without explanations.", + temperature=0.7, + max_tokens=300 + ) + + code_example = response.text + return f"```python\n{code_example}\n```" +``` + + +### Model Preferences + +Specify model preferences for different use cases: + +```python {4-8, 17-22} +@mcp.tool +async def creative_writing(topic: str, ctx: Context) -> str: + """Generate creative content using a specific model.""" + response = await ctx.sample( + messages=f"Write a creative short story about {topic}", + model_preferences="claude-3-sonnet", # Prefer a specific model + include_context="thisServer", # Use the server's context + temperature=0.9, # High creativity + max_tokens=1000 + ) + + return response.text + +@mcp.tool +async def technical_analysis(data: str, ctx: Context) -> str: + """Perform technical analysis with a reasoning-focused model.""" + response = await ctx.sample( + messages=f"Analyze this technical data and provide insights: {data}", + model_preferences=["claude-3-opus", "gpt-4"], # Prefer reasoning models + temperature=0.2, # Low randomness for consistency + max_tokens=800 + ) + + return response.text +``` + +### Complex Message Structures + +Use structured messages for more complex interactions: + +```python {1, 6-10} +from fastmcp.client.sampling import SamplingMessage + +@mcp.tool +async def multi_turn_analysis(user_query: str, context_data: str, ctx: Context) -> str: + """Perform analysis using multi-turn conversation structure.""" + messages = [ + SamplingMessage(role="user", content=f"I have this data: {context_data}"), + SamplingMessage(role="assistant", content="I can see your data. What would you like me to analyze?"), + SamplingMessage(role="user", content=user_query) + ] + + response = await ctx.sample( + messages=messages, + system_prompt="You are a data analyst. Provide detailed insights based on the conversation context.", + temperature=0.3 + ) + + return response.text +``` + +## Client Requirements + +LLM sampling requires client support: + +- Clients must implement sampling handlers to process requests +- If the client doesn't support sampling, calls to `ctx.sample()` will fail +- See [Client Sampling](/clients/sampling) for details on implementing client-side sampling handlers diff --git a/docs/servers/server.mdx b/docs/servers/server.mdx new file mode 100644 index 000000000..cb27d80ac --- /dev/null +++ b/docs/servers/server.mdx @@ -0,0 +1,413 @@ +--- +title: The FastMCP Server +sidebarTitle: Overview +description: The core FastMCP server class for building MCP applications with tools, resources, and prompts. +icon: server +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + +The central piece of a FastMCP application is the `FastMCP` server class. This class acts as the main container for your application's tools, resources, and prompts, and manages communication with MCP clients. + +## Creating a Server + +Instantiating a server is straightforward. You typically provide a name for your server, which helps identify it in client applications or logs. + +```python +from fastmcp import FastMCP + +# Create a basic server instance +mcp = FastMCP(name="MyAssistantServer") + +# You can also add instructions for how to interact with the server +mcp_with_instructions = FastMCP( + name="HelpfulAssistant", + instructions=""" + This server provides data analysis tools. + Call get_average() to analyze numerical data. + """, +) +``` + +The `FastMCP` constructor accepts several arguments: + + + + A human-readable name for your server + + + + Description of how to interact with this server. These instructions help clients understand the server's purpose and available functionality + + + + Authentication provider for securing HTTP-based transports. See [Authentication](/servers/auth/authentication) for configuration options + + + + An async context manager function for server startup and shutdown logic + + + + A list of tools (or functions to convert to tools) to add to the server. In some cases, providing tools programmatically may be more convenient than using the `@mcp.tool` decorator + + + + + Only expose components with at least one matching tag + + + + Hide components with any matching tag + + + + How to handle duplicate tool registrations + + + + How to handle duplicate resource registrations + + + + How to handle duplicate prompt registrations + + + + + + Whether to include FastMCP metadata in component responses. When `True`, component tags and other FastMCP-specific metadata are included in the `_fastmcp` namespace within each component's `meta` field. When `False`, this metadata is omitted, resulting in cleaner integration with external systems. Can be overridden globally via `FASTMCP_INCLUDE_FASTMCP_META` environment variable + + +## Components + +FastMCP servers expose several types of components to the client: + +### Tools + +Tools are functions that the client can call to perform actions or access external systems. + +```python +@mcp.tool +def multiply(a: float, b: float) -> float: + """Multiplies two numbers together.""" + return a * b +``` + +See [Tools](/servers/tools) for detailed documentation. + +### Resources + +Resources expose data sources that the client can read. + +```python +@mcp.resource("data://config") +def get_config() -> dict: + """Provides the application configuration.""" + return {"theme": "dark", "version": "1.0"} +``` + +See [Resources & Templates](/servers/resources) for detailed documentation. + +### Resource Templates + +Resource templates are parameterized resources that allow the client to request specific data. + +```python +@mcp.resource("users://{user_id}/profile") +def get_user_profile(user_id: int) -> dict: + """Retrieves a user's profile by ID.""" + # The {user_id} in the URI is extracted and passed to this function + return {"id": user_id, "name": f"User {user_id}", "status": "active"} +``` + +See [Resources & Templates](/servers/resources) for detailed documentation. + +### Prompts + +Prompts are reusable message templates for guiding the LLM. + +```python +@mcp.prompt +def analyze_data(data_points: list[float]) -> str: + """Creates a prompt asking for analysis of numerical data.""" + formatted_data = ", ".join(str(point) for point in data_points) + return f"Please analyze these data points: {formatted_data}" +``` + +See [Prompts](/servers/prompts) for detailed documentation. + +## Tag-Based Filtering + + + +FastMCP supports tag-based filtering to selectively expose components based on configurable include/exclude tag sets. This is useful for creating different views of your server for different environments or users. + +Components can be tagged when defined using the `tags` parameter: + +```python +@mcp.tool(tags={"public", "utility"}) +def public_tool() -> str: + return "This tool is public" + +@mcp.tool(tags={"internal", "admin"}) +def admin_tool() -> str: + return "This tool is for admins only" +``` + + +The filtering logic works as follows: +- **Include tags**: If specified, only components with at least one matching tag are exposed +- **Exclude tags**: Components with any matching tag are filtered out +- **Precedence**: Exclude tags always take priority over include tags + + +To ensure a component is never exposed, you can set `enabled=False` on the component itself. To learn more, see the component-specific documentation. + + +You configure tag-based filtering when creating your server: + +```python +# Only expose components tagged with "public" +mcp = FastMCP(include_tags={"public"}) + +# Hide components tagged as "internal" or "deprecated" +mcp = FastMCP(exclude_tags={"internal", "deprecated"}) + +# Combine both: show admin tools but hide deprecated ones +mcp = FastMCP(include_tags={"admin"}, exclude_tags={"deprecated"}) +``` + +This filtering applies to all component types (tools, resources, resource templates, and prompts) and affects both listing and access. + +## Running the Server + +FastMCP servers need a transport mechanism to communicate with clients. You typically start your server by calling the `mcp.run()` method on your `FastMCP` instance, often within an `if __name__ == "__main__":` block in your main server script. This pattern ensures compatibility with various MCP clients. + +```python +# my_server.py +from fastmcp import FastMCP + +mcp = FastMCP(name="MyServer") + +@mcp.tool +def greet(name: str) -> str: + """Greet a user by name.""" + return f"Hello, {name}!" + +if __name__ == "__main__": + # This runs the server, defaulting to STDIO transport + mcp.run() + + # To use a different transport, e.g., HTTP: + # mcp.run(transport="http", host="127.0.0.1", port=9000) +``` + +FastMCP supports several transport options: +- STDIO (default, for local tools) +- HTTP (recommended for web services, uses Streamable HTTP protocol) +- SSE (legacy web transport, deprecated) + +The server can also be run using the FastMCP CLI. + +For detailed information on each transport, how to configure them (host, port, paths), and when to use which, please refer to the [**Running Your FastMCP Server**](/deployment/running-server) guide. + +## Custom Routes + +When running your server with HTTP transport, you can add custom web routes alongside your MCP endpoint using the `@custom_route` decorator. This is useful for simple endpoints like health checks that need to be served alongside your MCP server: + +```python +from fastmcp import FastMCP +from starlette.requests import Request +from starlette.responses import PlainTextResponse + +mcp = FastMCP("MyServer") + +@mcp.custom_route("/health", methods=["GET"]) +async def health_check(request: Request) -> PlainTextResponse: + return PlainTextResponse("OK") + +if __name__ == "__main__": + mcp.run(transport="http") # Health check at http://localhost:8000/health +``` + +Custom routes are served alongside your MCP endpoint and are useful for: +- Health check endpoints for monitoring +- Simple status or info endpoints +- Basic webhooks or callbacks + +For more complex web applications, consider [mounting your MCP server into a FastAPI or Starlette app](/deployment/self-hosted#integration-with-web-frameworks). + +## Composing Servers + + + +FastMCP supports composing multiple servers together using `import_server` (static copy) and `mount` (live link). This allows you to organize large applications into modular components or reuse existing servers. + +See the [Server Composition](/servers/composition) guide for full details, best practices, and examples. + +```python +# Example: Importing a subserver +from fastmcp import FastMCP +import asyncio + +main = FastMCP(name="Main") +sub = FastMCP(name="Sub") + +@sub.tool +def hello(): + return "hi" + +# Mount directly +main.mount(sub, prefix="sub") +``` + +## Proxying Servers + + + +FastMCP can act as a proxy for any MCP server (local or remote) using `FastMCP.as_proxy`, letting you bridge transports or add a frontend to existing servers. For example, you can expose a remote SSE server locally via stdio, or vice versa. + +Proxies automatically handle concurrent operations safely by creating fresh sessions for each request when using disconnected clients. + +See the [Proxying Servers](/servers/proxy) guide for details and advanced usage. + +```python +from fastmcp import FastMCP, Client + +backend = Client("http://example.com/mcp/sse") +proxy = FastMCP.as_proxy(backend, name="ProxyServer") +# Now use the proxy like any FastMCP server +``` + +## OpenAPI Integration + + + +FastMCP can automatically generate servers from OpenAPI specifications or existing FastAPI applications using `FastMCP.from_openapi()` and `FastMCP.from_fastapi()`. This allows you to instantly convert existing APIs into MCP servers without manual tool creation. + +See the [FastAPI Integration](/integrations/fastapi) and [OpenAPI Integration](/integrations/openapi) guides for detailed examples and configuration options. + +```python +import httpx +from fastmcp import FastMCP + +# From OpenAPI spec +spec = httpx.get("https://api.example.com/openapi.json").json() +mcp = FastMCP.from_openapi(openapi_spec=spec, client=httpx.AsyncClient()) + +# From FastAPI app +from fastapi import FastAPI +app = FastAPI() +mcp = FastMCP.from_fastapi(app=app) +``` + +## Server Configuration + +Servers can be configured using a combination of initialization arguments, global settings, and transport-specific settings. + +### Server-Specific Configuration + +Server-specific settings are passed when creating the `FastMCP` instance and control server behavior: + +```python +from fastmcp import FastMCP + +# Configure server-specific settings +mcp = FastMCP( + name="ConfiguredServer", + include_tags={"public", "api"}, # Only expose these tagged components + exclude_tags={"internal", "deprecated"}, # Hide these tagged components + on_duplicate_tools="error", # Handle duplicate registrations + on_duplicate_resources="warn", + on_duplicate_prompts="replace", + include_fastmcp_meta=False, # Disable FastMCP metadata for cleaner integration +) +``` + +### Global Settings + +Global settings affect all FastMCP servers and can be configured via environment variables (prefixed with `FASTMCP_`) or in a `.env` file: + +```python +import fastmcp + +# Access global settings +print(fastmcp.settings.log_level) # Default: "INFO" +print(fastmcp.settings.mask_error_details) # Default: False +print(fastmcp.settings.resource_prefix_format) # Default: "path" +print(fastmcp.settings.include_fastmcp_meta) # Default: True +``` + +Common global settings include: +- **`log_level`**: Logging level ("DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"), set with `FASTMCP_LOG_LEVEL` +- **`mask_error_details`**: Whether to hide detailed error information from clients, set with `FASTMCP_MASK_ERROR_DETAILS` +- **`resource_prefix_format`**: How to format resource prefixes ("path" or "protocol"), set with `FASTMCP_RESOURCE_PREFIX_FORMAT` +- **`include_fastmcp_meta`**: Whether to include FastMCP metadata in component responses (default: True), set with `FASTMCP_INCLUDE_FASTMCP_META` + +### Transport-Specific Configuration + +Transport settings are provided when running the server and control network behavior: + +```python +# Configure transport when running +mcp.run( + transport="http", + host="0.0.0.0", # Bind to all interfaces + port=9000, # Custom port + log_level="DEBUG", # Override global log level +) + +# Or for async usage +await mcp.run_async( + transport="http", + host="127.0.0.1", + port=8080, +) +``` + +### Setting Global Configuration + +Global FastMCP settings can be configured via environment variables (prefixed with `FASTMCP_`): + +```bash +# Configure global FastMCP behavior +export FASTMCP_LOG_LEVEL=DEBUG +export FASTMCP_MASK_ERROR_DETAILS=True +export FASTMCP_RESOURCE_PREFIX_FORMAT=protocol +export FASTMCP_INCLUDE_FASTMCP_META=False +``` + +### Custom Tool Serialization + + + +By default, FastMCP serializes tool return values to JSON when they need to be converted to text. You can customize this behavior by providing a `tool_serializer` function when creating your server: + +```python +import yaml +from fastmcp import FastMCP + +# Define a custom serializer that formats dictionaries as YAML +def yaml_serializer(data): + return yaml.dump(data, sort_keys=False) + +# Create a server with the custom serializer +mcp = FastMCP(name="MyServer", tool_serializer=yaml_serializer) + +@mcp.tool +def get_config(): + """Returns configuration in YAML format.""" + return {"api_key": "abc123", "debug": True, "rate_limit": 100} +``` + +The serializer function takes any data object and returns a string representation. This is applied to **all non-string return values** from your tools. Tools that already return strings bypass the serializer. + +This customization is useful when you want to: +- Format data in a specific way (like YAML or custom formats) +- Control specific serialization options (like indentation or sorting) +- Add metadata or transform data before sending it to clients + + +If the serializer function raises an exception, the tool will fall back to the default JSON serialization to avoid breaking the server. + diff --git a/docs/servers/tools.mdx b/docs/servers/tools.mdx new file mode 100644 index 000000000..8da84c202 --- /dev/null +++ b/docs/servers/tools.mdx @@ -0,0 +1,1095 @@ +--- +title: Tools +sidebarTitle: Tools +description: Expose functions as executable capabilities for your MCP client. +icon: wrench +--- + +import { VersionBadge } from '/snippets/version-badge.mdx' + +Tools are the core building blocks that allow your LLM to interact with external systems, execute code, and access data that isn't in its training data. In FastMCP, tools are Python functions exposed to LLMs through the MCP protocol. + +## What Are Tools? + +Tools in FastMCP transform regular Python functions into capabilities that LLMs can invoke during conversations. When an LLM decides to use a tool: + +1. It sends a request with parameters based on the tool's schema. +2. FastMCP validates these parameters against your function's signature. +3. Your function executes with the validated inputs. +4. The result is returned to the LLM, which can use it in its response. + +This allows LLMs to perform tasks like querying databases, calling APIs, making calculations, or accessing filesโ€”extending their capabilities beyond what's in their training data. + +## Tools + +### The `@tool` Decorator + +Creating a tool is as simple as decorating a Python function with `@mcp.tool`: + +```python +from fastmcp import FastMCP + +mcp = FastMCP(name="CalculatorServer") + +@mcp.tool +def add(a: int, b: int) -> int: + """Adds two integer numbers together.""" + return a + b +``` + +When this tool is registered, FastMCP automatically: +- Uses the function name (`add`) as the tool name. +- Uses the function's docstring (`Adds two integer numbers...`) as the tool description. +- Generates an input schema based on the function's parameters and type annotations. +- Handles parameter validation and error reporting. + +The way you define your Python function dictates how the tool appears and behaves for the LLM client. + + +Functions with `*args` or `**kwargs` are not supported as tools. This restriction exists because FastMCP needs to generate a complete parameter schema for the MCP protocol, which isn't possible with variable argument lists. + + +#### Decorator Arguments + +While FastMCP infers the name and description from your function, you can override these and add additional metadata using arguments to the `@mcp.tool` decorator: + +```python +@mcp.tool( + name="find_products", # Custom tool name for the LLM + description="Search the product catalog with optional category filtering.", # Custom description + tags={"catalog", "search"}, # Optional tags for organization/filtering + meta={"version": "1.2", "author": "product-team"} # Custom metadata +) +def search_products_implementation(query: str, category: str | None = None) -> list[dict]: + """Internal function description (ignored if description is provided above).""" + # Implementation... + print(f"Searching for '{query}' in category '{category}'") + return [{"id": 2, "name": "Another Product"}] +``` + + + + Sets the explicit tool name exposed via MCP. If not provided, uses the function name + + + + Provides the description exposed via MCP. If set, the function's docstring is ignored for this purpose + + + + A set of strings used to categorize the tool. These can be used by the server and, in some cases, by clients to filter or group available tools. + + + + A boolean to enable or disable the tool. See [Disabling Tools](#disabling-tools) for more information + + + + A list of argument names to exclude from the tool schema shown to the LLM. See [Excluding Arguments](#excluding-arguments) for more information + + + + An optional `ToolAnnotations` object or dictionary to add additional metadata about the tool. + + + A human-readable title for the tool. + + + If true, the tool does not modify its environment. + + + If true, the tool may perform destructive updates to its environment. + + + If true, calling the tool repeatedly with the same arguments will have no additional effect on the its environment. + + + If true, this tool may interact with an "open world" of external entities. If false, the tool's domain of interaction is closed. + + + + + + + + Optional meta information about the tool. This data is passed through to the MCP client as the `_meta` field of the client-side tool object and can be used for custom metadata, versioning, or other application-specific purposes. + + + + +### Async and Synchronous Tools + +FastMCP is an async-first framework that seamlessly supports both asynchronous (`async def`) and synchronous (`def`) functions as tools. Async tools are preferred for I/O-bound operations to keep your server responsive. + +While synchronous tools work seamlessly in FastMCP, they can block the event loop during execution. For CPU-intensive or potentially blocking synchronous operations, consider alternative strategies. One approach is to use `anyio` (which FastMCP already uses internally) to wrap them as async functions, for example: + +```python {1, 13} +import anyio +from fastmcp import FastMCP + +mcp = FastMCP() + +def cpu_intensive_task(data: str) -> str: + # Some heavy computation that could block the event loop + return processed_data + +@mcp.tool +async def wrapped_cpu_task(data: str) -> str: + """CPU-intensive task wrapped to prevent blocking.""" + return await anyio.to_thread.run_sync(cpu_intensive_task, data) +``` + +Alternative approaches include using `asyncio.get_event_loop().run_in_executor()` or other threading techniques to manage blocking operations without impacting server responsiveness. For example, here's a recipe for using the `asyncer` library (not included in FastMCP) to create a decorator that wraps synchronous functions, courtesy of [@hsheth2](https://github.com/jlowin/fastmcp/issues/864#issuecomment-3103678258): + + +```python Decorator Recipe +import asyncer +import functools +from typing import Callable, ParamSpec, TypeVar, Awaitable + +_P = ParamSpec("_P") +_R = TypeVar("_R") + +def make_async_background(fn: Callable[_P, _R]) -> Callable[_P, Awaitable[_R]]: + @functools.wraps(fn) + async def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _R: + return await asyncer.asyncify(fn)(*args, **kwargs) + + return wrapper +``` + +```python Using the Decorator {6} +from fastmcp import FastMCP + +mcp = FastMCP() + +@mcp.tool() +@make_async_background +def my_tool() -> None: + time.sleep(5) +``` + + + +### Type Annotations + +Type annotations for parameters are essential for proper tool functionality. They: +1. Inform the LLM about the expected data types for each parameter +2. Enable FastMCP to validate input data from clients +3. Generate accurate JSON schemas for the MCP protocol + +Use standard Python type annotations for parameters: + +```python +@mcp.tool +def analyze_text( + text: str, + max_tokens: int = 100, + language: str | None = None +) -> dict: + """Analyze the provided text.""" + # Implementation... +``` + +FastMCP supports a wide range of type annotations, including all Pydantic types: + +| Type Annotation | Example | Description | +| :---------------------- | :---------------------------- | :---------------------------------- | +| Basic types | `int`, `float`, `str`, `bool` | Simple scalar values - see [Built-in Types](#built-in-types) | +| Binary data | `bytes` | Binary content - see [Binary Data](#binary-data) | +| Date and Time | `datetime`, `date`, `timedelta` | Date and time objects - see [Date and Time Types](#date-and-time-types) | +| Collection types | `list[str]`, `dict[str, int]`, `set[int]` | Collections of items - see [Collection Types](#collection-types) | +| Optional types | `float \| None`, `Optional[float]`| Parameters that may be null/omitted - see [Union and Optional Types](#union-and-optional-types) | +| Union types | `str \| int`, `Union[str, int]`| Parameters accepting multiple types - see [Union and Optional Types](#union-and-optional-types) | +| Constrained types | `Literal["A", "B"]`, `Enum` | Parameters with specific allowed values - see [Constrained Types](#constrained-types) | +| Paths | `Path` | File system paths - see [Paths](#paths) | +| UUIDs | `UUID` | Universally unique identifiers - see [UUIDs](#uuids) | +| Pydantic models | `UserData` | Complex structured data - see [Pydantic Models](#pydantic-models) | + +For additional type annotations not listed here, see the [Parameter Types](#parameter-types) section below for more detailed information and examples. +### Parameter Metadata + +You can provide additional metadata about parameters in several ways: + +#### Simple String Descriptions + + + +For basic parameter descriptions, you can use a convenient shorthand with `Annotated`: + +```python +from typing import Annotated + +@mcp.tool +def process_image( + image_url: Annotated[str, "URL of the image to process"], + resize: Annotated[bool, "Whether to resize the image"] = False, + width: Annotated[int, "Target width in pixels"] = 800, + format: Annotated[str, "Output image format"] = "jpeg" +) -> dict: + """Process an image with optional resizing.""" + # Implementation... +``` + +This shorthand syntax is equivalent to using `Field(description=...)` but more concise for simple descriptions. + + +This shorthand syntax is only applied to `Annotated` types with a single string description. + + +#### Advanced Metadata with Field + +For validation constraints and advanced metadata, use Pydantic's `Field` class with `Annotated`: + +```python +from typing import Annotated +from pydantic import Field + +@mcp.tool +def 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, + width: Annotated[int, Field(description="Target width in pixels", ge=1, le=2000)] = 800, + format: Annotated[ + Literal["jpeg", "png", "webp"], + Field(description="Output image format") + ] = "jpeg" +) -> dict: + """Process an image with optional resizing.""" + # Implementation... +``` + + +You can also use the Field as a default value, though the Annotated approach is preferred: + +```python +@mcp.tool +def search_database( + query: str = Field(description="Search query string"), + limit: int = Field(10, description="Maximum number of results", ge=1, le=100) +) -> list: + """Search the database with the provided query.""" + # Implementation... +``` + +Field provides several validation and documentation features: +- `description`: Human-readable explanation of the parameter (shown to LLMs) +- `ge`/`gt`/`le`/`lt`: Greater/less than (or equal) constraints +- `min_length`/`max_length`: String or collection length constraints +- `pattern`: Regex pattern for string validation +- `default`: Default value if parameter is omitted + + + +### Optional Arguments + +FastMCP follows Python's standard function parameter conventions. Parameters without default values are required, while those with default values are optional. + +```python +@mcp.tool +def search_products( + query: str, # Required - no default value + max_results: int = 10, # Optional - has default value + sort_by: str = "relevance", # Optional - has default value + category: str | None = None # Optional - can be None +) -> list[dict]: + """Search the product catalog.""" + # Implementation... +``` + +In this example, the LLM must provide a `query` parameter, while `max_results`, `sort_by`, and `category` will use their default values if not explicitly provided. + + +### Excluding Arguments + + + +You can exclude certain arguments from the tool schema shown to the LLM. This is useful for arguments that are injected at runtime (such as `state`, `user_id`, or credentials) and should not be exposed to the LLM or client. Only arguments with default values can be excluded; attempting to exclude a required argument will raise an error. + +Example: + +```python +@mcp.tool( + name="get_user_details", + exclude_args=["user_id"] +) +def get_user_details(user_id: str = None) -> str: + # user_id will be injected by the server, not provided by the LLM + ... +``` + +With this configuration, `user_id` will not appear in the tool's parameter schema, but can still be set by the server or framework at runtime. + +For more complex tool transformations, see [Transforming Tools](/patterns/tool-transformation). + +### Disabling Tools + + + +You can control the visibility and availability of tools by enabling or disabling them. This is useful for feature flagging, maintenance, or dynamically changing the toolset available to a client. Disabled tools will not appear in the list of available tools returned by `list_tools`, and attempting to call a disabled tool will result in an "Unknown tool" error, just as if the tool did not exist. + +By default, all tools are enabled. You can disable a tool upon creation using the `enabled` parameter in the decorator: + +```python +@mcp.tool(enabled=False) +def maintenance_tool(): + """This tool is currently under maintenance.""" + return "This tool is disabled." +``` + +You can also toggle a tool's state programmatically after it has been created: + +```python +@mcp.tool +def dynamic_tool(): + return "I am a dynamic tool." + +# Disable and re-enable the tool +dynamic_tool.disable() +dynamic_tool.enable() +``` + +### Return Values + + +FastMCP tools can return data in two complementary formats: **traditional content blocks** (like text and images) and **structured outputs** (machine-readable JSON). When you add return type annotations, FastMCP automatically generates **output schemas** to validate the structured data and enables clients to deserialize results back to Python objects. + +Understanding how these three concepts work together: + +- **Return Values**: What your Python function returns (determines both content blocks and structured data) +- **Structured Outputs**: JSON data sent alongside traditional content for machine processing +- **Output Schemas**: JSON Schema declarations that describe and validate the structured output format + +The following sections explain each concept in detail. + +#### Content Blocks + +FastMCP automatically converts tool return values into appropriate MCP content blocks: + +- **`str`**: Sent as `TextContent` +- **`bytes`**: Base64 encoded and sent as `BlobResourceContents` (within an `EmbeddedResource`) +- **`fastmcp.utilities.types.Image`**: Sent as `ImageContent` +- **`fastmcp.utilities.types.Audio`**: Sent as `AudioContent` +- **`fastmcp.utilities.types.File`**: Sent as base64-encoded `EmbeddedResource` +- **A list of any of the above**: Converts each item appropriately +- **`None`**: Results in an empty response + +#### Structured Output + + + +The 6/18/2025 MCP spec update [introduced](https://modelcontextprotocol.io/specification/2025-06-18/server/tools#structured-content) structured content, which is a new way to return data from tools. Structured content is a JSON object that is sent alongside traditional content. FastMCP automatically creates structured outputs alongside traditional content when your tool returns data that has a JSON object representation. This provides machine-readable JSON data that clients can deserialize back to Python objects. + +**Automatic Structured Content Rules:** +- **Object-like results** (`dict`, Pydantic models, dataclasses) โ†’ Always become structured content (even without output schema) +- **Non-object results** (`int`, `str`, `list`) โ†’ Only become structured content if there's an output schema to validate/serialize them +- **All results** โ†’ Always become traditional content blocks for backward compatibility + + +This automatic behavior enables clients to receive machine-readable data alongside human-readable content without requiring explicit output schemas for object-like returns. + + +##### Object-like Results (Automatic Structured Content) + + +```python Dict Return (No Schema Needed) +@mcp.tool +def get_user_data(user_id: str) -> dict: + """Get user data without type annotation.""" + return {"name": "Alice", "age": 30, "active": True} +``` + +```json Traditional Content +"{\n \"name\": \"Alice\",\n \"age\": 30,\n \"active\": true\n}" +``` + +```json Structured Content (Automatic) +{ + "name": "Alice", + "age": 30, + "active": true +} +``` + + +##### Non-object Results (Schema Required) + + +```python Integer Return (No Schema) +@mcp.tool +def calculate_sum(a: int, b: int): + """Calculate sum without return annotation.""" + return a + b # Returns 8 +``` + +```json Traditional Content Only +"8" +``` + +```python Integer Return (With Schema) +@mcp.tool +def calculate_sum(a: int, b: int) -> int: + """Calculate sum with return annotation.""" + return a + b # Returns 8 +``` + +```json Traditional Content +"8" +``` + +```json Structured Content (From Schema) +{ + "result": 8 +} +``` + + +##### Complex Type Example + + +```python Tool Definition +from dataclasses import dataclass +from fastmcp import FastMCP + +mcp = FastMCP() + +@dataclass +class Person: + name: str + age: int + email: str + +@mcp.tool +def get_user_profile(user_id: str) -> Person: + """Get a user's profile information.""" + return Person(name="Alice", age=30, email="alice@example.com") +``` + +```json Generated Output Schema +{ + "properties": { + "name": {"title": "Name", "type": "string"}, + "age": {"title": "Age", "type": "integer"}, + "email": {"title": "Email", "type": "string"} + }, + "required": ["name", "age", "email"], + "title": "Person", + "type": "object" +} +``` + +```json Structured Output +{ + "name": "Alice", + "age": 30, + "email": "alice@example.com" +} +``` + + +#### Output Schemas + + + +The 6/18/2025 MCP spec update [introduced](https://modelcontextprotocol.io/specification/2025-06-18/server/tools#output-schema) output schemas, which are a new way to describe the expected output format of a tool. When an output schema is provided, the tool *must* return structured output that matches the schema. + +When you add return type annotations to your functions, FastMCP automatically generates JSON schemas that describe the expected output format. These schemas help MCP clients understand and validate the structured data they receive. + +##### Primitive Type Wrapping + +For primitive return types (like `int`, `str`, `bool`), FastMCP automatically wraps the result under a `"result"` key to create valid structured output: + + +```python Primitive Return Type +@mcp.tool +def calculate_sum(a: int, b: int) -> int: + """Add two numbers together.""" + return a + b +``` + +```json Generated Schema (Wrapped) +{ + "type": "object", + "properties": { + "result": {"type": "integer"} + }, + "x-fastmcp-wrap-result": true +} +``` + +```json Structured Output +{ + "result": 8 +} +``` + + +##### Manual Schema Control + +You can override the automatically generated schema by providing a custom `output_schema`: + +```python +@mcp.tool(output_schema={ + "type": "object", + "properties": { + "data": {"type": "string"}, + "metadata": {"type": "object"} + } +}) +def custom_schema_tool() -> dict: + """Tool with custom output schema.""" + return {"data": "Hello", "metadata": {"version": "1.0"}} +``` + +Schema generation works for most common types including basic types, collections, union types, Pydantic models, TypedDict structures, and dataclasses. + + +**Important Constraints**: +- Output schemas must be object types (`"type": "object"`) +- If you provide an output schema, your tool **must** return structured output that matches it +- However, you can provide structured output without an output schema (using `ToolResult`) + + +#### Full Control with ToolResult + +For complete control over both traditional content and structured output, return a `ToolResult` object: + +```python +from fastmcp.tools.tool import ToolResult + +@mcp.tool +def advanced_tool() -> ToolResult: + """Tool with full control over output.""" + return ToolResult( + content=[TextContent(text="Human-readable summary")], + structured_content={"data": "value", "count": 42} + ) +``` + +When returning `ToolResult`: +- You control exactly what content and structured data is sent +- Output schemas are optional - structured content can be provided without a schema +- Clients receive both traditional content blocks and structured data + + +If your return type annotation cannot be converted to a JSON schema (e.g., complex custom classes without Pydantic support), the output schema will be omitted but the tool will still function normally with traditional content. + + +### Error Handling + + + +If your tool encounters an error, you can raise a standard Python exception (`ValueError`, `TypeError`, `FileNotFoundError`, custom exceptions, etc.) or a FastMCP `ToolError`. + +By default, all exceptions (including their details) are logged and converted into an MCP error response to be sent back to the client LLM. This helps the LLM understand failures and react appropriately. + +If you want to mask internal error details for security reasons, you can: + +1. Use the `mask_error_details=True` parameter when creating your `FastMCP` instance: +```python +mcp = FastMCP(name="SecureServer", mask_error_details=True) +``` + +2. Or use `ToolError` to explicitly control what error information is sent to clients: +```python +from fastmcp import FastMCP +from fastmcp.exceptions import ToolError + +@mcp.tool +def divide(a: float, b: float) -> float: + """Divide a by b.""" + + if b == 0: + # Error messages from ToolError are always sent to clients, + # regardless of mask_error_details setting + raise ToolError("Division by zero is not allowed.") + + # If mask_error_details=True, this message would be masked + if not isinstance(a, (int, float)) or not isinstance(b, (int, float)): + raise TypeError("Both arguments must be numbers.") + + return a / b +``` + +When `mask_error_details=True`, only error messages from `ToolError` will include details, other exceptions will be converted to a generic message. + +### Annotations + + + +FastMCP allows you to add specialized metadata to your tools through annotations. These annotations communicate how tools behave to client applications without consuming token context in LLM prompts. + +Annotations serve several purposes in client applications: +- Adding user-friendly titles for display purposes +- Indicating whether tools modify data or systems +- Describing the safety profile of tools (destructive vs. non-destructive) +- Signaling if tools interact with external systems + +You can add annotations to a tool using the `annotations` parameter in the `@mcp.tool` decorator: + +```python +@mcp.tool( + annotations={ + "title": "Calculate Sum", + "readOnlyHint": True, + "openWorldHint": False + } +) +def calculate_sum(a: float, b: float) -> float: + """Add two numbers together.""" + return a + b +``` + +FastMCP supports these standard annotations: + +| Annotation | Type | Default | Purpose | +| :--------- | :--- | :------ | :------ | +| `title` | string | - | Display name for user interfaces | +| `readOnlyHint` | boolean | false | Indicates if the tool only reads without making changes | +| `destructiveHint` | boolean | true | For non-readonly tools, signals if changes are destructive | +| `idempotentHint` | boolean | false | Indicates if repeated identical calls have the same effect as a single call | +| `openWorldHint` | boolean | true | Specifies if the tool interacts with external systems | + +Remember that annotations help make better user experiences but should be treated as advisory hints. They help client applications present appropriate UI elements and safety controls, but won't enforce security boundaries on their own. Always focus on making your annotations accurately represent what your tool actually does. + +### Notifications + + + +FastMCP automatically sends `notifications/tools/list_changed` notifications to connected clients when tools are added, removed, enabled, or disabled. This allows clients to stay up-to-date with the current tool set without manually polling for changes. + +```python +@mcp.tool +def example_tool() -> str: + return "Hello!" + +# These operations trigger notifications: +mcp.add_tool(example_tool) # Sends tools/list_changed notification +example_tool.disable() # Sends tools/list_changed notification +example_tool.enable() # Sends tools/list_changed notification +mcp.remove_tool("example_tool") # Sends tools/list_changed notification +``` + +Notifications are only sent when these operations occur within an active MCP request context (e.g., when called from within a tool or other MCP operation). Operations performed during server initialization do not trigger notifications. + +Clients can handle these notifications using a [message handler](/clients/messages) to automatically refresh their tool lists or update their interfaces. + +## MCP Context + +Tools can access MCP features like logging, reading resources, or reporting progress through the `Context` object. To use it, add a parameter to your tool function with the type hint `Context`. + +```python +from fastmcp import FastMCP, Context + +mcp = FastMCP(name="ContextDemo") + +@mcp.tool +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}") + + # Read a resource + resource = await ctx.read_resource(data_uri) + data = resource[0].content if resource else "" + + # Report progress + await ctx.report_progress(progress=50, total=100) + + # Example request to the client's LLM for help + summary = await ctx.sample(f"Summarize this in 10 words: {data[:200]}") + + await ctx.report_progress(progress=100, total=100) + return { + "length": len(data), + "summary": summary.text + } +``` + +The Context object provides access to: + +- **Logging**: `ctx.debug()`, `ctx.info()`, `ctx.warning()`, `ctx.error()` +- **Progress Reporting**: `ctx.report_progress(progress, total)` +- **Resource Access**: `ctx.read_resource(uri)` +- **LLM Sampling**: `ctx.sample(...)` +- **Request Information**: `ctx.request_id`, `ctx.client_id` + +For full documentation on the Context object and all its capabilities, see the [Context documentation](/servers/context). + +## Parameter Types + +FastMCP supports a wide variety of parameter types to give you flexibility when designing your tools. + +FastMCP generally supports all types that Pydantic supports as fields, including all Pydantic custom types. This means you can use any type that can be validated and parsed by Pydantic in your tool parameters. + +FastMCP supports **type coercion** when possible. This means that if a client sends data that doesn't match the expected type, FastMCP will attempt to convert it to the appropriate type. For example, if a client sends a string for a parameter annotated as `int`, FastMCP will attempt to convert it to an integer. If the conversion is not possible, FastMCP will return a validation error. + +### Built-in Types + +The most common parameter types are Python's built-in scalar types: + +```python +@mcp.tool +def process_values( + name: str, # Text data + count: int, # Integer numbers + amount: float, # Floating point numbers + enabled: bool # Boolean values (True/False) +): + """Process various value types.""" + # Implementation... +``` + +These types provide clear expectations to the LLM about what values are acceptable and allow FastMCP to validate inputs properly. Even if a client provides a string like "42", it will be coerced to an integer for parameters annotated as `int`. + +### Date and Time Types + +FastMCP supports various date and time types from the `datetime` module: + +```python +from datetime import datetime, date, timedelta + +@mcp.tool +def process_date_time( + event_date: date, # ISO format date string or date object + event_time: datetime, # ISO format datetime string or datetime object + duration: timedelta = timedelta(hours=1) # Integer seconds or timedelta +) -> str: + """Process date and time information.""" + # Types are automatically converted from strings + assert isinstance(event_date, date) + assert isinstance(event_time, datetime) + assert isinstance(duration, timedelta) + + return f"Event on {event_date} at {event_time} for {duration}" +``` + +- `datetime` - Accepts ISO format strings (e.g., "2023-04-15T14:30:00") +- `date` - Accepts ISO format date strings (e.g., "2023-04-15") +- `timedelta` - Accepts integer seconds or timedelta objects + +### Collection Types + +FastMCP supports all standard Python collection types: + +```python +@mcp.tool +def analyze_data( + values: list[float], # List of numbers + properties: dict[str, str], # Dictionary with string keys and values + unique_ids: set[int], # Set of unique integers + coordinates: tuple[float, float], # Tuple with fixed structure + mixed_data: dict[str, list[int]] # Nested collections +): + """Analyze collections of data.""" + # Implementation... +``` + +All collection types can be used as parameter annotations: +- `list[T]` - Ordered sequence of items +- `dict[K, V]` - Key-value mapping +- `set[T]` - Unordered collection of unique items +- `tuple[T1, T2, ...]` - Fixed-length sequence with potentially different types + +Collection types can be nested and combined to represent complex data structures. JSON strings that match the expected structure will be automatically parsed and converted to the appropriate Python collection type. + +### Union and Optional Types + +For parameters that can accept multiple types or may be omitted: + +```python +@mcp.tool +def flexible_search( + query: str | int, # Can be either string or integer + filters: dict[str, str] | None = None, # Optional dictionary + sort_field: str | None = None # Optional string +): + """Search with flexible parameter types.""" + # Implementation... +``` + +Modern Python syntax (`str | int`) is preferred over older `Union[str, int]` forms. Similarly, `str | None` is preferred over `Optional[str]`. + +### Constrained Types + +When a parameter must be one of a predefined set of values, you can use either Literal types or Enums: + +#### Literals + +Literals constrain parameters to a specific set of values: + +```python +from typing import Literal + +@mcp.tool +def sort_data( + data: list[float], + order: Literal["ascending", "descending"] = "ascending", + algorithm: Literal["quicksort", "mergesort", "heapsort"] = "quicksort" +): + """Sort data using specific options.""" + # Implementation... +``` + +Literal types: +- Specify exact allowable values directly in the type annotation +- Help LLMs understand exactly which values are acceptable +- Provide input validation (errors for invalid values) +- Create clear schemas for clients + +#### Enums + +For more structured sets of constrained values, use Python's Enum class: + +```python +from enum import Enum + +class Color(Enum): + RED = "red" + GREEN = "green" + BLUE = "blue" + +@mcp.tool +def process_image( + image_path: str, + color_filter: Color = Color.RED +): + """Process an image with a color filter.""" + # Implementation... + # color_filter will be a Color enum member +``` + +When using Enum types: +- Clients should provide the enum's value (e.g., "red"), not the enum member name (e.g., "RED") +- FastMCP automatically coerces the string value into the appropriate Enum object +- Your function receives the actual Enum member (e.g., `Color.RED`) +- Validation errors are raised for values not in the enum + +### Binary Data + +There are two approaches to handling binary data in tool parameters: + +#### Bytes + +```python +@mcp.tool +def process_binary(data: bytes): + """Process binary data directly. + + The client can send a binary string, which will be + converted directly to bytes. + """ + # Implementation using binary data + data_length = len(data) + # ... +``` + +When you annotate a parameter as `bytes`, FastMCP will: +- Convert raw strings directly to bytes +- Validate that the input can be properly represented as bytes + +FastMCP does not automatically decode base64-encoded strings for bytes parameters. If you need to accept base64-encoded data, you should handle the decoding manually as shown below. + +#### Base64-encoded strings + +```python +from typing import Annotated +from pydantic import Field + +@mcp.tool +def process_image_data( + image_data: Annotated[str, Field(description="Base64-encoded image data")] +): + """Process an image from base64-encoded string. + + The client is expected to provide base64-encoded data as a string. + You'll need to decode it manually. + """ + # Manual base64 decoding + import base64 + binary_data = base64.b64decode(image_data) + # Process binary_data... +``` + +This approach is recommended when you expect to receive base64-encoded binary data from clients. + +### Paths + +The `Path` type from the `pathlib` module can be used for file system paths: + +```python +from pathlib import Path + +@mcp.tool +def process_file(path: Path) -> str: + """Process a file at the given path.""" + assert isinstance(path, Path) # Path is properly converted + return f"Processing file at {path}" +``` + +When a client sends a string path, FastMCP automatically converts it to a `Path` object. + +### UUIDs + +The `UUID` type from the `uuid` module can be used for unique identifiers: + +```python +import uuid + +@mcp.tool +def process_item( + item_id: uuid.UUID # String UUID or UUID object +) -> str: + """Process an item with the given UUID.""" + assert isinstance(item_id, uuid.UUID) # Properly converted to UUID + return f"Processing item {item_id}" +``` + +When a client sends a string UUID (e.g., "123e4567-e89b-12d3-a456-426614174000"), FastMCP automatically converts it to a `UUID` object. + +### Pydantic Models + +For complex, structured data with nested fields and validation, use Pydantic models: + +```python +from pydantic import BaseModel, Field +from typing import Optional + +class User(BaseModel): + username: str + email: str = Field(description="User's email address") + age: int | None = None + is_active: bool = True + +@mcp.tool +def create_user(user: User): + """Create a new user in the system.""" + # The input is automatically validated against the User model + # Even if provided as a JSON string or dict + # Implementation... +``` + +Using Pydantic models provides: +- Clear, self-documenting structure for complex inputs +- Built-in data validation +- Automatic generation of detailed JSON schemas for the LLM +- Automatic conversion from dict/JSON input + +Clients can provide data for Pydantic model parameters as either: +- A JSON object (string) +- A dictionary with the appropriate structure +- Nested parameters in the appropriate format + +### Pydantic Fields + +FastMCP supports robust parameter validation through Pydantic's `Field` class. This is especially useful to ensure that input values meet specific requirements beyond just their type. + +Note that fields can be used *outside* Pydantic models to provide metadata and validation constraints. The preferred approach is using `Annotated` with `Field`: + +```python +from typing import Annotated +from pydantic import Field + +@mcp.tool +def analyze_metrics( + # Numbers with range constraints + count: Annotated[int, Field(ge=0, le=100)], # 0 <= count <= 100 + ratio: Annotated[float, Field(gt=0, lt=1.0)], # 0 < ratio < 1.0 + + # String with pattern and length constraints + user_id: Annotated[str, Field( + pattern=r"^[A-Z]{2}\d{4}$", # Must match regex pattern + description="User ID in format XX0000" + )], + + # String with length constraints + comment: Annotated[str, Field(min_length=3, max_length=500)] = "", + + # Numeric constraints + factor: Annotated[int, Field(multiple_of=5)] = 10, # Must be multiple of 5 +): + """Analyze metrics with validated parameters.""" + # Implementation... +``` + +You can also use `Field` as a default value, though the `Annotated` approach is preferred: + +```python +@mcp.tool +def validate_data( + # Value constraints + age: int = Field(ge=0, lt=120), # 0 <= age < 120 + + # String constraints + email: str = Field(pattern=r"^[\w\.-]+@[\w\.-]+\.\w+$"), # Email pattern + + # Collection constraints + tags: list[str] = Field(min_length=1, max_length=10) # 1-10 tags +): + """Process data with field validations.""" + # Implementation... +``` + +Common validation options include: + +| Validation | Type | Description | +| :--------- | :--- | :---------- | +| `ge`, `gt` | Number | Greater than (or equal) constraint | +| `le`, `lt` | Number | Less than (or equal) constraint | +| `multiple_of` | Number | Value must be a multiple of this number | +| `min_length`, `max_length` | String, List, etc. | Length constraints | +| `pattern` | String | Regular expression pattern constraint | +| `description` | Any | Human-readable description (appears in schema) | + +When a client sends invalid data, FastMCP will return a validation error explaining why the parameter failed validation. + +## Server Behavior + +### Duplicate Tools + + + +You can control how the FastMCP server behaves if you try to register multiple tools with the same name. This is configured using the `on_duplicate_tools` argument when creating the `FastMCP` instance. + +```python +from fastmcp import FastMCP + +mcp = FastMCP( + name="StrictServer", + # Configure behavior for duplicate tool names + on_duplicate_tools="error" +) + +@mcp.tool +def my_tool(): return "Version 1" + +# This will now raise a ValueError because 'my_tool' already exists +# and on_duplicate_tools is set to "error". +# @mcp.tool +# def my_tool(): return "Version 2" +``` + +The duplicate behavior options are: + +- `"warn"` (default): Logs a warning and the new tool replaces the old one. +- `"error"`: Raises a `ValueError`, preventing the duplicate registration. +- `"replace"`: Silently replaces the existing tool with the new one. +- `"ignore"`: Keeps the original tool and ignores the new registration attempt. + +### Removing Tools + + + +You can dynamically remove tools from a server using the `remove_tool` method: + +```python +from fastmcp import FastMCP + +mcp = FastMCP(name="DynamicToolServer") + +@mcp.tool +def calculate_sum(a: int, b: int) -> int: + """Add two numbers together.""" + return a + b + +mcp.remove_tool("calculate_sum") +``` diff --git a/docs/snippets/local-focus.mdx b/docs/snippets/local-focus.mdx new file mode 100644 index 000000000..4da6ac752 --- /dev/null +++ b/docs/snippets/local-focus.mdx @@ -0,0 +1,7 @@ +export const LocalFocusTip = () => { + return ( + + This integration focuses on running local FastMCP server files with STDIO transport. For remote servers running with HTTP or SSE transport, use your client's native configuration - FastMCP's integrations focus on simplifying the complex local setup with dependencies and uv commands. + + ); +}; \ No newline at end of file diff --git a/docs/snippets/version-badge.mdx b/docs/snippets/version-badge.mdx new file mode 100644 index 000000000..cbb28cb9d --- /dev/null +++ b/docs/snippets/version-badge.mdx @@ -0,0 +1,12 @@ +export const VersionBadge = ({ version }) => { + return ( + +

+ New in version:  + {version} +

+
+ + + ); +}; \ No newline at end of file diff --git a/docs/snippets/youtube-embed.mdx b/docs/snippets/youtube-embed.mdx new file mode 100644 index 000000000..1c36e6cfd --- /dev/null +++ b/docs/snippets/youtube-embed.mdx @@ -0,0 +1,12 @@ +export const YouTubeEmbed = ({ videoId, title }) => { + return ( +