Skip to content

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

@JeffreySu

Description

@JeffreySu

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

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions