Skip to content

Add unit test to check that AnyRequest handles missing parameters #24

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

Merged
merged 3 commits into from
Mar 19, 2025

Conversation

adamwulf
Copy link
Contributor

First part of the issue:

Here's a unit test that shows part of the issue. When a new message comes in, the JSON is first decoded into an AnyRequest in Server's input stream handler, and then is decoded to the concrete Request type in TypedRequestHandler.

This means that the AnyRequest type must also be able to handle missing params.

These screenshots show the issue. First screenshot shows the list tools request arrive with {"method":"tools/list","jsonrpc":"2.0","id":1} trying to decode into an AnyRequest

CleanShot 2025-03-19 at 00 13 43@2x

second screenshot shows the decoding fail because M.Parameters.self is Value not Empty

CleanShot 2025-03-19 at 00 17 34@2x

Second part of the issue:

I believe this wouldn't fix it entirely, as the parameters from the AnyRequest are re-encoded and decoded in callAsFunction of the TypedRequestHandler. This decodes into M.Parameters.self directly, so the NotRequired handling in Request.init(from decoder:) doesn't get called here, and instead the decoding would fail immediately.

@mattt
Copy link
Contributor

mattt commented Mar 19, 2025

@adamwulf This is incredibly helpful. Thanks so much for taking the time to investigate and document everything.

I found a minimal change that gets your failing test to pass. Check this out —

extension Value: NotRequired {
    public init() {
        self = .null
    }
}

@mattt
Copy link
Contributor

mattt commented Mar 19, 2025

Alright, I'm going to merge this and cut a new release (0.5.1) now. Hopefully this gets everything sorted for you 🤞

@mattt mattt merged commit da97a3d into modelcontextprotocol:main Mar 19, 2025
1 check passed
@adamwulf
Copy link
Contributor Author

phew - finally have time again to sit down and try this out - it works! 🎉 Huge thanks for your help and persistence on this 😄

@adamwulf adamwulf deleted the feature/params-unit-tests branch March 25, 2025 18:54
devyhan pushed a commit to devyhan/swift-sdk that referenced this pull request Apr 4, 2025
…delcontextprotocol#24)

* Add unit test to check that AnyRequest handles missing parameters

* Conform Value to NotRequired

* Add test for decoding AnyRequest with empty parameters

---------

Co-authored-by: Mattt Zmuda <mattt@loopwork.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants