Back to Blog
March 17, 2026

Avoiding Stateful Pitfalls in MCP Servers for Microsoft 365 Copilot Studio

Share

Avoiding Stateful Pitfalls in MCP Servers for Microsoft 365 Copilot Studio

Date: 2026-03-17

Learn why building stateless MCP Servers is essential when integrating with Microsoft 365 Copilot Studio and how a simple configuration tweak can save your agents from session errors.

Tags: ["Microsoft 365", "Copilot Studio", "MCP", "Azure", "Serverless"]

Avoiding Stateful Pitfalls in MCP Servers for Microsoft 365 Copilot Studio

Integrating Microsoft 365 Copilot Studio with custom Model Context Protocol (MCP) Servers can enable AI-assisted workflows, but it comes with challenges related to state management within MCP Servers. If your MCP Server holds session state, your Copilot Agent may encounter errors like “Session not found,” disrupting the user experience and complicating troubleshooting.

In this post, we explore why statelessness matters for MCP Servers running alongside Copilot Studio, especially when deployed on serverless or ephemeral environments. We examine what causes these session errors, why stateful designs are problematic, and how a simple switch in your MCP Server’s configuration can improve reliability.

By the end, you’ll understand this important nuance in building MCP Servers and be equipped with practical tips to avoid common pitfalls when working with Copilot Studio integration.

Architecture Overview

The architecture involves enterprise data sources, the Foundry platform, and AI applications interacting with MCP Servers and Copilot Studio.

Key Technical Observations

  • Session ID Dependency in MCP Servers — MCP Servers interacting with Copilot Studio maintain session IDs to correlate requests and manage stateful interactions. Losing or expiring these session IDs breaks ongoing agent-server communication.

  • Serverless Platforms Scale-to-Zero Impact — Hosting MCP Servers on serverless platforms like Azure Functions often leads to processes shutting down when idle, clearing in-memory session state and causing session expirations.

  • Stateless Mode Configuration Bypasses Session Checks — The .WithHttpTransport method in the MCP .NET SDK has a Stateless flag. Setting Stateless = true disables session state management, disables server-sent events (SSE) endpoints, and makes the MCP Server treat each request independently.

  • Trade-offs of Stateless MCP Servers — Stateless mode disables unsolicited server-to-client messages and root capabilities because the server cannot make request callbacks, meaning some features requiring persistent state or ongoing dialogs aren't supported.

  • Error Propagation is Non-Transparent — The session expiration issue surfaces as a JSON-RPC error { "code": -32001, "message": "Session not found" } on the backend, rarely visible to end users until failure occurs, making fault diagnosis challenging.

  • MCP SDK Provides Clear Documentation on Session Behavior — The official Model Context Protocol .NET SDK doc explicitly defines how stateless mode disables session handling and what features are impacted, enabling informed design decisions.

How It Works: Building a Stateless MCP Server

To work around session expiration issues, especially on serverless hosts, the key is to build your MCP Server stateless.

Understanding Session Handling in MCP

By default, the MCP Server expects to maintain session IDs (MCP-Session-Id header) to keep track of conversations. This session ID is used internally to route messages and maintain context.

However, if the server process recycles or the session ID expires, the Copilot Agent is disconnected and receives errors. This is particularly problematic on serverless hosts where processes can be short-lived.

Switching to Stateless Mode

The MCP .NET SDK exposes HTTP transport configuration through the WithHttpTransport method. Here's the crucial snippet you need:

var options = new HttpServerTransportOptions()
{
    Stateless = true
};

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMcpServer()
    .WithHttpTransport(options);

Setting Stateless = true instructs the MCP Server to:

  • Ignore session IDs (SessionId will be null)
  • Disable the /sse endpoint (no server sent events)
  • Call the RunSessionHandler for every single request independently
  • Disable client-initiated sampling and root-level capabilities

This means your MCP Server acts like a pure request/response HTTP API with no internal session state. Each incoming request is handled fresh, avoiding session expiration altogether.

Why This Matters

This approach aligns with ephemeral serverless environments that may shut down and spin up instances on demand. Your server does not need to hold session state in memory or persistent storage, eliminating a common cause of Copilot Agent errors.

When Stateful Might Still Be Needed

If your MCP Server must hold context to support complex multi-turn conversations or unsolicited messages, stateless mode may not be feasible. In these cases, you need to implement external persistence for session data and design robust recovery and expiration logic, which is an advanced topic still evolving in the Copilot ecosystem.

Quick Tips & Tricks

  1. Enable Stateless Mode Early During Development — Avoid session headaches by setting Stateless = true while prototyping MCP Servers.

  2. Monitor JSON-RPC Errors Closely — Look for error code -32001 "Session not found" to quickly identify session expiration problems.

  3. Host MCP Servers Where You Can Control Process Lifetime — If statefulness is required, consider using container-based hosting (e.g., Azure Container Apps) instead of purely serverless functions to maintain lifetime.

  4. Review MCP SDK Documentation Regularly — The Model Context Protocol and Copilot Studio are evolving, so keep an eye on official updates for new features or fixes around session management.

  5. Plan for External State Storage if Needed — Use distributed caches or durable storage if your MCP Server must maintain rich session state across requests.

  6. Use Logging to Correlate Sessions — When debugging, log all incoming MCP-Session-Id headers and their lifecycle to detect expiry or session loss patterns.

Conclusion

When building MCP Servers for Microsoft 365 Copilot Studio, managing session state is a critical concern, especially on serverless or ephemeral hosting platforms. Stateful server designs frequently encounter "Session not found" errors once their in-memory session expires, breaking communication with Copilot Agents and harming user experience.

The simplest and most robust workaround today is to embrace stateless MCP Servers by enabling the Stateless configuration option in your MCP .NET SDK HTTP transport. This sidesteps session ID management altogether, improving reliability and compatibility with modern serverless environments.

While stateless mode disables some advanced capabilities like unsolicited messages, it solves a pressing stability problem and allows you to iterate quickly. For implementations that require maintaining complex state, external session persistence strategies will be required as the ecosystem matures.

This pattern is an important consideration for developers working with MCP and Copilot Studio—helping ensure your AI-powered agents remain resilient and responsive.

References

  1. Errors with Copilot Studio and MCP? – Are you Stateless? | Simon Doy’s Microsoft 365 and Azure Dev Blog — Original analysis and workaround description.
  2. Model Context Protocol .NET SDK HttpServerTransportOptions Documentation — Definitive source for stateless config.
  3. My Adventures in building and understanding MCP for Microsoft 365 Copilot | Simon Doy — Related deep dive into MCP Server development.
  4. How to: Build a Custom MCP Server with the .NET MCP SDK, host as an Azure Container and connect to Copilot Studio — Guide for hosting MCP Servers effectively.
  5. Missing Topics And Agent Components in your Deployed Copilot Studio Solutions? Try This — Additional helpful MCP troubleshooting techniques.