Background server #32
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background Server Host Implementation
Problem
Currently, the MCP server is designed to be the primary entry point of an application, blocking the main thread. This makes it difficult to integrate into larger applications where the MCP server should be a component rather than the main process.
Solution
I've introduced a new
MCPServerHostclass that provides a background thread execution model for the MCP server. This allows the server to run asynchronously while the main application continues executing. Key features include:IAsyncDisposableJoinableTaskFactoryImplementation Details
MCPServerHostclass inCore/MCPServerHost.csMCPServerto expose its instance and makeDisposepublicMCPSharp.Example.BackgroundServerdemonstrating usageExample Usage
Testing
The changes include a new example project that demonstrates the background server functionality. The example shows how to:
Additional Context
This implementation allows the MCP server to be used as a component in larger applications while maintaining its core functionality. The background execution model ensures that the server doesn't block the main application thread, making it suitable for integration into existing codebases.
Additional Note
I wrote this before you added net8.0/net9.0 targeting, so I didn't use the newer IHostedService/DI/BackgroundService stuff. This worked on netstandard2.0 and still does. That said, I can rewrite with the newer patterns if you prefer.