Zero Labs
Industry News / AI

Stop Building AI Apps for Every Idea. Start Building MCP Servers — Part #2

Source · Towards AI By Andrii Tkachuk

From the publisher’s feed, collected in the ZeroLabs newsroom.

Over the past year, the Model Context Protocol has quietly moved from “interesting Anthropic experiment” to the default way AI clients connect to the outside world. Claude, ChatGPT, Gemini, Grok, Mistral — they all speak it now. And once a protocol becomes cross-vendor, the conversation stops being “should I use this?” and starts being “how do I build something on top of it that doesn’t fall over the moment real users show up?” That’s the question this article tries to answer.Photo by Ryan Quintal on UnsplashBefore we start!🦾If this piece gives you something practical you can take into your own system:👏 leave 50 claps (yes, you can!) — Medium’s algorithm favors this, increasing visibility to others who then discover the article.🔔 Follow me on Medium and LinkedIn for more deep dives into agentic systems, LLM architecture, and production-grade AI engineering.In Part #1, I argued that the center of gravity in applied AI is shifting. The UI is becoming the shell. MCP servers are becoming the product. Your leverage isn’t in rebuilding chat for the hundredth time — it’s in the capability layer.Stop Building AI Apps for Every Idea. Start Building MCP Servers — Part #1That part was about why. This one is about how — specifically, how a server survives contact with production.Because here’s what I keep seeing: a team reads about MCP, spins up a FastMCP instance, decorates a few functions with @mcp.tool(), points Claude at it over stdio, and ships. It works. People are happy. Then someone says "let's deploy this so the rest of the company can use it" — and a quiet wall of complexity shows up that nobody warned them about.Sampling stops working. Sessions get lost behind the load balancer. Logs disappear. Long-running tools feel like they hang. The same code that worked locally behaves differently in the cloud, and nobody can quite say why.This article is the production-readiness layer that Part #1 deliberately skipped. I’m an Applied AI Architect by day — I’ve built and shipped a number of these servers, and I’ve turned the recurring decisions into a cookiecutter template so I never have to make them twice. What follows are the parts of that template that aren’t really about boilerplate — they’re about understanding the protocol well enough to make the right architectural calls.The transport layer is an architectural decision, not a config flagMost introductory MCP material treats transport as a checkbox: pick stdio for local, streamable-http for remote, done. That framing hides the most consequential decision you'll make about your server.On top of that, you might be surprised, but even official MCP servers from very well-known vendors are not always built perfectly. Many of them are still based on stdio and are designed mainly for very trivial use cases 🤯stdio is straightforward. The server runs as a subprocess, MCP messages flow over standard input and output, and the client and server are essentially attached at the hip. No network, no sessions, no auth at the transport layer. Perfect for local tools, Claude Desktop integrations, and anything where the server's lifecycle is tied to a single client process.streamable-http is what you reach for when the server needs to live on the network and serve many clients. And here's where the protocol's design starts pushing back on naive HTTP thinking.MCP is bidirectional by design. The server doesn’t just respond to the client’s calls — it can initiate requests back to the client. That’s how features like sampling work (the server asks the client to call its LLM on the server’s behalf). It’s how list_roots works (the server asks the client which directories it's allowed to touch). It's how progress and log streaming work during a long-running tool call.HTTP, fundamentally, doesn’t allow that. Clients call servers; servers respond. So streamable-http works around it with a clever pattern: two parallel SSE channels per session. A primary SSE stream stays open for the entire session and carries server-initiated requests and notifications. A second, ephemeral SSE stream is opened for each tool call and closes when the tool returns its result. Progress notifications flow through the primary stream. Logs and the tool result flow through the call-specific one.Once you internalize that model, a lot of “weird” production behavior stops being weird. It’s not weird; it’s just the cost of putting a bidirectional protocol on top of a unidirectional transport.The two flags that quietly remove half the protocolFastMCP exposes two settings that look innocent and aren't: stateless_http and json_response. They're the most consequential knobs in the entire framework, and most teams flip them without fully understanding what they're giving up.stateless_http=True tells the server not to track sessions. No session IDs, no persistent SSE channel per client, no memory of who's talking to whom between requests. In return, you get something genuinely valuable: the server becomes trivially horizontally scalable. Any instance behind a load balancer can answer any request. No sticky sessions, no inter-instance coordination, no shared state to synchronize.The cost: anything that depends on the server initiating communication back to the client is gone. No sampling. No list_roots. No progress notifications during a long tool call. No subscriptions to resource changes. The server can answer questions; it cannot have a conversation.json_response=True is narrower. It disables streaming for tool-call responses specifically — instead of opening a tool-specific SSE stream and possibly emitting progress and log messages along the way, the server just returns a plain JSON response when the tool finishes. You lose intermediate updates within a single call; you keep the rest of the protocol if stateless_http is False.These flags are independent, and the combinations matter:stateless_http json_response What you get What you lose False False Full MCP — sampling, progress, logs, server→client requests Need sticky sessions on the load balancer False True Sampling, list_roots, full session state No streaming progress/logs within a tool call True True Stateless horizontal scaling, simplest deployment Sampling, progress streaming, server-initiated anything True False Almost no advantage over the row above Avoid this combination — it's the worst of both worldsThe decision is rarely “what flags do I want.” It’s “what does my server actually need to do?” A server whose tools just hit a database and return rows can run fully stateless and scale to a thousand instances behind any boring load balancer. A server whose tools call back to the LLM via sampling, or stream progress through a five-minute job, fundamentally cannot — and pretending otherwise will produce mysterious, intermittent failures in production that are very hard to debug.In my template, the default is stateless_http=True, json_response=True — because most internal capability servers don't need bidirectionality and the operational simplicity is enormous. When a server does need sampling or streaming, that's a deliberate departure from the default with sticky sessions configured at the load balancer or a different deployment topology entirely.Sampling: the feature that quietly inverts your architecturePart #1 mentioned that an MCP server can do “much more than wrap APIs” — it can host real business logic, orchestration, policy. Sampling is the feature that takes this to its logical extreme, and almost nobody talks about it.Sampling is a request the server makes to the client: “please run this prompt against your LLM and give me the result.” The server doesn’t need its own model credentials. It doesn’t need its own LLM. It just describes what it wants the model to do, and the client — which already has the user’s authenticated, paid-for LLM access — does it.This sounds small. It isn’t. It changes who owns the LLM call.Without sampling, every server that needs an LLM has to either embed model calls itself (with its own API keys, cost center, vendor lock-in, rate limits) or do without. With sampling, your tool can say “summarize these 200 search results” or “rewrite this in formal English” or “extract structured data from this blob” and the user’s LLM does the work. The server provides capability and policy; the client provides intelligence.For internal enterprise tooling, this is huge. The user’s company is already paying for Claude or GPT. Your server doesn’t need a separate model budget, separate vendor relationship, separate compliance review for the LLM provider. The provider was already approved when the AI client was approved.The catch is that sampling is server-initiated, which means it cannot work in a fully stateless deployment. If your server’s value proposition includes calling an LLM as part of its tools, you’ve made an architectural commitment: stateful sessions, sticky load balancing, and a more careful deployment story. That’s a fine commitment to make — just make it consciously.Roots: the permission model nobody implementsRight alongside sampling sits its mirror image: roots. Where sampling lets the server ask the client to use its LLM, roots let the server ask the client which files and directories it’s allowed to touch.A root is just a file:// URI that the client hands to the server when asked. The server stores nothing — it queries list_roots whenever it needs to know what's accessible. This solves a very real UX problem: when a user says "convert biking.mp4," nobody wants to type /Users/me/Movies/biking.mp4. The server can ask the client for its roots, search inside them, find the file, and proceed. Without ever having access to anything outside that boundary.The thing the SDK won’t tell you, but you absolutely need to know: roots are a contract, not an enforcement mechanism. The SDK exposes list_roots and trusts you to honor the answer. Nothing prevents you from writing a tool that ignores roots entirely and reads ~/.ssh/id_rsa. The protocol just describes the shape of the conversation; the protection only exists because the server author writes a is_path_allowed() helper and calls it before every filesystem operation.In a production server, that helper is non-negotiable. It’s a small function — resolve the requested path, fetch roots from the session, check that the path is contained in at least one root — but it’s the difference between a tool that respects the user’s intent and a tool that’s a security incident waiting to happen. In my template, every filesystem-touching tool calls it as the very first thing inside the function body, before any I/O. It costs nothing at runtime. It costs everything to forget.Authentication: the part that separates real servers from demosPart #1 noted that FastMCP supports OAuth flows through providers like Cognito, Entra ID, GitHub, Google. That’s true and the docs are good. But “the docs are good” isn’t the same as “you have a deployment plan.” Let me describe what production OAuth actually looks like, because it’s where I see the most prototypes go to die.The starting point: in development, you have no auth. The server is on localhost, you're the only user, sign-in feels like overkill. In production, you immediately need three things — and only the first one is OAuth itself.The first is the OAuth flow proper: the server validates incoming access tokens against the identity provider, doesn’t trust the client’s word about who it is, and rejects calls without a valid token. FastMCP gives you this with relatively little code. You pick a provider, configure issuer URLs and audience, and the framework handles validation on every request.The second is encrypted state storage. OAuth involves moving tokens around — refresh tokens, client secrets, sometimes raw access tokens during a flow. Putting these in any datastore in plaintext is the kind of thing that gets you a phone call from your security team. The principle is straightforward: anything sensitive that you persist gets encrypted at the application layer before it hits the database, with a key derived from a server secret. Even if someone reads the underlying table, they see ciphertext. Pair that with a TTL on transient records so stale state cleans itself up automatically — no cron jobs, no garbage to forget about. The specific tools (Fernet, KMS, your favorite KV store with built-in TTL) are interchangeable; the concept is non-negotiable.The third is the principal mapping: who, in your system, is making this call? When a user authenticates, you record them in a principals table. On every subsequent tool call, you update last_seen_at. That single field answers a half-dozen questions managers will eventually ask: who's actually using this thing, who hasn't logged in in three months, can we safely rotate that secret without breaking a real workflow? Without it, you're guessing — and "I'll just grep the logs" is not a viable answer when someone wants real numbers.None of these three pieces is exotic. All three are essential. And the reason I bake them into a cookiecutter template is exactly because they’re the kind of decisions that get skipped under deadline pressure and never retrofitted.Why the cookiecutter or any template tool existsIf you’ve followed this far, you can probably feel the underlying point. None of these decisions is hard in isolation. Each one is a few hours of work — sometimes less. The problem is all of them at once, every time you start a new server, with the discipline to not skip any of them under deadline pressure.A cookiecutter template solves this by making the decisions once. When I bootstrap a new MCP server, I get all of the above for free: OAuth wired up, state encryption baked in, structured logging configured, a deployment topology that runs the same code in any target — local, container, serverless, Kubernetes, whatever the team already operates — and pre-commit hooks running tests and security scans. The first day on a new server is spent on the actual capability — the integration, the workflow, the policy logic that’s the entire reason the server exists. Not on rebuilding the harness for the eleventh time.The deployment-agnostic part matters more than it sounds. The principle is simple: one codebase, multiple deployment targets, no special cases. The same Python code should run locally, in a container, on a serverless platform, or on whatever orchestrator the team already operates — without modification. The differences live in the entrypoint, not in the application logic. If the harness is the same everywhere, you stop having “but it worked locally” conversations, and you stop being locked into whatever cloud you happened to start on.When I learn something new — a better way to handle a particular OAuth provider, a faster bootstrap pattern, a cleaner approach to a class of tool — it goes into the template. Every future server inherits it. Existing servers get updated through deliberate PRs, not through hopeful copy-paste.The economics are simple. New MCP server with a new SaaS integration used to be a two-week task starting from a blank repo. With the template, it’s a day and a half — and the day and a half is all on the business logic that’s the whole point. The harness is solved.Final takePart #1 made the case that the future of applied AI is in capability layers, not in another chat UI. Part #2 is the unglamorous follow-up: capability layers in production are a discipline, not a vibe. The protocol gives you a lot of power — bidirectional communication, sampling, roots, streaming, fine-grained permission boundaries — but it also gives you a lot of ways to deploy something that looks fine on a laptop and falls apart at scale.Stop Building AI Apps for Every Idea. Start Building MCP Servers — Part #1The good news is that almost every one of these problems is one-time. Decide your transport model once. Set up your auth and encryption once. Wire your logging once. Bake it into a template once. Then go build the actual capabilities, which is the part that was supposed to matter all along.That’s the part where the leverage actually lives.And that’s a wrap! If you’ve read this far, it probably means you found this article useful or insightful. If that’s the case, consider leaving a few claps or sharing it with your team, please. Thanks for reading! 🚀Stop Building AI Apps for Every Idea. Start Building MCP Servers — Part #2 was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.