Skip to content

Add helper functions to distinguish between jsonRPC message types #281

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

Conversation

fredericbarthelet
Copy link
Contributor

Added simple type guard functions isJSONRPCRequest, isJSONRPCResponse and isJSONRPCNotification relying on existing schemas

Motivation and Context

This is to avoid "key" in message conditions in the codebase and ensure condition are actually handling properly formatted message. This helper functions are also quite handy to avoid having to implement custom type guards in custom transport librairies - i.e. https://github.com/fredericbarthelet/middy-mcp/blob/df6e932d60aef4fad9ef436d1466057f773f1fb6/src/HttpServerTransport.ts#L4-L11

How Has This Been Tested?

There is no dedicated test file for types.ts but existing tests in protocol.test.ts are used to make sure assertion are the same.

Breaking Changes

No breaking changes

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

@jmorrell-cloudflare
Copy link

+1 for these. I've been using the Schema.safeParse for this, but helper functions would be a lot nicer and discoverable

Copy link
Contributor

@ihrpr ihrpr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. Looks good!

@ihrpr
Copy link
Contributor

ihrpr commented Apr 9, 2025

@fredericbarthelet, looks like tests are failing, please can you take a look?

@fredericbarthelet fredericbarthelet force-pushed the add-json-rpc-helper-methods branch from 2a9ee34 to 4e749db Compare April 9, 2025 22:06
@fredericbarthelet
Copy link
Contributor Author

@fredericbarthelet, looks like tests are failing, please can you take a look?

Sorry about that, indeed missing a condition on RPC error response. Now fixed. All tests passing locally :)

@fredericbarthelet fredericbarthelet requested a review from ihrpr April 10, 2025 11:31
@ihrpr ihrpr merged commit c6668c1 into modelcontextprotocol:main Apr 10, 2025
2 checks passed
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.

3 participants