dataforseo-mcp-server
Version:
A Model Context Protocol (MCP) server for the DataForSEO API, enabling modular and extensible integration of DataForSEO endpoints with support for both HTTP and SSE transports.
66 lines (65 loc) • 2.13 kB
JavaScript
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
class MCPClient {
mcp;
transport = null;
constructor() {
// Initialize Anthropic client and MCP client
this.mcp = new Client({ name: "mcp-client-cli", version: "1.0.0" });
}
async connectToServer(serverScriptPath) {
/**
* Connect to an MCP server
*
* @param serverScriptPath - Path to the server script (.py or .js)
*/
try {
// Determine script type and appropriate command
const isJs = serverScriptPath.endsWith(".js");
const isPy = serverScriptPath.endsWith(".py");
if (!isJs && !isPy) {
throw new Error("Server script must be a .js or .py file");
}
const command = isPy
? process.platform === "win32"
? "python"
: "python3"
: process.execPath;
// Initialize transport and connect to server
this.transport = new StdioClientTransport({
command,
args: [serverScriptPath],
});
this.mcp.connect(this.transport);
// List available tools
const toolsResult = await this.mcp.listTools();
console.log(toolsResult);
console.log("Connected to server with tools:", toolsResult.tools.map(({ name }) => name));
}
catch (e) {
console.log("Failed to connect to MCP server: ", e);
throw e;
}
}
async cleanup() {
/**
* Clean up resources
*/
await this.mcp.close();
}
}
async function main() {
if (process.argv.length < 3) {
console.log("Usage: node build/test.js <path_to_server_script>");
return;
}
const mcpClient = new MCPClient();
try {
await mcpClient.connectToServer(process.argv[2]);
}
finally {
await mcpClient.cleanup();
process.exit(0);
}
}
main();