Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Building a Scalable FastAPI Server for Diverse Tools

Hello! Welcome back.

In the last module, you successfully navigated a complex, multi-step refactoring task, culminating in updating the test suite to validate the new asynchronous code. You demonstrated a robust, professional workflow for AI-assisted development.

Now, we pivot from modifying existing code to architecting new capabilities. This module, Advanced MCP Server Integration, is where you'll build the custom infrastructure to supercharge your Claude Code workflows. Our first learning outcome is to architect an extensible MCP server with FastAPI designed to host multiple stateful and stateless tools.

This lesson moves beyond the basics of creating a single tool. We'll focus on the architectural decisions required to build a robust, scalable platform capable of hosting a diverse suite of tools—some that are simple, fire-and-forget functions (stateless), and others that need to remember context across multiple interactions (stateful).

The Big Picture: Your MCP Server as an Intelligent Gateway

An MCP server acts as an intelligent gateway, translating high-level requests from an AI agent into concrete actions on your backend systems. Before we dive into the design patterns, let's visualize where our server fits into the ecosystem.

Model Context Protocol (MCP) Architecture and Custom Server Build Guide
This diagram shows the overall MCP architecture. Our focus is on the "MCP Server" component, which we will build to expose custom "Tools" that can interact with databases and other services.

We are building a remote server that can be accessed by any MCP client, like Claude Code. This server will become a central hub for your custom tools, making them available wherever you're working.

Remote MCP Server Architecture Diagram
This illustrates our goal: a remote MCP server, built with FastAPI and running in the cloud, exposing multiple tools to clients like Claude Code.

The Core Architectural Challenge: Managing State

The most critical decision when architecting an MCP server is how to handle state. A stateless tool is simple: it receives a request, performs an action, and returns a response, forgetting everything immediately. A calculator tool is a perfect example.

A stateful tool, however, must maintain context across multiple calls. Imagine a tool for querying a large dataset. The first call might connect to the data source, the second might apply a filter, and a third might perform an aggregation. The tool needs to remember the connection and the filter from previous steps.

This requirement for memory, or "state," is fundamental to building powerful agentic workflows. A fantastic technical article from Zeo, "MCP Server Architecture," provides a blueprint for thinking about this problem. It introduces three models for state management.

MCP Server Architecture: State Management, Security & Tool Orchestration - Zeo

To understand the architectural trade-offs, please read the following section from the 'MCP Server Architecture' article. It details different models for handling state in multi-turn interactions, which is the foundational concept for this lesson.

Please read the entire section titled State Management Models for Multi-Turn Interactions. Focus on understanding the core ideas behind: Model 1: Server-Side State Cache: How it uses a central store like Redis and a session ID to maintain state. Model 2: Client-Side State via Ephemeral Pointers: How it offloads state by giving the client signed, temporary links to state stored elsewhere (like S3). The Comparative Matrix and Decision Framework: Pay close attention to the trade-offs in security, performance, scalability, and complexity for each model.

As you can see from the reading, while multiple patterns exist, the Server-Side State Cache model is often the most practical and secure starting point for building sophisticated, stateful tools. It gives the server full control over the conversational context. For our purposes, we can think of your PostgreSQL database as a more persistent, structured version of this state cache.

Practical Implementation Patterns with FastAPI

Now let's translate this architectural theory into practice using FastAPI. Your experience with FastAPI will be a great asset here.

Distinguishing Stateful and Stateless Connections

The MCP protocol itself has evolved to better handle the distinction between stateful and stateless interactions. The modern streamable HTTP transport is more flexible than the older SSE (Server-Sent Events) transport.

This first video provides a clear, concise demonstration of how this works in fastmcp, the library we'll be using.

Model Context Protocol - Stateless MCP Servers with Streamable HTTP - Why this is BIG!

The video 'Stateless MCP Servers with Streamable HTTP' clearly shows how to configure a fastmcp server for either stateful or stateless operation at the connection level.

Please watch from the beginning until 05:30. Pay close attention to: The difference between a stateful connection (which requires an initialize handshake) and a stateless one (which is a single HTTP request). The stateless_http parameter in the FastMCP class constructor (01:51) and how setting it to True or False changes the server's behavior. How a stateless request is much simpler, omitting the session management overhead (04:29).

This shows that you can decide at the server level whether you want to enforce session management for all tools or allow simple, one-off calls. For an extensible server hosting both types of tools, enabling stateless HTTP is generally preferable as it provides maximum flexibility. You can then build state management into specific tools that need it.

Structuring a Multi-Tool Server

Now, let's look at the structure for hosting multiple tools and implementing a stateful tool using a database—a perfect real-world example of the "Server-Side State Cache" pattern.

Build ANYTHING With an Advanced MCP Server (Python, Authentication, Databases & More)

The video 'Build ANYTHING With an Advanced MCP Server' provides an excellent end-to-end walkthrough of creating a multi-tool server with authentication and database-backed state.

This is a longer video, so we'll focus on two key parts: Initial Server and Tool Setup (14:32 - 18:39): Watch this segment to see how easily you can define multiple tools (get_my_notes, add_note) in a single FastAPI application using the @mcp.tool decorator. Implementing State with a Database (53:22 - 1:03:14): This is the most crucial part for our lesson. Observe how the author: Uses SQLAlchemy to define a Note model and a repository to interact with a SQLite database. Modifies the add_note and get_my_notes tools to use this repository. Uses a user_id (obtained from an authentication token) to associate notes with a specific user, thereby making the tools stateful for that user.

This video demonstrates the key architectural pattern for our extensible server:

  1. A single FastAPI application serves as the host.
  2. Stateless tools can be simple functions decorated with @mcp.tool that take input and return output.
  3. Stateful tools are also functions decorated with @mcp.tool, but they contain logic to read from and write to a persistent store (like your PostgreSQL database) using a unique identifier (like a user ID or session ID) to retrieve the correct context for the current interaction.

This architecture is highly extensible. To add a new stateless tool, you just add a new decorated function. To add a new stateful tool, you define its data model in your database and add a new decorated function that interacts with that model.

Your Architectural Blueprint

Based on these principles, here is a blueprint for your extensible MCP server:

  1. Framework: Use FastAPI as the web server and fastmcp for the MCP implementation.
  2. Transport: Use streamable HTTP, configured for stateless connections (stateless_http=True) to allow maximum flexibility.
  3. Tool Definition: Define each tool as a separate Python function decorated with @mcp.tool. The function's name, docstring, and typed parameters will automatically become the tool's definition for Claude.
  4. State Management:
    • For stateless tools (e.g., a code formatter, a simple calculator), the function will perform its logic and return a result without any database interaction.
    • For stateful tools (e.g., a database query builder, a multi-step refactoring assistant), the function will connect to your PostgreSQL database (using SQLAlchemy or asyncpg). It will use a session identifier passed into the tool to store, retrieve, and update state across multiple calls.
  5. Extensibility: Place tools in separate Python modules (e.g., tools/database_tools.py, tools/file_tools.py) and import them into your main FastAPI application. This keeps the project organized and makes it easy to add or remove functionality.

Conclusion

You now have a solid architectural foundation for building an advanced MCP server. We've moved from abstract concepts to a concrete, actionable blueprint tailored to your skills and goals.

Key Takeaways:

  • State management is the central architectural decision. The Server-Side State Cache (using your database as a persistent store) is a robust and secure pattern for building complex tools.
  • An extensible MCP server can host both stateful and stateless tools. You can choose the right implementation for each tool's needs.
  • Using FastAPI with fastmcp provides a modern, high-performance stack for building these servers.
  • The key to a stateful tool is using a unique identifier (like a user or session ID) to link successive calls to the correct data in your persistent store.

Next Up:
With the architecture defined, our next step is to get our hands dirty. In the next lesson, you will implement and expose a stateful tool in a custom MCP server that maintains context across multiple calls. We will build on the database pattern from today's lesson to create a tool that can remember and build upon previous interactions within a single conversation.

Can't find a good explanation? Sign up and we'll make it for you

Sign up