|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// A client that connects to a server and exercises the resources API. |
| 6 | +import 'dart:async'; |
| 7 | +import 'dart:io'; |
| 8 | + |
| 9 | +import 'package:dart_mcp/client.dart'; |
| 10 | +import 'package:dart_mcp/stdio.dart'; |
| 11 | + |
| 12 | +void main() async { |
| 13 | + // Create a client, which is the top level object that manages all |
| 14 | + // server connections. |
| 15 | + final client = MCPClient( |
| 16 | + Implementation(name: 'example dart client', version: '0.1.0'), |
| 17 | + ); |
| 18 | + print('connecting to server'); |
| 19 | + |
| 20 | + // Start the server as a separate process. |
| 21 | + final process = await Process.start('dart', [ |
| 22 | + 'run', |
| 23 | + 'example/resources_server.dart', |
| 24 | + ]); |
| 25 | + // Connect the client to the server. |
| 26 | + final server = client.connectServer( |
| 27 | + stdioChannel(input: process.stdout, output: process.stdin), |
| 28 | + ); |
| 29 | + // When the server connection is closed, kill the process. |
| 30 | + unawaited(server.done.then((_) => process.kill())); |
| 31 | + print('server started'); |
| 32 | + |
| 33 | + // Initialize the server and let it know our capabilities. |
| 34 | + print('initializing server'); |
| 35 | + final initializeResult = await server.initialize( |
| 36 | + InitializeRequest( |
| 37 | + protocolVersion: ProtocolVersion.latestSupported, |
| 38 | + capabilities: client.capabilities, |
| 39 | + clientInfo: client.implementation, |
| 40 | + ), |
| 41 | + ); |
| 42 | + print('initialized: $initializeResult'); |
| 43 | + |
| 44 | + // Ensure the server supports the resources capability. |
| 45 | + if (initializeResult.capabilities.resources == null) { |
| 46 | + await server.shutdown(); |
| 47 | + throw StateError('Server doesn\'t support resources!'); |
| 48 | + } |
| 49 | + |
| 50 | + // Notify the server that we are initialized. |
| 51 | + server.notifyInitialized(); |
| 52 | + print('sent initialized notification'); |
| 53 | + |
| 54 | + // List all the available resources from the server. |
| 55 | + print('Listing resources from server'); |
| 56 | + final resourcesResult = await server.listResources(ListResourcesRequest()); |
| 57 | + for (final resource in resourcesResult.resources) { |
| 58 | + // For each resource, read its content. |
| 59 | + final content = (await server.readResource( |
| 60 | + ReadResourceRequest(uri: resource.uri), |
| 61 | + )).contents.map((part) => (part as TextResourceContents).text).join(''); |
| 62 | + print( |
| 63 | + 'Found resource: ${resource.name} with uri ${resource.uri} and contents: ' |
| 64 | + '"$content"', |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + // List all the available resource templates from the server. |
| 69 | + print('Listing resource templates from server'); |
| 70 | + final templatesResult = await server.listResourceTemplates( |
| 71 | + ListResourceTemplatesRequest(), |
| 72 | + ); |
| 73 | + for (final template in templatesResult.resourceTemplates) { |
| 74 | + print('Found resource template `${template.uriTemplate}`'); |
| 75 | + // For each template, fill in the path variable and read the resource. |
| 76 | + for (var path in ['zip', 'zap']) { |
| 77 | + final uri = template.uriTemplate.replaceFirst(RegExp('{.*}'), path); |
| 78 | + final contents = (await server.readResource( |
| 79 | + ReadResourceRequest(uri: uri), |
| 80 | + )).contents.map((part) => (part as TextResourceContents).text).join(''); |
| 81 | + print('Read resource `$uri`: "$contents"'); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // Shutdown the client, which will also shutdown the server connection. |
| 86 | + await client.shutdown(); |
| 87 | +} |
0 commit comments