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
通过高级过滤和情绪分析搜索金融新闻文章。
参数:
| 参数 | 类型 | 说明 |
|---|---|---|
query | string | 带布尔运算符(AND、OR、NOT)和字段过滤器(ticker:AAPL、source:reuters.com)的搜索查询 |
tickers | string[] | 按股票代码过滤(例如 ["AAPL", "NVDA"]) |
sources | string[] | 限制为特定的新闻来源域名 |
optInSources | string[] | 将来源添加到默认集合(而非替换) |
excludeSources | string[] | 从结果中排除特定来源 |
countries | string[] | 按 ISO 3166-1 alpha-2 国家/地区代码过滤 |
from | string | 起始日期(YYYY-MM-DD 或 ISO 8601) |
to | string | 结束日期(YYYY-MM-DD 或 ISO 8601) |
language | string | 语言代码(默认:"en") |
includeContent | boolean | 包含完整文章正文(需要订阅) |
includeEntities | boolean | 包含标记的公司实体 |
excludeEmptyContent | boolean | 跳过无内容的文章 |
order | "ASC" | "DESC" | 排序方向(默认:DESC = 最新优先) |
orderBy | "publishDate" | "createdAt" | "revisedDate" | 排序字段 —— revisedDate 按最近修订日期排序 |
pageSize | number | 每页结果数(1-100,默认:20) |
page | number | 页码(默认: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 检索特定文章。
参数:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
link | string | 是 | 文章的完整 URL |
includeContent | boolean | 否 | 包含完整文章正文 |
includeEntities | boolean | 否 | 包含标记的公司实体 |
示例请求:
{
"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-resource | GET | 受保护资源元数据 |
/.well-known/oauth-authorization-server | GET | 授权服务器元数据 |
/register | POST | 动态客户端注册 |
/authorize | GET | 授权页面 |
/authorize/submit | POST | 处理授权 |
/token | POST | 用授权码换取令牌 |
流程概览
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 错误码
| 代码 | 消息 | 说明 |
|---|---|---|
| -32700 | Parse error | 无效的 JSON |
| -32600 | Invalid Request | 缺少 jsonrpc 或 method |
| -32601 | Method not found | 未知的方法 |
| -32602 | Invalid params | 无效的工具参数 |
| -32001 | Unauthorized | 缺失或无效的 API 密钥 |
| -32603 | Internal 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帮助
支持
- 文档:docs.finlight.me
- 控制台:app.finlight.me
- 支持:联系我们