👉 Try the demo: https://github.com/LM-Kit/lm-kit-net-samples/tree/main/console_net/agents/mcp_stdio_integration
MCP Stdio Transport Integration for C# .NET Applications
Purpose of the Sample
MCP Stdio Integration demonstrates how to use LM-Kit.NET to connect AI assistants with local MCP servers using the stdio transport. This is the standard transport for running MCP servers as subprocesses with communication via stdin/stdout.
The sample shows how to:
- Spawn local MCP servers as subprocesses using
StdioTransport. - Configure servers with
StdioTransportOptions. - Use the fluent
McpClientBuilderAPI. - Handle process lifecycle, stderr capture, and graceful shutdown.
- Integrate local tools with
MultiTurnConversation.
Why Stdio Transport?
- Local execution: MCP servers run on your machine, no network required.
- Process isolation: each server runs in its own subprocess.
- Ecosystem compatibility: works with all stdio-based MCP servers.
- Environment control: pass environment variables, working directory.
- Lifecycle management: automatic startup, monitoring, and shutdown.
Sample Application Description
Console app that:
- Lets you select from popular local MCP servers (filesystem, memory, Git, SQLite).
- Spawns the server as a subprocess with configured options.
- Establishes JSON-RPC communication over stdin/stdout.
- Registers discovered tools with the LLM assistant.
- Enables natural language interaction with local tools.
Key Features
| Feature | Description |
|---|---|
| Stdio Transport | Subprocess-based MCP communication via stdin/stdout |
| Multiple Server Types | Support for Node.js (npx), Python (uvx), and native executables |
| Process Lifecycle | Automatic startup, monitoring, and graceful shutdown |
| Event Handling | Server notifications, stderr logging, disconnection events |
| Fluent Builder API | Easy configuration with McpClientBuilder |
| Auto-Restart | Optional automatic restart on unexpected process termination |
Stdio vs HTTP Transport
| Aspect | Stdio Transport | HTTP Transport |
|---|---|---|
| Use Case | Local MCP servers | Remote/cloud MCP servers |
| Communication | stdin/stdout (JSONL) | HTTP POST + SSE |
| Server Lifecycle | Managed by client | External |
| Network | Not required | Required |
| Typical Servers | filesystem, git, sqlite | cloud APIs, web services |
Code Examples
Simple Stdio Client
using LMKit.Mcp.Client;
// Quick way to create a stdio client
var client = McpClient.ForStdio("npx", "@modelcontextprotocol/server-filesystem /tmp");
await client.InitializeAsync();
var tools = await client.GetToolsAsync();
foreach (var tool in tools)
{
Console.WriteLine($"Tool: {tool.Name}");
}
Using the Builder Pattern
using LMKit.Mcp.Client;
var client = McpClientBuilder.ForStdio("python", "-m my_mcp_server")
.WithWorkingDirectory("/path/to/server")
.WithEnvironment("API_KEY", "your-secret-key")
.WithEnvironment("DEBUG", "true")
.WithStderrHandler(line => Console.WriteLine($"[Server] {line}"))
.WithRequestTimeout(TimeSpan.FromMinutes(2))
.WithAutoRestart(maxAttempts: 3)
.Build();
await client.InitializeAsync();
With Detailed Options
using LMKit.Mcp.Client;
using LMKit.Mcp.Transport;
var options = new StdioTransportOptions
{
Command = "uvx",
Arguments = "mcp-server-git --repository /path/to/repo",
WorkingDirectory = "/path/to/repo",
Environment = new Dictionary<string, string>
{
["GIT_AUTHOR_NAME"] = "AI Assistant",
["GIT_AUTHOR_EMAIL"] = "ai@example.com"
},
RequestTimeout = TimeSpan.FromSeconds(60),
StderrHandler = line => Console.Error.WriteLine(line),
GracefulShutdown = true,
AutoRestart = true,
MaxRestartAttempts = 3
};
var client = McpClient.ForStdio(options);
await client.InitializeAsync();
Integration with MultiTurnConversation
using LMKit.Model;
using LMKit.TextGeneration;
using LMKit.Mcp.Client;
// Load model
LM model = LM.LoadFromModelID("llama3.1:8b");
// Create stdio MCP client
var mcpClient = McpClientBuilder.ForStdio("npx", "@modelcontextprotocol/server-filesystem /home/user")
.WithStderrHandler(line => Console.WriteLine($"[MCP] {line}"))
.Build();
await mcpClient.InitializeAsync();
// Create conversation with MCP tools
var chat = new MultiTurnConversation(model);
chat.Tools.Register(mcpClient);
// Now the assistant can use filesystem tools
var result = chat.Submit("List all files in the current directory");
Console.WriteLine(result.Completion);
Popular Stdio MCP Servers
Node.js Servers (via npx)
| Server | Package | Purpose |
|---|---|---|
| Filesystem | @modelcontextprotocol/server-filesystem |
Read/write files |
| Memory | @modelcontextprotocol/server-memory |
Key-value storage |
| Fetch | @modelcontextprotocol/server-fetch |
HTTP requests |
| GitHub | @modelcontextprotocol/server-github |
GitHub API |
| Slack | @modelcontextprotocol/server-slack |
Slack messaging |
| PostgreSQL | @modelcontextprotocol/server-postgres |
Database queries |
Python Servers (via uvx)
| Server | Package | Purpose |
|---|---|---|
| Git | mcp-server-git |
Git operations |
| SQLite | mcp-server-sqlite |
SQLite database |
| Brave Search | mcp-server-brave-search |
Web search |
| Google Drive | mcp-server-gdrive |
Google Drive access |
Getting Started
Prerequisites
- .NET 8.0 or later
- VRAM for selected model (3-6 GB depending on model)
- For Node.js servers: Node.js 18+ with npx
- For Python servers: Python 3.10+ with uvx (from uv package manager)
Install Node.js MCP Servers
# No installation needed - npx downloads on first use
npx @modelcontextprotocol/server-filesystem --help
Install Python MCP Servers
# Install uv package manager
curl -LsSf https://astral.sh/uv/install.sh | sh
# Run Python MCP server (downloads automatically)
uvx mcp-server-git --help
Run the Demo
cd demos/console_net/agents/mcp_stdio_integration
dotnet build
dotnet run
Troubleshooting
| Issue | Solution |
|---|---|
| "npx not found" | Install Node.js from https://nodejs.org |
| "uvx not found" | Install uv: curl -LsSf https://astral.sh/uv/install.sh \| sh |
| Server exits immediately | Check stderr output for errors; verify command syntax |
| Timeout errors | Increase RequestTimeout in options |
| Permission denied | Check file/directory permissions for the server |