Skip to content

[McpServerToolType] class does not support dependency injection for constructors with parameters #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JeffreySu opened this issue Apr 8, 2025 · 8 comments · Fixed by #276
Assignees
Labels
bug Something isn't working

Comments

@JeffreySu
Copy link

JeffreySu commented Apr 8, 2025

Describe the bug
When [McpServerToolType] class has an constructors with arguments need to be dependency injected, it will throw "Cannot resolve scoped service 'xxx' from root provider." when the function was been called by MCP.

To Reproduce
Steps to reproduce the behavior:
I forked this project and built a branch for the Test: https://github.com/JeffreySu/mcp-csharp-sdk/tree/DI-Error-Test/samples/AspNetCoreSseServer

You can run this project and add the Server to a host(like Cursor), then launch "Echo" function, then you can see the error

Image

Expected behavior
Constructors with arguments should be well supported.

Logs

Image Image

Additional context
For this test project, I added a new class named MyData, and added it to DI register progress:

Image

Then add MyData as a parameter to ctor. of EchoTool class:

Image
@JeffreySu JeffreySu added the bug Something isn't working label Apr 8, 2025
@JeffreySu
Copy link
Author

I also created a simple project to reproduce this problem, but there was no reply yet: #114 (comment)

@stephentoub
Copy link
Contributor

I assume it's only for scoped services that you're having troubles? If so, that's likely #198.

@JeffreySu
Copy link
Author

JeffreySu commented Apr 8, 2025

I assume it's only for scoped services that you're having troubles? If so, that's likely #198.

I cannot sure if it's the sample problem. But I found the point:

https://github.com/JeffreySu/mcp-csharp-sdk/blob/343f0e0107e766eeaaad74a5bf0de0fc129d1325/src/ModelContextProtocol/Configuration/McpServerBuilderExtensions.cs#L44-L44

Here just support for AddSingleton, so when the class needs to be scoped as AddScoped, it wouldn't be found.

And when I cry to change the AddSingleton in source code, an other error raised when the console launch:

Image Image

So here is the problem: how could AddTools<> and WithToolsFromAssembly() and other functions support for AddScoped?

PS: I re-read the reply of #198, maybe the source of the problem is the same, but the scenarios are different and complicated. In many scenarios, we want to share this class, and hope that this class can work in the system as AddScroped. As a base library, I think it needs to take into account various usage scenarios.

@JeffreySu
Copy link
Author

@stephentoub Are there any current plans to support it?

BTW: at present, the Nuget package and the main-branch code seem to be out of sync, causing a lot of debugging trouble, can we consider synchronization processing, like using CI/CD ?

@JeffreySu
Copy link
Author

@stephentoub Thank you, I tested with source code is successes, but when I use nuget package is faild with another error:

Image

Does Nuget package out of sync with the source code?

@stephentoub
Copy link
Contributor

We don't publish a nuget package for every commit. As such, the nuget package invariably lags. The package doesn't yet include #276.

@JeffreySu
Copy link
Author

@stephentoub Okay, that makes sense, but it's important that master or some distribution branch and Nuget package are synchronized, otherwise the debugger will get very messy, and we've already wasted 2 days on this.

JeffreySu added a commit to NeuCharFramework/NcfPackageSources that referenced this issue Apr 15, 2025
@stephentoub
Copy link
Contributor

otherwise the debugger will get very messy

Can you elaborate on this? Why is the debugger impacted by the current state of the repo? The source you step into when using the nuget should be the source it was built from, not whatever is current in main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants