test-client.js•6.54 kB
#!/usr/bin/env node
/**
* Simple test client for the Google Slides MCP Server
* This script demonstrates how to interact with the MCP server
*/
const { spawn } = require('child_process');
const readline = require('readline');
class MCPTestClient {
constructor() {
this.serverProcess = null;
this.messageId = 1;
}
async startServer() {
console.log('Starting MCP server...');
// Start the MCP server process
this.serverProcess = spawn('node', ['dist/index.js'], {
stdio: ['pipe', 'pipe', 'pipe'],
cwd: process.cwd()
});
this.serverProcess.stderr.on('data', (data) => {
console.error('Server error:', data.toString());
});
this.serverProcess.on('close', (code) => {
console.log(`Server process exited with code ${code}`);
});
// Send initialize request
await this.sendRequest({
jsonrpc: '2.0',
id: this.messageId++,
method: 'initialize',
params: {
protocolVersion: '2024-11-05',
capabilities: {
tools: {}
},
clientInfo: {
name: 'test-client',
version: '1.0.0'
}
}
});
console.log('MCP server started successfully!');
}
async sendRequest(request) {
return new Promise((resolve, reject) => {
const requestStr = JSON.stringify(request) + '\n';
// Set up response handler
const onData = (data) => {
try {
const response = JSON.parse(data.toString().trim());
this.serverProcess.stdout.off('data', onData);
resolve(response);
} catch (error) {
reject(error);
}
};
this.serverProcess.stdout.on('data', onData);
this.serverProcess.stdin.write(requestStr);
});
}
async listTools() {
console.log('\n=== Listing available tools ===');
const response = await this.sendRequest({
jsonrpc: '2.0',
id: this.messageId++,
method: 'tools/list'
});
console.log('Available tools:');
response.result.tools.forEach((tool) => {
console.log(`- ${tool.name}: ${tool.description}`);
});
return response.result.tools;
}
async getAuthUrl() {
console.log('\n=== Getting authentication URL ===');
const response = await this.sendRequest({
jsonrpc: '2.0',
id: this.messageId++,
method: 'tools/call',
params: {
name: 'get_auth_url',
arguments: {}
}
});
console.log('Response:', response.result.content[0].text);
return response;
}
async authenticate(code) {
console.log('\n=== Authenticating with code ===');
const response = await this.sendRequest({
jsonrpc: '2.0',
id: this.messageId++,
method: 'tools/call',
params: {
name: 'authenticate',
arguments: { code }
}
});
console.log('Response:', response.result.content[0].text);
return response;
}
async createSlide(presentationId) {
console.log('\n=== Creating new slide ===');
const response = await this.sendRequest({
jsonrpc: '2.0',
id: this.messageId++,
method: 'tools/call',
params: {
name: 'create_slide',
arguments: { presentationId }
}
});
console.log('Response:', response.result.content[0].text);
return response;
}
async addRectangle(presentationId, slideId) {
console.log('\n=== Adding rectangle to slide ===');
const response = await this.sendRequest({
jsonrpc: '2.0',
id: this.messageId++,
method: 'tools/call',
params: {
name: 'add_rectangle',
arguments: { presentationId, slideId }
}
});
console.log('Response:', response.result.content[0].text);
return response;
}
async getPresentationInfo(presentationId) {
console.log('\n=== Getting presentation info ===');
const response = await this.sendRequest({
jsonrpc: '2.0',
id: this.messageId++,
method: 'tools/call',
params: {
name: 'get_presentation_info',
arguments: { presentationId }
}
});
console.log('Response:', response.result.content[0].text);
return response;
}
async listSlides(presentationId) {
console.log('\n=== Listing slides ===');
const response = await this.sendRequest({
jsonrpc: '2.0',
id: this.messageId++,
method: 'tools/call',
params: {
name: 'list_slides',
arguments: { presentationId }
}
});
console.log('Response:', response.result.content[0].text);
return response;
}
async stop() {
if (this.serverProcess) {
this.serverProcess.kill();
}
}
}
// Interactive testing function
async function runInteractiveTest() {
const client = new MCPTestClient();
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function question(prompt) {
return new Promise((resolve) => {
rl.question(prompt, resolve);
});
}
try {
await client.startServer();
await client.listTools();
console.log('\n' + '='.repeat(50));
console.log('MCP Google Slides Server Test Client');
console.log('='.repeat(50));
// Get auth URL
await client.getAuthUrl();
const authCode = await question('\nEnter the authorization code from the OAuth flow: ');
if (authCode.trim()) {
await client.authenticate(authCode.trim());
}
const presentationId = await question('\nEnter your Google Slides presentation ID: ');
if (presentationId.trim()) {
// Test getting presentation info
await client.getPresentationInfo(presentationId.trim());
// Test listing slides
await client.listSlides(presentationId.trim());
// Test creating a new slide
const createResponse = await client.createSlide(presentationId.trim());
// Extract slide ID from response (this is a simple extraction, you might need to parse JSON)
const slideIdMatch = createResponse.result.content[0].text.match(/slide_\d+/);
if (slideIdMatch) {
const slideId = slideIdMatch[0];
console.log(`\nExtracted slide ID: ${slideId}`);
// Test adding rectangle
await client.addRectangle(presentationId.trim(), slideId);
}
}
} catch (error) {
console.error('Test error:', error);
} finally {
await client.stop();
rl.close();
}
}
// Run the test
if (require.main === module) {
runInteractiveTest().catch(console.error);
}