<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/5-lines-of-code-turned-my-fastapi-app-into-dozens-of-ai-agent-tools-qm2z1l1vm" -->

---
title: 5 Lines of Code Turned My FastAPI App Into Dozens of...
description: Using the `fastapi-mcp` library, a production FastAPI classification API was automatically exposed as MCP tools for AI agents in about 5 lines of code. Every...
canonical: https://daily.dev/posts/5-lines-of-code-turned-my-fastapi-app-into-dozens-of-ai-agent-tools-qm2z1l1vm
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: 5 Lines of Code Turned My FastAPI App Into Dozens of AI-Agent Tools | daily.dev
og:description: Using the `fastapi-mcp` library, a production FastAPI classification API was automatically exposed as MCP tools for AI agents in about 5 lines of code. Every...
og:url: https://daily.dev/posts/5-lines-of-code-turned-my-fastapi-app-into-dozens-of-ai-agent-tools-qm2z1l1vm
og:image: https://api.daily.dev/og/posts/qm2z1l1VM.png
og:image:alt: 5 Lines of Code Turned My FastAPI App Into Dozens of AI-Agent Tools
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# 5 Lines of Code Turned My FastAPI App Into Dozens of AI-Agent Tools

**[Medium](https://daily.dev/sources/medium_js)** · 18 min read · 0 upvotes · 0 comments

## Summary

Using the `fastapi-mcp` library, a production FastAPI classification API was automatically exposed as MCP tools for AI agents in about 5 lines of code. Every REST endpoint became an MCP tool with no hand-written schemas, and existing JWT-based RBAC was forwarded automatically. The post covers the minimal mount, production hardening (custom httpx client for timeout handling, ASGITransport for in-process calls, mount ordering), a critical security backdoor discovered and fixed (a server-side admin token that silently elevated all agent callers), and practical gotchas like ugly auto-generated tool names, silent 401s for unauthenticated agents, and observability instrumentation ordering conflicts. The author argues this pattern works because a well-built FastAPI app already maintains a machine-readable contract that MCP simply reuses.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://medium.com/@pramukha/5-lines-of-code-turned-my-fastapi-app-into-dozens-of-ai-agent-tools-7a7396258685>

## Questions this post answers

### How do I expose an existing FastAPI app's endpoints as MCP tools for AI agents like Claude Desktop?

Mount the fastapi-mcp library (version 0.4.x) onto the FastAPI app: import FastApiMCP, instantiate it with the app and a name/description, then call mcp.mount(). This serves every existing route as an MCP tool over SSE at /mcp, using the operation ID, docstring, and Pydantic request model already present on each endpoint, with no new service or port required.

_Developers wiring agent tooling onto existing APIs can find integration write-ups like this through daily.dev._

### Why did forwarding a default Authorization header in an MCP tool mount create a security backdoor?

An early implementation minted a static admin JWT server-side and attached it as a default Authorization header on the internal HTTP client used for tool invocations, so any unauthenticated MCP caller silently executed every tool as an admin even though endpoint role checks still ran. The fix was full removal of the service token and fallback credential, backed by a regression test that scans the source file for the token-minting import and the service-account string.

_Teams evaluating agent-facing API auth patterns often track pitfalls like this via daily.dev before shipping._

### Why do MCP tool calls return 401 even though list_tools shows all the tools successfully?

Discovery through list_tools does not require authentication, but calling a protected tool does, so an MCP client without a forwarded bearer token sees a full tool list and then gets 401 responses on every protected call, which looks like broken tooling rather than a missing login. The fix is passing headers with the Authorization bearer token into the SSE client session explicitly.

_Anyone debugging silent auth failures in MCP integrations can surface fixes like this through daily.dev._

## Similar posts on daily.dev

- [Medium](https://daily.dev/posts/medium-avonr6rrx) · Medium · 0 upvotes · 0 comments
- [Why More MCP Tools Can Make an AI Agent Less Useful](https://daily.dev/posts/why-more-mcp-tools-can-make-an-ai-agent-less-useful-piqjmps6m) · Dev Genius · 3 upvotes · 1 comments
- [Creating an MCP Server from OpenAPI Documentation](https://daily.dev/posts/creating-an-mcp-server-from-openapi-documentation-gtz4uv4kd) · marmelab · 6 upvotes · 0 comments
- [FastMCP Makes MCP Easy. The First Risk Is Exposing Too Much.](https://daily.dev/posts/fastmcp-makes-mcp-easy-the-first-risk-is-exposing-too-much--4o7rze5ca) · Medium · 1 upvotes · 0 comments
- [Learn MCP Essentials and How to Create Secure Agent Interfaces with FastMCP](https://daily.dev/posts/learn-mcp-essentials-and-how-to-create-secure-agent-interfaces-with-fastmcp-y5hvuxmnv) · freeCodeCamp · 6 upvotes · 0 comments

---

Tags: [#python](https://daily.dev/tags/python), [#ai-agents](https://daily.dev/tags/ai-agents), [#mcp](https://daily.dev/tags/mcp), [#fastapi](https://daily.dev/tags/fastapi)

[View this post on daily.dev](https://daily.dev/posts/5-lines-of-code-turned-my-fastapi-app-into-dozens-of-ai-agent-tools-qm2z1l1vm)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"5 Lines of Code Turned My FastAPI App Into Dozens of AI-Agent Tools","url":"https://daily.dev/posts/5-lines-of-code-turned-my-fastapi-app-into-dozens-of-ai-agent-tools-qm2z1l1vm","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/5-lines-of-code-turned-my-fastapi-app-into-dozens-of-ai-agent-tools-qm2z1l1vm"},"datePublished":"2026-07-24T06:42:18.074Z","dateModified":"2026-09-13T20:54:38.018Z","description":"Using the `fastapi-mcp` library, a production FastAPI classification API was automatically exposed as MCP tools for AI agents in about 5 lines of code. Every...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/2d205bfd3ef0f460f862b993e4c26523?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/2d205bfd3ef0f460f862b993e4c26523?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Medium","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Medium","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/medium","url":"https://daily.dev/sources/medium_js"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/5-lines-of-code-turned-my-fastapi-app-into-dozens-of-ai-agent-tools-qm2z1l1vm","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"python,ai-agents,mcp,fastapi","timeRequired":"PT18M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Medium","item":"https://daily.dev/sources/medium_js"},{"@type":"ListItem","position":3,"name":"5 Lines of Code Turned My FastAPI App Into Dozens of AI-Agent Tools"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/5-lines-of-code-turned-my-fastapi-app-into-dozens-of-ai-agent-tools-qm2z1l1vm#faq","mainEntity":[{"@type":"Question","name":"How do I expose an existing FastAPI app's endpoints as MCP tools for AI agents like Claude Desktop?","acceptedAnswer":{"@type":"Answer","text":"Mount the fastapi-mcp library (version 0.4.x) onto the FastAPI app: import FastApiMCP, instantiate it with the app and a name/description, then call mcp.mount(). This serves every existing route as an MCP tool over SSE at /mcp, using the operation ID, docstring, and Pydantic request model already present on each endpoint, with no new service or port required. Developers wiring agent tooling onto existing APIs can find integration write-ups like this through daily.dev."}},{"@type":"Question","name":"Why did forwarding a default Authorization header in an MCP tool mount create a security backdoor?","acceptedAnswer":{"@type":"Answer","text":"An early implementation minted a static admin JWT server-side and attached it as a default Authorization header on the internal HTTP client used for tool invocations, so any unauthenticated MCP caller silently executed every tool as an admin even though endpoint role checks still ran. The fix was full removal of the service token and fallback credential, backed by a regression test that scans the source file for the token-minting import and the service-account string. Teams evaluating agent-facing API auth patterns often track pitfalls like this via daily.dev before shipping."}},{"@type":"Question","name":"Why do MCP tool calls return 401 even though list_tools shows all the tools successfully?","acceptedAnswer":{"@type":"Answer","text":"Discovery through list_tools does not require authentication, but calling a protected tool does, so an MCP client without a forwarded bearer token sees a full tool list and then gets 401 responses on every protected call, which looks like broken tooling rather than a missing login. The fix is passing headers with the Authorization bearer token into the SSE client session explicitly. Anyone debugging silent auth failures in MCP integrations can surface fixes like this through daily.dev."}}]}
```

