本页面为机器翻译。英文版本为原文,可能更准确或更及时。 查看英文版

MCP 服务器 —— 开发者集成

本指南介绍与 finlight MCP 服务器的编程集成。用它来构建自定义 AI 应用、自动化新闻检索,或将金融新闻集成到您现有的系统中。


INFO端点

服务器信息

环境URL
生产环境https://mcp.finlight.me

该服务器通过基于 HTTP 的 JSON-RPC 2.0 实现 MCP(模型上下文协议)


AUTHAPI 密钥

认证

所有工具调用都需要通过 Bearer 令牌进行认证。您的 finlight API 密钥即作为访问令牌。

Authorization: Bearer YOUR_API_KEY

对于自动化的 OAuth 流程(如 Claude Desktop),该服务器实现了带 PKCE 的 OAuth 2.0。详情参见 OAuth 流程


QUICKSTART示例

快速开始

使用 cURL

列出可用工具:

curl -X POST https://mcp.finlight.me \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "tools/list"
  }'

搜索文章:

curl -X POST https://mcp.finlight.me \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "tools/call",
    "params": {
      "name": "search_articles",
      "arguments": {
        "query": "NVIDIA earnings",
        "tickers": ["NVDA"],
        "pageSize": 10
      }
    }
  }'

使用 TypeScript

import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";

const transport = new StreamableHTTPClientTransport(
  new URL("https://mcp.finlight.me"),
  {
    requestInit: {
      headers: {
        Authorization: `Bearer ${process.env.FINLIGHT_API_KEY}`,
      },
    },
  }
);

const client = new Client({
  name: "my-app",
  version: "1.0.0",
});

await client.connect(transport);

// List available tools
const tools = await client.listTools();
console.log("Available tools:", tools);

// Search articles
const result = await client.callTool({
  name: "search_articles",
  arguments: {
    query: "Federal Reserve interest rates",
    from: "2024-01-01",
    pageSize: 5,
  },
});

console.log("Articles:", result);

使用 Python

import requests
import json

API_KEY = "your-api-key"
MCP_URL = "https://mcp.finlight.me"

def mcp_request(method: str, params: dict = None):
    response = requests.post(
        MCP_URL,
        headers={
            "Content-Type": "application/json",
            "Authorization": f"Bearer {API_KEY}",
        },
        json={
            "jsonrpc": "2.0",
            "id": 1,
            "method": method,
            "params": params or {},
        },
    )
    return response.json()

# List available tools
tools = mcp_request("tools/list")
print("Available tools:", json.dumps(tools, indent=2))

# Search articles
articles = mcp_request("tools/call", {
    "name": "search_articles",
    "arguments": {
        "query": "Tesla Model 3",
        "tickers": ["TSLA"],
        "from": "2024-01-01",
        "pageSize": 10,
    },
})
print("Articles:", json.dumps(articles, indent=2))

TOOLS参考

可用工具

search_articles

通过高级过滤和情绪分析搜索金融新闻文章。

参数:

参数类型说明
querystring带布尔运算符(AND、OR、NOT)和字段过滤器(ticker:AAPLsource:reuters.com)的搜索查询
tickersstring[]按股票代码过滤(例如 ["AAPL", "NVDA"]
sourcesstring[]限制为特定的新闻来源域名
optInSourcesstring[]将来源添加到默认集合(而非替换)
excludeSourcesstring[]从结果中排除特定来源
countriesstring[]按 ISO 3166-1 alpha-2 国家/地区代码过滤
fromstring起始日期(YYYY-MM-DD 或 ISO 8601)
tostring结束日期(YYYY-MM-DD 或 ISO 8601)
languagestring语言代码(默认:"en")
includeContentboolean包含完整文章正文(需要订阅)
includeEntitiesboolean包含标记的公司实体
excludeEmptyContentboolean跳过无内容的文章
order"ASC" | "DESC"排序方向(默认:DESC = 最新优先)
orderBy"publishDate" | "createdAt" | "revisedDate"排序字段 —— revisedDate 按最近修订日期排序
pageSizenumber每页结果数(1-100,默认:20)
pagenumber页码(默认:1)

示例请求:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "search_articles",
    "arguments": {
      "query": "earnings report",
      "tickers": ["AAPL", "MSFT"],
      "from": "2024-01-01",
      "to": "2024-03-31",
      "language": "en",
      "pageSize": 20
    }
  }
}

示例响应:

{
  "jsonrpc": "2.0",
  "result": {
    "content": [{
      "type": "text",
      "text": "{\"articles\":[{\"title\":\"Apple Reports Record Q1...\",\"summary\":\"...\",\"link\":\"https://...\",\"publishDate\":\"2024-02-01T16:30:00Z\",\"source\":\"www.reuters.com\",\"sentiment\":\"positive\",\"sentimentConfidence\":0.92}],\"totalResults\":847,\"page\":1,\"pageSize\":20}"
    }]
  },
  "id": 1
}

get_article_by_link

通过 URL 检索特定文章。

参数:

参数类型必填说明
linkstring文章的完整 URL
includeContentboolean包含完整文章正文
includeEntitiesboolean包含标记的公司实体

示例请求:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "get_article_by_link",
    "arguments": {
      "link": "https://www.reuters.com/technology/example-article",
      "includeContent": true,
      "includeEntities": true
    }
  }
}

list_sources

获取所有可用的新闻来源及其元数据。

参数:

示例请求:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "list_sources",
    "arguments": {}
  }
}

响应包含:

  • domain - 来源网站域名
  • isDefaultSource - 是否包含在默认搜索中
  • isContentAvailable - 是否支持完整内容检索

PROTOCOLJSON-RPC

MCP 协议方法

该服务器实现了以下 MCP 协议方法:

方法说明
initialize初始化 MCP 会话
notifications/initialized确认初始化完成
tools/list列出可用工具及其 schema
tools/call使用参数执行工具
ping健康检查

Initialize 示例:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "initialize",
  "params": {
    "protocolVersion": "2024-11-05",
    "capabilities": {},
    "clientInfo": {
      "name": "my-client",
      "version": "1.0.0"
    }
  }
}

响应:

{
  "jsonrpc": "2.0",
  "result": {
    "protocolVersion": "2024-11-05",
    "capabilities": {
      "tools": {}
    },
    "serverInfo": {
      "name": "finlight-news-api",
      "version": "1.0.0"
    }
  },
  "id": 1
}

OAUTH认证

OAuth 流程

对于支持 OAuth 的客户端(如 Claude Desktop),该服务器实现了带 PKCE 的 OAuth 2.0。

端点

端点方法说明
/.well-known/oauth-protected-resourceGET受保护资源元数据
/.well-known/oauth-authorization-serverGET授权服务器元数据
/registerPOST动态客户端注册
/authorizeGET授权页面
/authorize/submitPOST处理授权
/tokenPOST用授权码换取令牌

流程概览

1. Client discovers OAuth endpoints via .well-known
2. Client optionally registers via /register
3. Client redirects user to /authorize with PKCE challenge
4. User enters API key and submits
5. Server redirects back with authorization code
6. Client exchanges code + verifier for access token
7. Client uses access token as Bearer token for API calls

安全特性

  • 要求 PKCE - 防止授权码被拦截
  • 自加密授权码 - 采用 AES-256-GCM 加密,无状态
  • 短时效授权码 - 120 秒过期
  • 重定向 URI 验证 - 授权码与原始 URI 绑定

ERRORS故障排除

错误处理

JSON-RPC 错误码

代码消息说明
-32700Parse error无效的 JSON
-32600Invalid Request缺少 jsonrpc 或 method
-32601Method not found未知的方法
-32602Invalid params无效的工具参数
-32001Unauthorized缺失或无效的 API 密钥
-32603Internal error服务器端错误

API 错误响应

失败的工具调用会返回一个错误结构:

{
  "jsonrpc": "2.0",
  "result": {
    "isError": true,
    "content": [{
      "type": "text",
      "text": "Error: Invalid API key"
    }]
  },
  "id": 1
}

常见的 HTTP 级别错误:

状态说明
401无效的 API 密钥
403订阅不允许此操作
429超出速率限制
400验证错误

LIMITS配额

速率限制

速率限制取决于您的 finlight 订阅套餐。当超出限制时,API 会返回 429 状态码。

app.finlight.me 查看您当前的用量和限制。


SUPPORT帮助

支持