File size: 15,217 Bytes
a5d718a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | #!/usr/bin/env node
import { createServer } from 'node:http';
import { createInterface } from 'node:readline';
import { URL } from 'node:url';
import { Client } from '../../client/index.js';
import { StreamableHTTPClientTransport } from '../../client/streamableHttp.js';
import { CallToolResultSchema, ListToolsResultSchema } from '../../types.js';
import { UnauthorizedError } from '../../client/auth.js';
import { InMemoryOAuthClientProvider } from './simpleOAuthClientProvider.js';
// Configuration
const DEFAULT_SERVER_URL = 'http://localhost:3000/mcp';
const CALLBACK_PORT = 8090; // Use different port than auth server (3001)
const CALLBACK_URL = `http://localhost:${CALLBACK_PORT}/callback`;
/**
* Interactive MCP client with OAuth authentication
* Demonstrates the complete OAuth flow with browser-based authorization
*/
class InteractiveOAuthClient {
constructor(serverUrl, clientMetadataUrl) {
this.serverUrl = serverUrl;
this.clientMetadataUrl = clientMetadataUrl;
this.client = null;
this.rl = createInterface({
input: process.stdin,
output: process.stdout
});
}
/**
* Prompts user for input via readline
*/
async question(query) {
return new Promise(resolve => {
this.rl.question(query, resolve);
});
}
/**
* Example OAuth callback handler - in production, use a more robust approach
* for handling callbacks and storing tokens
*/
/**
* Starts a temporary HTTP server to receive the OAuth callback
*/
async waitForOAuthCallback() {
return new Promise((resolve, reject) => {
const server = createServer((req, res) => {
// Ignore favicon requests
if (req.url === '/favicon.ico') {
res.writeHead(404);
res.end();
return;
}
console.log(`π₯ Received callback: ${req.url}`);
const parsedUrl = new URL(req.url || '', 'http://localhost');
const code = parsedUrl.searchParams.get('code');
const error = parsedUrl.searchParams.get('error');
if (code) {
console.log(`β
Authorization code received: ${code?.substring(0, 10)}...`);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(`
<html>
<body>
<h1>Authorization Successful!</h1>
<p>You can close this window and return to the terminal.</p>
<script>setTimeout(() => window.close(), 2000);</script>
</body>
</html>
`);
resolve(code);
setTimeout(() => server.close(), 3000);
}
else if (error) {
console.log(`β Authorization error: ${error}`);
res.writeHead(400, { 'Content-Type': 'text/html' });
res.end(`
<html>
<body>
<h1>Authorization Failed</h1>
<p>Error: ${error}</p>
</body>
</html>
`);
reject(new Error(`OAuth authorization failed: ${error}`));
}
else {
console.log(`β No authorization code or error in callback`);
res.writeHead(400);
res.end('Bad request');
reject(new Error('No authorization code provided'));
}
});
server.listen(CALLBACK_PORT, () => {
console.log(`OAuth callback server started on http://localhost:${CALLBACK_PORT}`);
});
});
}
async attemptConnection(oauthProvider) {
console.log('π’ Creating transport with OAuth provider...');
const baseUrl = new URL(this.serverUrl);
const transport = new StreamableHTTPClientTransport(baseUrl, {
authProvider: oauthProvider
});
console.log('π’ Transport created');
try {
console.log('π Attempting connection (this will trigger OAuth redirect)...');
await this.client.connect(transport);
console.log('β
Connected successfully');
}
catch (error) {
if (error instanceof UnauthorizedError) {
console.log('π OAuth required - waiting for authorization...');
const callbackPromise = this.waitForOAuthCallback();
const authCode = await callbackPromise;
await transport.finishAuth(authCode);
console.log('π Authorization code received:', authCode);
console.log('π Reconnecting with authenticated transport...');
await this.attemptConnection(oauthProvider);
}
else {
console.error('β Connection failed with non-auth error:', error);
throw error;
}
}
}
/**
* Establishes connection to the MCP server with OAuth authentication
*/
async connect() {
console.log(`π Attempting to connect to ${this.serverUrl}...`);
const clientMetadata = {
client_name: 'Simple OAuth MCP Client',
redirect_uris: [CALLBACK_URL],
grant_types: ['authorization_code', 'refresh_token'],
response_types: ['code'],
token_endpoint_auth_method: 'client_secret_post'
};
console.log('π Creating OAuth provider...');
const oauthProvider = new InMemoryOAuthClientProvider(CALLBACK_URL, clientMetadata, (redirectUrl) => {
console.log(`\nπ Please open this URL in your browser to authorize:\n ${redirectUrl.toString()}`);
}, this.clientMetadataUrl);
console.log('π OAuth provider created');
console.log('π€ Creating MCP client...');
this.client = new Client({
name: 'simple-oauth-client',
version: '1.0.0'
}, { capabilities: {} });
console.log('π€ Client created');
console.log('π Starting OAuth flow...');
await this.attemptConnection(oauthProvider);
// Start interactive loop
await this.interactiveLoop();
}
/**
* Main interactive loop for user commands
*/
async interactiveLoop() {
console.log('\nπ― Interactive MCP Client with OAuth');
console.log('Commands:');
console.log(' list - List available tools');
console.log(' call <tool_name> [args] - Call a tool');
console.log(' stream <tool_name> [args] - Call a tool with streaming (shows task status)');
console.log(' quit - Exit the client');
console.log();
while (true) {
try {
const command = await this.question('mcp> ');
if (!command.trim()) {
continue;
}
if (command === 'quit') {
console.log('\nπ Goodbye!');
this.close();
process.exit(0);
}
else if (command === 'list') {
await this.listTools();
}
else if (command.startsWith('call ')) {
await this.handleCallTool(command);
}
else if (command.startsWith('stream ')) {
await this.handleStreamTool(command);
}
else {
console.log("β Unknown command. Try 'list', 'call <tool_name>', 'stream <tool_name>', or 'quit'");
}
}
catch (error) {
if (error instanceof Error && error.message === 'SIGINT') {
console.log('\n\nπ Goodbye!');
break;
}
console.error('β Error:', error);
}
}
}
async listTools() {
if (!this.client) {
console.log('β Not connected to server');
return;
}
try {
const request = {
method: 'tools/list',
params: {}
};
const result = await this.client.request(request, ListToolsResultSchema);
if (result.tools && result.tools.length > 0) {
console.log('\nπ Available tools:');
result.tools.forEach((tool, index) => {
console.log(`${index + 1}. ${tool.name}`);
if (tool.description) {
console.log(` Description: ${tool.description}`);
}
console.log();
});
}
else {
console.log('No tools available');
}
}
catch (error) {
console.error('β Failed to list tools:', error);
}
}
async handleCallTool(command) {
const parts = command.split(/\s+/);
const toolName = parts[1];
if (!toolName) {
console.log('β Please specify a tool name');
return;
}
// Parse arguments (simple JSON-like format)
let toolArgs = {};
if (parts.length > 2) {
const argsString = parts.slice(2).join(' ');
try {
toolArgs = JSON.parse(argsString);
}
catch {
console.log('β Invalid arguments format (expected JSON)');
return;
}
}
await this.callTool(toolName, toolArgs);
}
async callTool(toolName, toolArgs) {
if (!this.client) {
console.log('β Not connected to server');
return;
}
try {
const request = {
method: 'tools/call',
params: {
name: toolName,
arguments: toolArgs
}
};
const result = await this.client.request(request, CallToolResultSchema);
console.log(`\nπ§ Tool '${toolName}' result:`);
if (result.content) {
result.content.forEach(content => {
if (content.type === 'text') {
console.log(content.text);
}
else {
console.log(content);
}
});
}
else {
console.log(result);
}
}
catch (error) {
console.error(`β Failed to call tool '${toolName}':`, error);
}
}
async handleStreamTool(command) {
const parts = command.split(/\s+/);
const toolName = parts[1];
if (!toolName) {
console.log('β Please specify a tool name');
return;
}
// Parse arguments (simple JSON-like format)
let toolArgs = {};
if (parts.length > 2) {
const argsString = parts.slice(2).join(' ');
try {
toolArgs = JSON.parse(argsString);
}
catch {
console.log('β Invalid arguments format (expected JSON)');
return;
}
}
await this.streamTool(toolName, toolArgs);
}
async streamTool(toolName, toolArgs) {
if (!this.client) {
console.log('β Not connected to server');
return;
}
try {
// Using the experimental tasks API - WARNING: may change without notice
console.log(`\nπ§ Streaming tool '${toolName}'...`);
const stream = this.client.experimental.tasks.callToolStream({
name: toolName,
arguments: toolArgs
}, CallToolResultSchema, {
task: {
taskId: `task-${Date.now()}`,
ttl: 60000
}
});
// Iterate through all messages yielded by the generator
for await (const message of stream) {
switch (message.type) {
case 'taskCreated':
console.log(`β Task created: ${message.task.taskId}`);
break;
case 'taskStatus':
console.log(`β³ Status: ${message.task.status}`);
if (message.task.statusMessage) {
console.log(` ${message.task.statusMessage}`);
}
break;
case 'result':
console.log('β Completed!');
message.result.content.forEach(content => {
if (content.type === 'text') {
console.log(content.text);
}
else {
console.log(content);
}
});
break;
case 'error':
console.log('β Error:');
console.log(` ${message.error.message}`);
break;
}
}
}
catch (error) {
console.error(`β Failed to stream tool '${toolName}':`, error);
}
}
close() {
this.rl.close();
if (this.client) {
// Note: Client doesn't have a close method in the current implementation
// This would typically close the transport connection
}
}
}
/**
* Main entry point
*/
async function main() {
const args = process.argv.slice(2);
const serverUrl = args[0] || DEFAULT_SERVER_URL;
const clientMetadataUrl = args[1];
console.log('π Simple MCP OAuth Client');
console.log(`Connecting to: ${serverUrl}`);
if (clientMetadataUrl) {
console.log(`Client Metadata URL: ${clientMetadataUrl}`);
}
console.log();
const client = new InteractiveOAuthClient(serverUrl, clientMetadataUrl);
// Handle graceful shutdown
process.on('SIGINT', () => {
console.log('\n\nπ Goodbye!');
client.close();
process.exit(0);
});
try {
await client.connect();
}
catch (error) {
console.error('Failed to start client:', error);
process.exit(1);
}
finally {
client.close();
}
}
// Run if this file is executed directly
main().catch(error => {
console.error('Unhandled error:', error);
process.exit(1);
});
//# sourceMappingURL=simpleOAuthClient.js.map |