|
| 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 interacts with a server that provides prompts and completions |
| 6 | +/// for the prompt arguments. |
| 7 | +library; |
| 8 | + |
| 9 | +import 'dart:async'; |
| 10 | +import 'dart:io'; |
| 11 | + |
| 12 | +import 'package:dart_mcp/client.dart'; |
| 13 | +import 'package:dart_mcp/stdio.dart'; |
| 14 | + |
| 15 | +void main() async { |
| 16 | + try { |
| 17 | + // Change the stdin mode so we can handle bytes one at a time, to intercept |
| 18 | + // the tab key for auto complete. |
| 19 | + stdin.echoMode = false; |
| 20 | + stdin.lineMode = false; |
| 21 | + |
| 22 | + // Create the client, which is the top level object that manages all |
| 23 | + // server connections. |
| 24 | + final client = MCPClient( |
| 25 | + Implementation(name: 'example dart client', version: '0.1.0'), |
| 26 | + ); |
| 27 | + print('connecting to server'); |
| 28 | + |
| 29 | + // Start the server as a separate process. |
| 30 | + final process = await Process.start('dart', [ |
| 31 | + 'run', |
| 32 | + 'example/prompts_and_completions_server.dart', |
| 33 | + ]); |
| 34 | + // Connect the client to the server. |
| 35 | + final server = client.connectServer( |
| 36 | + stdioChannel(input: process.stdout, output: process.stdin), |
| 37 | + ); |
| 38 | + // When the server connection is closed, kill the process. |
| 39 | + unawaited(server.done.then((_) => process.kill())); |
| 40 | + print('server started'); |
| 41 | + |
| 42 | + // Initialize the server and let it know our capabilities. |
| 43 | + print('initializing server'); |
| 44 | + final initializeResult = await server.initialize( |
| 45 | + InitializeRequest( |
| 46 | + protocolVersion: ProtocolVersion.latestSupported, |
| 47 | + capabilities: client.capabilities, |
| 48 | + clientInfo: client.implementation, |
| 49 | + ), |
| 50 | + ); |
| 51 | + print('initialized: $initializeResult'); |
| 52 | + |
| 53 | + // Ensure the server supports the prompts capability. |
| 54 | + if (initializeResult.capabilities.prompts == null) { |
| 55 | + await server.shutdown(); |
| 56 | + throw StateError('Server doesn\'t support prompts!'); |
| 57 | + } |
| 58 | + |
| 59 | + // Ensure the server supports the completions capability. |
| 60 | + if (initializeResult.capabilities.completions == null) { |
| 61 | + await server.shutdown(); |
| 62 | + throw StateError('Server doesn\'t support completions!'); |
| 63 | + } |
| 64 | + |
| 65 | + // Notify the server that we are initialized. |
| 66 | + server.notifyInitialized(); |
| 67 | + print('sent initialized notification'); |
| 68 | + |
| 69 | + // List all the available prompts from the server. |
| 70 | + print('Listing prompts from server'); |
| 71 | + final promptsResult = await server.listPrompts(ListPromptsRequest()); |
| 72 | + |
| 73 | + // Iterate each prompt and have the user fill in the arguments. |
| 74 | + for (final prompt in promptsResult.prompts) { |
| 75 | + print( |
| 76 | + 'Found prompt ${prompt.name}, fill in the following arguments using ' |
| 77 | + 'tab to complete them:', |
| 78 | + ); |
| 79 | + // For each argument, get a value from the user. |
| 80 | + final arguments = <String, Object?>{}; |
| 81 | + for (var argument in prompt.arguments!) { |
| 82 | + stdout.write('${argument.name}: '); |
| 83 | + // The current user query. |
| 84 | + var current = ''; |
| 85 | + // Read characters until we get an enter key. |
| 86 | + while (true) { |
| 87 | + final next = stdin.readByteSync(); |
| 88 | + // User pressed tab, lets do an auto complete |
| 89 | + if (next == 9) { |
| 90 | + final completeResult = await server.requestCompletions( |
| 91 | + CompleteRequest( |
| 92 | + // The ref is the current prompt name. |
| 93 | + ref: PromptReference(name: prompt.name), |
| 94 | + argument: CompletionArgument( |
| 95 | + name: argument.name, |
| 96 | + value: current, |
| 97 | + ), |
| 98 | + ), |
| 99 | + ); |
| 100 | + // Just auto-fill the first completion for this example |
| 101 | + if (completeResult.completion.values.isNotEmpty) { |
| 102 | + final firstResult = completeResult.completion.values.first; |
| 103 | + stdout.write(firstResult.substring(current.length)); |
| 104 | + current = firstResult; |
| 105 | + } |
| 106 | + // If there are no completions, just do nothing. |
| 107 | + } else if (next == 10) { |
| 108 | + // Enter key, assign the argument and break the loop. |
| 109 | + arguments[argument.name] = current; |
| 110 | + stdout.writeln(''); |
| 111 | + break; |
| 112 | + } else if (next == 127) { |
| 113 | + // Backspace keypress. |
| 114 | + if (current.isNotEmpty) { |
| 115 | + // Write a backspace followed by a space and then another |
| 116 | + // backspace to clear one character. |
| 117 | + stdout.write('\b \b'); |
| 118 | + // Trim current by one. |
| 119 | + current = current.substring(0, current.length - 1); |
| 120 | + } |
| 121 | + } else { |
| 122 | + // A regular character, just add it to current and print it to the |
| 123 | + // console. |
| 124 | + final character = String.fromCharCode(next); |
| 125 | + current += character; |
| 126 | + stdout.write(character); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + // Now fetch the full prompt with the arguments filled in. |
| 132 | + final promptResult = await server.getPrompt( |
| 133 | + GetPromptRequest(name: prompt.name, arguments: arguments), |
| 134 | + ); |
| 135 | + final promptText = promptResult.messages |
| 136 | + .map((m) => (m.content as TextContent).text) |
| 137 | + .join(''); |
| 138 | + |
| 139 | + // Finally, print the prompt to the user. |
| 140 | + print('Got full prompt `${prompt.name}`: "$promptText"'); |
| 141 | + } |
| 142 | + |
| 143 | + // Shutdown the client, which will also shutdown the server connection. |
| 144 | + await client.shutdown(); |
| 145 | + } finally { |
| 146 | + // Reset the terminal modes. |
| 147 | + stdin.echoMode = true; |
| 148 | + stdin.lineMode = true; |
| 149 | + } |
| 150 | +} |
0 commit comments