-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexample-client.js
More file actions
152 lines (135 loc) · 5.3 KB
/
example-client.js
File metadata and controls
152 lines (135 loc) · 5.3 KB
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
#!/usr/bin/env node
/**
* Example script demonstrating how to use the face generator MCP server
* with the Model Context Protocol (MCP) and StdioTransport.
*
* This example shows:
* 1. How to connect to an MCP server using stdio transport
* 2. How to list available tools from the server
* 3. How to call a tool with parameters
* 4. How to handle the response from the tool
*
* The Model Context Protocol (MCP) is a standardized protocol for communication
* between clients and servers that provide tools or capabilities. It allows
* applications like Cline to interact with external tools in a consistent way.
*
* StdioTransport is one of the transport mechanisms supported by MCP, which uses
* standard input/output streams for communication between the client and server.
*/
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
import { spawn } from 'child_process';
import path from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';
async function main() {
// Get current directory
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Create output directory if it doesn't exist
const outputDir = path.join(__dirname, 'generated-faces');
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
console.log('Starting face generator MCP server...');
// Spawn the face generator server as a child process
// Adjust the path to the server executable as needed
const serverProcess = spawn('node', [path.join(__dirname, 'build', 'index.js')], {
stdio: ['pipe', 'pipe', 'inherit'] // stdin, stdout, stderr
});
// Create a transport that connects to the server via stdio
// StdioClientTransport handles the communication between the client and server
// using standard input/output streams. It can either:
// 1. Connect to an already running process (by passing stdin/stdout)
// 2. Launch a new process and connect to it (by passing command/args)
const transport = new StdioClientTransport({
command: 'node',
args: [path.join(__dirname, 'build', 'index.js')]
});
// Create an MCP client
// The client handles the high-level MCP protocol operations like:
// - Connecting to the server
// - Sending requests (list tools, call tool)
// - Receiving and parsing responses
const client = new Client({
name: 'face-generator-client',
version: '1.0.0'
});
try {
// Connect to the server
console.log('Connecting to server...');
await client.connect(transport);
console.log('Connected to server successfully!');
// List available tools
// This sends a "list_tools" request to the server to get information
// about what tools are available and their parameters
console.log('Listing available tools...');
const toolsResponse = await client.listTools();
console.log('Available tools:');
if (toolsResponse.tools && Array.isArray(toolsResponse.tools)) {
toolsResponse.tools.forEach(tool => {
console.log(`- ${tool.name}: ${tool.description}`);
});
}
// Call the generate_face tool
console.log('\nGenerating face images...');
// Example parameters for the generate_face tool
const params = {
outputDir: outputDir,
count: 2,
width: 256,
height: 256,
shape: 'circle',
returnImageContent: false
};
console.log('Parameters:', JSON.stringify(params, null, 2));
// Call the tool
// This sends a "call_tool" request to the server with the tool name
// and parameters. The server will execute the tool and return the result.
const result = await client.callTool({
name: 'generate_face',
arguments: params
});
// Handle the response
// The response from the server includes:
// - Whether the call was successful or resulted in an error
// - Content, which can be text, images, or other data types
if (result.isError) {
console.error('Error generating faces:',
result.content && Array.isArray(result.content) && result.content[0] && result.content[0].type === 'text'
? result.content[0].text
: 'Unknown error');
} else {
console.log('Success!');
// Display the result
if (result.content && Array.isArray(result.content)) {
result.content.forEach(content => {
if (content.type === 'text') {
console.log(content.text);
} else if (content.type === 'image') {
console.log(`Received image data (base64): ${content.data.substring(0, 20)}...`);
// You could save the image or display it in a UI here
}
});
}
}
} catch (error) {
console.error('Error:', error);
} finally {
// Clean up
console.log('\nClosing connection and terminating server...');
try {
// Different ways to disconnect based on the SDK version
if (typeof client.disconnect === 'function') {
await client.disconnect();
} else if (transport && typeof transport.close === 'function') {
await transport.close();
}
} catch (e) {
console.error('Error during disconnect:', e);
}
serverProcess.kill();
}
}
// Run the example
main().catch(console.error);