Skip to content

Add error checks for accessing Request members #184

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 12 commits into from
Jun 24, 2025

Conversation

gspencergoog
Copy link
Contributor

Description

This adds error checking and throws ArgumentError for Request subclasses which have required fields. This is because in shared.dart, in registerRequestHandler, it casts the argument's value to a Map<String, Object?>, but it doesn't do any argument checking because creating those request objects don't go through the constructors, they are just cast:

    return impl((p.value as Map?)?.cast<String, Object?>() as T);

So I added error checking to all of the accessors for required members of Request objects, so that when they are accessed they will throw.

I ran across this when a client tried to send a malformed initialization request that didn't include capabilities, and the stack trace it sent was less than helpful. This will at least send clearer errors.

Tests

  • Added a test to check that these all throw. Not sure how useful that is, really, but it at least keeps them from being removed accidentally.

Copy link

github-actions bot commented Jun 23, 2025

PR Health

Changelog Entry ✔️
Package Changed Files

Changes to files need to be accounted for in their respective changelogs.

Comment on lines 31 to 35
final ref = _value['ref'];
if (ref == null) {
throw ArgumentError('Missing ref field in $CompleteRequest.');
}
return ref as Reference;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
final ref = _value['ref'];
if (ref == null) {
throw ArgumentError('Missing ref field in $CompleteRequest.');
}
return ref as Reference;
final ref = _value['ref'] as Reference?;
if (ref == null) {
throw ArgumentError('Missing ref field in $CompleteRequest.');
}
return ref;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@@ -6,6 +6,8 @@
- Add `clientInfo` field to `MCPServer`, assigned during initialization.
- Move the `done` future from the `ServerConnection` into `MCPBase` so it is
available to the `MPCServer` class as well.
- Added error checking to required fields of all `Request` subclasses so that
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh actually this needs to move up into the next version 0.2.3-wip and the pubspec probably needs to be bumped as well.

Copy link
Contributor

@jakemac53 jakemac53 left a comment

Choose a reason for hiding this comment

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

LGTM once the pubspec/changelog get updated to 0.2.3-wip

@gspencergoog gspencergoog merged commit 8e3365f into dart-lang:main Jun 24, 2025
19 checks passed
copybara-service bot pushed a commit to dart-lang/sdk that referenced this pull request Jun 26, 2025
Revisions updated by `dart tools/rev_sdk_deps.dart`.

ai (https://github.com/dart-lang/ai/compare/706d224..d0b89cf):
  d0b89cf  2025-06-25  Greg Spencer  Add enum support to `Schema` (dart-lang/ai#190)
  f219861  2025-06-26  Brett Morgan  Update `simple_server.dart` (dart-lang/ai#187)
  8e3365f  2025-06-24  Greg Spencer  Add error checks for accessing Request members (dart-lang/ai#184)

protobuf (https://github.com/dart-lang/protobuf/compare/0bfff0c..bce362d):
  bce362d  2025-06-24  Devon Carew  prep to publish 22.4.0 (google/protobuf.dart#1023)
  269e004  2025-06-24  Devon Carew  improve the readability of generated gRPC client files (google/protobuf.dart#1021)
  48ba649  2025-06-24  Devon Carew  update the header for generated files (google/protobuf.dart#1022)

webdev (https://github.com/dart-lang/webdev/compare/6dc3dde..7f376d2):
  7f376d24  2025-06-25  Derek Xu  Set package versions to WIP versions (dart-lang/webdev#2637)

Change-Id: Ib720f5127f3fddeb82b62cf13f24c3ddb4038e33
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/437260
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Auto-Submit: Devon Carew <devoncarew@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants