SEO数据接口服务器

DataForSEO MCP服务器实现,通过标准化接口使ChatGPT或其他代理能够与选定的DataForSEO API交互并获取SEO数据。

  • by moaiandin
  • TypeScript
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 moaiandin/mcp-dataforseo

使用说明

DataForSEO MCP服务器

DataForSEO的模型上下文协议(MCP)服务器实现,使ChatGPT或其他代理能够与选定的DataForSEO API交互,并通过标准化接口获取SEO数据。该服务器现在包括可选工具,可以满足OpenAI对搜索和文档检索的远程MCP要求。

特性

  • SERP API:谷歌、必应和雅虎的实时搜索引擎结果页面(SERP)数据;
  • KEYWORDS_DATA API:关键词研究和点击流数据,包括搜索量、每次点击成本和其他指标;
  • ONPAGE API:允许根据可定制的参数抓取网站和网页,以获得页面SEO性能指标;
  • DATAFORSEO_LABS API:基于DATAFORSEO内部数据库和专有算法的关键词、SERP和域数据。

先决条件

  • Node.js(v14或更高版本)
  • DataForSEO API凭据(API登录名和密码)

安装

  1. 克隆存储库:
git clone https://github.com/dataforseo/mcp-server-typescript
cd mcp-server-typescript
  1. 安装依赖项:
npm install
  1. 设置环境变量:
# Required
export DATAFORSEO_USERNAME=your_username
export DATAFORSEO_PASSWORD=your_password

# Optional: specify which modules to enable (comma-separated)
# If not set, all modules will be enabled
export ENABLED_MODULES="SERP,KEYWORDS_DATA,ONPAGE,DATAFORSEO_LABS,BACKLINKS,BUSINESS_DATA,DOMAIN_ANALYTICS,OPENAI"

# Optional: enable full API responses
# If not set or set to false, the server will filter and transform API responses to a more concise format
# If set to true, the server will return the full, unmodified API responses
export DATAFORSEO_FULL_RESPONSE="false"

作为NPM包安装

您可以全局安装该软件包:

npm install -g dataforseo-mcp-server

或者直接运行它而不安装:

npx dataforseo-mcp-server

请记住在运行命令之前设置环境变量:

# Required environment variables
export DATAFORSEO_USERNAME=your_username
export DATAFORSEO_PASSWORD=your_password

# Run with npx
npx dataforseo-mcp-server

建立和运行

构建项目:

npm run build

运行服务器:

# Start local server (direct MCP communication)
npx dataforseo-mcp-server

# Start HTTP server
npx dataforseo-mcp-server http

HTTP服务器配置

默认情况下,服务器在端口3000上运行,支持基本身份验证和基于环境变量的身份验证。

要启动HTTP服务器,请运行:

npm run http

身份验证方法

  1. 基本身份验证

    • 使用Basic Auth标头发送请求:
    Authorization: Basic <base64-encoded-credentials>
    
    • 凭据格式: username:password
  2. 环境变量

    • 如果没有提供基本身份验证,服务器将使用环境变量中的凭据:
    export DATAFORSEO_USERNAME=your_username
    export DATAFORSEO_PASSWORD=your_password
    

可用模块

以下模块可供启用/禁用:

  • SERP:谷歌、必应和雅虎的实时SERP数据;
  • KEYWORDS_DATA:关键词研究和点击流数据;
  • ONPAGE:抓取网站和网页,以获取页面SEO性能指标;
  • DATAFORSEO_LABS:基于DataForSEO数据库和算法的关键字、SERP和域名数据;
  • BACKLINKS:任何域、子域或网页的入站链接、引用域和引用页面的数据;
  • BUSINESS_DATA:基于以下平台上公开共享的商业评论和商业信息:谷歌、Trustpilot、Tripadvisor;
  • DOMAIN_ANALYTICS:帮助识别用于构建网站的所有可能技术,并提供Whois数据;
  • OPENAI:提供 search 和 fetch 与ChatGPT连接器兼容的工具;

添加新工具/模块

模块结构

每个模块对应一个特定的DataForSEO API:

实施方案

您可以:

  1. 向现有模块添加新工具
  2. 创建一个全新的模块

添加新工具

以下是如何将新工具添加到任何新的或预先存在的模块中:

// src/modules/your-module/tools/your-tool.tool.ts
import { BaseTool } from '../../base.tool';
import { DataForSEOClient } from '../../../client/dataforseo.client';
import { z } from 'zod';

export class YourTool extends BaseTool {
  constructor(private client: DataForSEOClient) {
    super(client);
    // DataForSEO API returns extensive data with many fields, which can be overwhelming
    // for AI agents to process. We select only the most relevant fields to ensure
    // efficient and focused responses.
    this.fields = [
      'title',           // Example: Include the title field
      'description',     // Example: Include the description field
      'url',            // Example: Include the URL field
      // Add more fields as needed
    ];
  }

  getName() {
    return 'your-tool-name';
  }

  getDescription() {
    return 'Description of what your tool does';
  }

  getParams(): z.ZodRawShape {
    return {
      // Required parameters
      keyword: z.string().describe('The keyword to search for'),
      location: z.string().describe('Location in format "City,Region,Country" or just "Country"'),
      
      // Optional parameters
      fields: z.array(z.string()).optional().describe('Specific fields to return in the response. If not specified, all fields will be returned'),
      language: z.string().optional().describe('Language code (e.g., "en")'),
    };
  }

  async handle(params: any) {
    try {
      // Make the API call
      const response = await this.client.makeRequest({
        endpoint: '/v3/dataforseo_endpoint_path',
        method: 'POST',
        body: [{
          // Your request parameters
          keyword: params.keyword,
          location: params.location,
          language: params.language,
        }],
      });

      // Validate the response for errors
      this.validateResponse(response);

      //if the main data array is specified in tasks[0].result[:] field
      const result = this.handleDirectResult(response);
      //if main data array specified in tasks[0].result[0].items field
      const result = this.handleItemsResult(response);
      // Format and return the response
      return this.formatResponse(result);
    } catch (error) {
      // Handle and format any errors
      return this.formatErrorResponse(error);
    }
  }
}

创建新模块

  1. 在下创建新目录 src/modules/ 对于您的模块:
mkdir -p src/modules/your-module-name
  1. 创建模块文件:
// src/modules/your-module-name/your-module-name.module.ts
import { BaseModule } from '../base.module';
import { DataForSEOClient } from '../../client/dataforseo.client';
import { YourTool } from './tools/your-tool.tool';

export class YourModuleNameModule extends BaseModule {
  constructor(private client: DataForSEOClient) {
    super();
  }

  getTools() {
    return {
      'your-tool-name': new YourTool(this.client),
    };
  }
}
  1. 在中注册您的模块 src/config/modules.config.ts:
export const AVAILABLE_MODULES = [
  'SERP',
  'KEYWORDS_DATA',
  'ONPAGE',
  'DATAFORSEO_LABS',
  'YOUR_MODULE_NAME',
  'OPENAI'  // Built-in module providing search and fetch for ChatGPT
] as const;
  1. 在中初始化您的模块 src/index.ts:
if (isModuleEnabled('YOUR_MODULE_NAME', enabledModules)) {
  modules.push(new YourModuleNameModule(dataForSEOClient));
}

您希望我们接下来支持哪些端点/API?

我们一直在寻求扩展此MCP服务器的功能。如果您希望看到支持的特定DataForSEO端点或API,请:

  1. 检查 DataForSEO API文档 看看有什么可用的
  2. 在我们的GitHub存储库中打开一个问题:
    • 您希望看到支持的API/端点;
    • 用例的简要描述;
    • 描述您希望实现的任何具体功能。

您的反馈有助于我们确定下一步支持哪些API的优先级!

与ChatGPT一起使用

这 OPENAI 模块提供 search 工具和a fetch 遵循OpenAI的工具 远程MCP服务器要求通过包含以下内容来启用该模块 OPENAI 在 ENABLED_MODULES 环境变量或保持该变量未设置以启用所有模块。

搜索工具接受 query string,并允许可选参数来细化请求:

  • source – serp 用于实时搜索引擎结果(默认)或 labs 使用DataForSEO Labs关键字创意
  • search_engine –其中之一 google, bing,或 yahoo (在以下情况下使用 source=serp)
  • language_code 语言代码,例如 en
  • location_name –完整位置名称(默认 United States)
  • depth –从SERP获取的结果数量(当 source=serp)
  • limit –要返回的关键字创意数量(何时返回) source=labs)

它返回一个结果列表,其中包含 id, title, text,以及 url.使用 fetch 返回的工具 id 在ChatGPT中检索完整的引用记录。

资源