Skip to content

Invalid MCP Server instantiation fails poorly #96

Open
@domesticmouse

Description

@domesticmouse

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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions