-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest-http-client.js
More file actions
executable file
·83 lines (75 loc) · 1.91 KB
/
test-http-client.js
File metadata and controls
executable file
·83 lines (75 loc) · 1.91 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
#!/usr/bin/env node
/**
* Test HTTP Client for WordPress MCP Server
*
* This script tests the HTTP transport mode of the WordPress MCP Server.
*/
import fetch from 'node-fetch';
import minimist from 'minimist';
// Parse command line arguments
const argv = minimist(process.argv.slice(2), {
string: ['url', 'token', 'tool'],
default: {
url: 'http://localhost:3000/mcp',
token: 'default-token',
tool: 'ping'
}
});
/**
* Call an MCP tool via HTTP
* @param {string} url - The MCP endpoint URL
* @param {string} token - The authentication token
* @param {string} tool - The tool to call
* @param {Object} params - The tool parameters
* @returns {Promise<Object>} The tool response
*/
async function callMcpTool(url, token, tool, params = {}) {
console.log(`Calling tool: ${tool}`);
console.log(`URL: ${url}`);
console.log('Params:', params);
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({
jsonrpc: '2.0',
id: '1',
method: 'call_tool',
params: {
name: tool,
arguments: params
}
})
});
if (!response.ok) {
throw new Error(`HTTP error: ${response.status} ${response.statusText}`);
}
const result = await response.json();
return result;
} catch (error) {
console.error('Error calling MCP tool:', error);
throw error;
}
}
// Main function
async function main() {
try {
// Call the specified tool
const result = await callMcpTool(
argv.url,
argv.token,
argv.tool,
argv.params ? JSON.parse(argv.params) : {}
);
console.log('\nResult:');
console.log(JSON.stringify(result, null, 2));
} catch (error) {
console.error('Error:', error);
process.exit(1);
}
}
// Run the main function
main();