This example demonstrates how to use the face generator MCP server with the Model Context Protocol (MCP) and StdioTransport. It shows how to create a client application that connects to the MCP server, lists available tools, and calls the generate_face tool to generate human face images.
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. This makes it easy to integrate with command-line tools and processes.
Before running this example, make sure you have:
- Node.js installed (version 14 or higher)
- The face generator MCP server built (run
npm run buildin the project root)
-
Make sure you've built the face generator server:
npm run build
-
Run the example client:
node example-client.js
The example client:
- Spawns the face generator server as a child process
- Connects to the server using StdioTransport
- Lists available tools provided by the server
- Calls the
generate_facetool with parameters to generate face images - Handles the response, which may include text or image data
- Cleans up by disconnecting from the server and terminating the process
The example demonstrates several key concepts:
const transport = new StdioClientTransport({
command: 'node',
args: [path.join(__dirname, 'build', 'index.js')]
});This creates a transport that can communicate with the server over standard input/output. The transport can either connect to an already running process or launch a new one.
const client = new Client({
name: 'face-generator-client',
version: '1.0.0'
});The client handles high-level MCP protocol operations like connecting to the server, sending requests, and receiving responses.
const toolsResponse = await client.listTools();This sends a "list_tools" request to the server to get information about available tools and their parameters.
const result = await client.callTool({
name: 'generate_face',
arguments: params
});This sends a "call_tool" request to the server with the tool name and parameters. The server executes the tool and returns the result.
if (result.isError) {
// Handle error
} else {
// Process successful result
result.content.forEach(content => {
if (content.type === 'text') {
console.log(content.text);
} else if (content.type === 'image') {
// Handle image data
}
});
}The response includes whether the call was successful and the content, which can be text, images, or other data types.
You can modify the example to:
- Change the parameters for the
generate_facetool - Save generated images to a different directory
- Process the returned image data in different ways
- Add error handling for specific scenarios
This example can be adapted to work with other MCP servers by:
- Changing the server path in the transport configuration
- Updating the tool name and parameters to match what the server provides
- Adjusting the response handling based on the expected content types
For more information about the Model Context Protocol, visit: