Posted in

Build a Flexible MCP Server in .NET with Dynamic Transport Switching

Discover how to build a versatile MCP server in .NET that dynamically switches between STDIO and HTTP transports at runtime using a simple “–http” flag. This approach minimizes code duplication, streamlines deployment, and enhances integration flexibility for enterprise and cloud environments.

Run One MCP Server with Two Transport Options

Imagine simplifying your MCP server deployment by supporting both STDIO and HTTP transports in one app. Usually, MCP servers run locally using STDIO. However, modern enterprise needs and integrations like Copilot Studio demand remote access via HTTP. Managing two separate hosts adds overhead and duplicates code. What if a single MCP server could switch transports at runtime with a simple command-line flag? This approach reduces complexity and streamlines your development pipeline.
“Maintaining two hosts can duplicate code. Using a runtime switch cuts management overhead significantly,” explains the Microsoft developer community.

How the .NET Builder Pattern Enables Flexibility

.NET’s builder pattern makes this dual-transport setup seamless. Both console and web apps use builder instances implementing the same interface, IHostApplicationBuilder. You decide the transport before creating the builder by checking for a –http switch in the command-line arguments or environment variables. For example, running `dotnet run –project MyMcpServer — –http` activates the HTTP transport. This design lets you conditionally create either a HostApplicationBuilder or WebApplicationBuilder. Then, you configure the MCP server with transport-specific services: HTTP middleware for web hosts or STDIO handlers for console hosts. Finally, you build and run the app polymorphically, thanks to shared interfaces.

Practical Benefits for Tech Professionals

This single-server, dual-transport approach offers clear advantages. Firstly, it simplifies deployment pipelines by unifying server builds. Secondly, it enhances security by enabling remote HTTP hosting in controlled environments without rewriting core logic. Lastly, it boosts maintainability, since updates apply to one codebase rather than two. Developers also gain flexibility to switch transports without code changes, just by toggling a runtime flag. This capability is especially useful in hybrid cloud and containerized setups. Sample projects on GitHub demonstrate this pattern, helping you get started quickly.
“A single MCP server supporting multiple transports is a game-changer for scalable, maintainable server architectures,” says a .NET community contributor.
In conclusion, leveraging the .NET builder pattern to run one MCP server with both STDIO and HTTP transports is a smart move. It reduces code duplication, eases management, and adapts to diverse deployment needs. For tech professionals, this means faster iterations, cleaner infrastructure, and more robust integrations. Try this approach in your next MCP project and experience the streamlined power of runtime transport selection.

Key points from the article:

  • Leverage the .NET builder pattern to create a single MCP server supporting both STDIO and HTTP transports
  • Use the “–http” command-line switch for runtime transport selection, enabling seamless environment adaptability
  • Reduce code redundancy by sharing core logic across console and web hosts within one application
  • Integrate HTTP middleware conditionally for secure, scalable remote hosting scenarios like Copilot Studio
  • Access sample MCP server apps demonstrating dual-transport support to accelerate your development workflow
  • From the Microsoft Developer Community Blog articles