Skip to content

Invalid MCP Server instantiation fails poorly #96

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

Open
domesticmouse opened this issue Apr 30, 2025 · 4 comments
Open

Invalid MCP Server instantiation fails poorly #96

domesticmouse opened this issue Apr 30, 2025 · 4 comments

Comments

@domesticmouse
Copy link
Member

domesticmouse commented Apr 30, 2025

I was attempting to run an MCP server incorrectly. It exited with an error message. This error message was not surfaced by the MCP Client sample code.

$ dart run bin/mcp_client.dart
connecting to server
server started
initializing server
Unhandled exception:
Bad state: The client closed with pending request "initialize".

The simplified code is as follows. For context, the mcp_server requires a command line argument that is not supplied here.

import 'package:dart_mcp/client.dart';

void main() async {
  final client = MCPClient(
    ClientImplementation(name: 'example dart client', version: '0.1.0'),
  );
  print('connecting to server');
  final server = await client.connectStdioServer('dart', [
    'run',
    '../mcp_server/bin/mcp_server.dart',
  ]);
  print('server started');

  print('initializing server');
  final initializeResult = await server.initialize(
    InitializeRequest(
      protocolVersion: protocolVersion,
      capabilities: client.capabilities,
      clientInfo: client.implementation,
    ),
  );
  print('initialized: $initializeResult');
  if (initializeResult.protocolVersion != protocolVersion) {
    throw StateError(
      'Protocol version mismatch, expected $protocolVersion, '
      'got ${initializeResult.protocolVersion}',
    );
  }

  await client.shutdown();
}

For context, attempting to run the server incorrectly failed as follows:

$ dart run ../mcp_server/bin/mcp_server.dart
Usage: dart mcp_server.dart <path to project>

Can we please expose this error text as part of the bad state exception?

@jakemac53
Copy link
Contributor

#4 is likely related to this as well

@jakemac53
Copy link
Contributor

In this case, the first message from the server is an invalid one (not JSON), I am a bit surprised we aren't seeing some other sort of error bubble up here.

@jakemac53
Copy link
Contributor

Ok, so json_rpc_2 automatically handles invalid messages by sending a notification message back to the server that it had sent an invalid request.

So, the client never sees this message and it sends an initialize request. The server then exits, and then we get the error about the connection closing with the pending initialize request.

We don't really have a good opportunity to see the actual bad output at all because it's intercepted by the json_rpc_2 package, we may want to add something to that package to allow you to see these.

I did recently resolve #4 which allows you to see all protocol traffic which should help generally resolve this, but you have to explicitly pass a sink on which to emit these messages.

jakemac53 added a commit that referenced this issue May 2, 2025
Related to #96

- Tests the current behavior, ensures we aren't leaking exceptions beyond what is expected.
- Also documents explicitly the behavior in initialize to encourage exception handling, and helping you to debug.
@domesticmouse
Copy link
Member Author

Silly question, what happens with stderr for commands? I'm wondering if I put out the usage instructions to stderr instead of stdout will that work better?

sigurdm pushed a commit to sigurdm/ai that referenced this issue May 6, 2025
Related to dart-lang#96

- Tests the current behavior, ensures we aren't leaking exceptions beyond what is expected.
- Also documents explicitly the behavior in initialize to encourage exception handling, and helping you to debug.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants