Skip to content

Conversation

zhongyi51
Copy link
Contributor

Closes: #75

The Problem

As identified in issue #75, the individual *Request structs (e.g., PingRequest, ListResourcesRequest) do not currently validate that their method field matches the constant value defined in the JSON schema. This creates ambiguity. For example, a JSON payload like:

{
  "method": "wrong/method",
  "params": { /* ... valid params for ListResourcesRequest ... */ }
}

could be incorrectly deserialized as a ListResourcesRequest. The deserialization process should fail fast if the method name is incorrect for the target struct.

The Solution

This pull request proposes a robust pattern to validate the method field by leveraging Serde's #[serde(deserialize_with = "...")]:

  1. Deserializes the incoming value as a string.
  2. Compares it against the expected constant value for that specific request type.

Note for Maintainers

⚠️ This pull request should NOT be merged directly. It serves as a working example for the code generator.

@zhongyi51 zhongyi51 changed the title Support validation for subtypes of *Request enums (ServerRequest as an example) Support validation for method field in *Request Jul 19, 2025
@zhongyi51 zhongyi51 changed the title Support validation for method field in *Request Support validation for method fields in *Request Jul 19, 2025
@hashemix
Copy link
Member

Update: It has been a long time and my focus was on other priority features of the SDK. I started adding these changes into schema generator and will update this PR soon 🎉

@hashemix
Copy link
Member

hashemix commented Oct 2, 2025

I’m about to merge this. I’ve added it to the schema generator, so all static strings like method are validated consistently as you recommended.
Once merged, I’ll generate a new set of schemas to ensure a more type-safe schema.

@hashemix hashemix changed the title Support validation for method fields in *Request feat: support validation for method fields in *Request Oct 2, 2025
@hashemix hashemix merged commit 83ad262 into rust-mcp-stack:main Oct 2, 2025
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