modelcontextprotocol / modelcontextprotocol/csharp-sdk

AddSingleton does not work as expected

Open
#1,111 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement needs confirmation P3
Dominant language
C#
Stars
4.5k
Forks
814
Avg merge
9d 19h
Merged PRs (30d)
4

Description

Describe the bug
When we register tool like AddSingleton(type, instance) and pass a singleton instance, it seems that the instance is being ignored and the library creates a new instance when it needs it.
I tried also AddSingleton using a factory method like this:
services.AddSingleton(toolType, provider => Activator.CreateInstance(toolType, this.m_compositionService));
A breakpoint to Activator.CreateInstance was never hit and the tool just created the instance of the tool.

To Reproduce
Steps to reproduce the behavior:
1.

Expected behavior
A clear and concise description of what you expected to happen.

Logs
If applicable, add logs to help explain your problem.

Additional context
We discussed the issue over email. Please feel free to contact me there if you need additional information. Thanks!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the reported AddSingleton(type, instance) and factory registrations in the SDK's service setup, using the supplied tool type and Activator.CreateInstance path. Compare whether the registered instance or factory is invoked when the tool is created; done means both registration forms are honored instead of silently creating another instance.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend-api-design
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.