YouTube分析MCP服务器
用于YouTube Analytics数据访问的模型上下文协议(MCP)服务器,具有人口统计和发现工具,采用可扩展的配置驱动架构构建。
核心功能
- 渠道分析:获取全面的渠道概述、增长模式和生命体征
- 视频表演:分析单个视频指标、观众保留率和下降点
- 观众人口统计:获取年龄/性别细分和地理分布数据
- 发现见解:了解流量来源和搜索词驱动视图
- 参与度指标:跟踪点赞、评论、分享和观众互动模式
- 观众保留率:确定观众放弃内容优化的确切时刻
- 性能比较:比较不同时间段之间的指标
- 公共渠道分析:研究竞争对手渠道和趋势内容
核心架构原则
此MCP服务器遵循 配置驱动架构 它提供:
- 可维护性:明确区分工具定义和实施
- 可扩展性:无需修改核心服务器逻辑即可轻松添加新工具
- 一致性:标准化的错误处理和响应格式
- 可读性:作为文档的干净、声明性配置
设置
1.Google API证书
若要使用此YouTube Analytics MCP服务器,您需要设置Google API凭据:
- 转到 谷歌云控制台
- 创建新项目或选择现有项目
- 启用YouTube Analytics API和YouTube Data API v3
- 转到“凭据”并创建新的OAuth 2.0客户端ID
- 下载JSON格式的凭据
- 将文件另存为
credentials.json在src/auth/目录
隐私声明:所有数据处理都在您的计算机上本地进行。您的凭据和分析数据永远不会离开您的机器——服务器完全在本地运行,并从您的系统直接连接到谷歌的API。
2.发展
# Install dependencies
npm install
# Build the project
npm run build
# Run in development mode
npm run dev
# Inspect with MCP Inspector
npm run inspect架构概述
项目结构
src/
├── index.ts # Main server entry point (config-driven)
├── tool-configs.ts # Central tool configuration aggregator
├── types.ts # TypeScript interfaces and types
├── auth/
│ ├── tool-configs.ts # Authentication tool configurations
│ └── ...
├── server/
│ ├── info-configs.ts # Server info tool configurations
│ └── ...
└── youtube/tools/
├── channel-configs.ts # Channel analysis tool configurations
├── health-configs.ts # Channel health tool configurations
├── audience-configs.ts # Audience demographics tool configurations
├── discovery-configs.ts # Traffic source tool configurations
├── performance-configs.ts # Performance analysis tool configurations
└── engagement-configs.ts # Engagement metrics tool configurations刀具配置结构
每个工具都由一个配置对象定义:
interface ToolConfig {
name: string; // Tool name
description: string; // Tool description
schema: any; // Zod schema for validation
handler: (params: T, context: ToolContext) => Promise;
category?: string; // Optional grouping
}可用工具
身份验证工具
check_auth_status-检查YouTube身份验证状态revoke_auth-撤销身份验证并清除令牌
渠道工具
get_channel_info-获取基本频道信息get_channel_videos-获取带有过滤器的频道视频列表
健康工具
get_channel_overview-获取渠道生命体征和生长模式get_comparison_metrics-比较时间段之间的指标get_average_view_percentage-获取平均视图百分比
受众工具
get_video_demographics-获取年龄/性别细分get_geographic_distribution-获取观众的地理分布get_subscriber_analytics-获取订阅者与非订阅者分析
发现工具
get_traffic_sources-获取流量来源分析get_search_terms-获取搜索词以获取SEO见解
性能工具
get_audience_retention-追踪观众留存模式get_retention_dropoff_points-查找确切的下车时刻
参与工具
get_engagement_metrics-分析点赞、评论和分享
添加新工具
要添加新工具,只需创建一个配置对象并将其添加到相应的配置文件中:
// In src/youtube/tools/new-category-configs.ts
export const newCategoryToolConfigs: ToolConfig[] = [
{
name: "new_tool_name",
description: "Description of what the tool does",
category: "new_category",
schema: z.object({
// Define your parameters here
param1: z.string().describe("Description of parameter 1"),
param2: z.number().optional().describe("Optional parameter 2"),
}),
handler: async ({ param1, param2 }, { getYouTubeClient }: ToolContext) => {
try {
const youtubeClient = await getYouTubeClient();
// Your tool implementation here
return {
content: [{
type: "text",
text: "Tool result here"
}]
};
} catch (error) {
return {
content: [{
type: "text",
text: `Error: ${error instanceof Error ? error.message : String(error)}`
}],
isError: true
};
}
},
},
];
// Then add to src/tool-configs.ts
export const allToolConfigs = [
// ... existing configs
...newCategoryToolConfigs,
];配置驱动架构的好处
- 清洁分离:工具定义与服务器设置是分开的
- 类型安全:对模式和处理程序的完全TypeScript支持
- 文档配置文件作为实时文档
- 测试:更容易对单个工具进行单元测试
- 可扩展性:添加新工具类别很简单
- 可维护性:所有工具的一致模式
- 可扩展性:易于管理许多工具,而不会弄乱主文件
服务器注册模式
服务器会自动从配置中注册所有工具:
// Automatic registration from configs - no manual server.tool() calls needed
allToolConfigs.forEach((toolConfig) => {
server.tool(
toolConfig.name, // Tool name from config
toolConfig.description, // Description from config
toolConfig.schema, // Zod schema from config
async (params: any) => { // Handler wrapper
return toolConfig.handler(params, {
authManager,
getYouTubeClient,
clearYouTubeClientCache
});
}
);
});错误处理
所有工具都遵循一致的错误处理模式:
try {
// Tool implementation
return {
content: [{ type: "text", text: "Success result" }]
};
} catch (error) {
return {
content: [{
type: "text",
text: `Error: ${error instanceof Error ? error.message : String(error)}`
}],
isError: true
};
}上下文注入
工具接收具有共享依赖关系的上下文对象:
interface ToolContext {
authManager: AuthManager;
getYouTubeClient: () => Promise;
clearYouTubeClientCache: () => void;
}这种架构使代码库更易于维护、扩展和扩展,同时保留了所有现有功能。
