该项目提供了一个模型上下文协议(MCP)服务器,该服务器连接AI助手到ScreenshotOne.com API以捕获网站的截图。
Bearer <token>的"Authorization"头)# 捕获一个URL的截图
npm run dev:cli -- take-screenshot --url "https://example.com" --access-key "your-access-key"
# 使用自定义视口捕获截图
npm run dev:cli -- take-screenshot --url "https://example.com" --viewport-width 1920 --viewport-height 1080
# 捕获全页截图
npm run dev:cli -- take-screenshot --url "https://example.com" --full-page
# 将截图保存到文件
npm run dev:cli -- take-screenshot --url "https://example.com" --output screenshot.png
# 阻止广告和跟踪器
npm run dev:cli -- take-screenshot --url "https://example.com" --block-ads --block-trackers --block-cookie-banners
# ----------------------------------------------
# 将截图上传到Cloudflare
# 记得设置环境变量
# > 查看示例在".env.example"文件中
# ----------------------------------------------
# 捕获截图并上传到Cloudflare
npm run dev:cli -- take-screenshot --url https://example.com --upload
# 使用自定义文件名捕获截图
npm run dev:cli -- take-screenshot --url https://example.com --upload --upload-filename my-screenshot
# 启用上传调试捕获截图
npm run dev:cli -- take-screenshot --url https://example.com --upload --upload-debug
对于本地配置使用stdio传输:
{
"mcpServers": {
"screenshotone": {
"command": "node",
"args": ["/path/to/screenshotone-mcp-server/dist/index.js"],
"transportType": "stdio"
}
}
}
对于远程HTTP配置:
{
"mcpServers": {
"screenshotone": {
"type": "http",
"url": "http://localhost:8080/mcp"
}
}
}
HTTP传输的环境变量:
您可以使用这些环境变量来配置HTTP服务器:
MCP_HTTP_HOST:绑定的主机(默认:127.0.0.1)MCP_HTTP_PORT:监听的端口(默认:8080)MCP_HTTP_PATH:端点路径(默认:/mcp)模型上下文协议(MCP)是一个开放标准,允许AI系统安全且有上下文地连接到外部工具和数据源。
此样板实现了MCP规范,具有清晰分层的架构,可以扩展以构建任何API或数据源的自定义MCP服务器。
# 克隆仓库
git clone https://github.com/mrgoonie/screenshotone-mcp-server.git
cd screenshotone-mcp-server
# 安装依赖
npm install
启动带有热重载和MCP检查器的开发模式服务器(默认使用stdio传输):
npm run dev:server
或者使用流式HTTP传输:
npm run dev:server:http
这将启动MCP服务器,并在http://localhost:5173启用MCP检查器。
⚙️ 代理服务器正在监听端口6277 🔍 MCP检查器已在http://127.0.0.1:6274运行
当使用HTTP传输时,默认情况下服务器将在http://127.0.0.1:8080/mcp可用。
使用CLI进行截图:
# 基本截图
npm run dev:cli -- take-screenshot --url "https://example.com" --access-key "your-access-key"
# 高级选项
npm run dev:cli -- take-screenshot --url "https://example.com" --format png --viewport-width 1920 --viewport-height 1080 --full-page --output screenshot.png
此样板遵循一种干净的分层架构模式,分离关注点并促进可维护性。
src/
├── cli/ # 命令行接口
├── controllers/ # 业务逻辑
├── resources/ # MCP资源:从您的服务器向LLMs暴露数据和内容
├── services/ # 外部API交互
├── tools/ # MCP工具定义
├── types/ # 类型定义
├── utils/ # 共享工具
└── index.ts # 入口点
src/cli/*.cli.ts)<feature>.cli.ts<feature>.cli.test.tssrc/tools/*.tool.ts)<feature>.tool.ts,类型在<feature>.types.tssrc/controllers/*.controller.ts)<feature>.controller.tsControllerResponse对象src/services/*.service.ts)<feature>.service.tssrc/utils/*.util.ts)logger.util.ts:结构化日志记录error.util.ts:错误处理和标准化formatter.util.ts:Markdown格式化助手# 在开发模式下启动服务器(热重载及检查器)
npm run dev:server
# 在开发模式下运行CLI
npm run dev:cli -- [command] [args]
# 构建项目
npm run build
# 在生产模式下启动服务器
npm run start:server
# 在生产模式下运行CLI
npm run start:cli -- [command] [args]
# 运行所有测试
npm test
# 运行特定测试
npm test -- src/path/to/test.ts
# 生成测试覆盖率报告
npm run test:coverage
# 检查代码
npm run lint
# 使用Prettier格式化代码
npm run format
# 检查类型
npm run typecheck
按照以下步骤将您自己的工具添加到服务器:
在src/services/中创建一个新的服务以与您的外部API交互:
// src/services/example.service.ts
import { Logger } from '../utils/logger.util.js';
const logger = Logger.forContext('services/example.service.ts');
export async function getData(param: string): Promise<any> {
logger.debug('获取数据', { param });
// API交互代码在此处
return { result: '示例数据' };
}
在src/controllers/中添加一个控制器以处理业务逻辑:
// src/controllers/example.controller.ts
import { Logger } from '../utils/logger.util.js';
import * as exampleService from '../services/example.service.js';
import { formatMarkdown } from '../utils/formatter.util.js';
import { handleControllerError } from '../utils/error-handler.util.js';
import { ControllerResponse } from '../types/common.types.js';
const logger = Logger.forContext('controllers/example.controller.ts');
export interface GetDataOptions {
param?: string;
}
export async function getData(
options: GetDataOptions = {},
): Promise<ControllerResponse> {
try {
logger.debug('使用选项获取数据', options);
const data = await exampleService.getData(options.param || '默认值');
const content = formatMarkdown(data);
return { content };
} catch (error) {
throw handleControllerError(error, {
entityType: '示例数据',
操作: 'getData',
来源: 'controllers/example.controller.ts',
});
}
}
在src/tools/中创建一个工具定义:
// src/tools/example.tool.ts
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { z } from 'zod';
import { Logger } from '../utils/logger.util.js';
import { formatErrorForMcpTool } from '../utils/error.util.js';
import * as exampleController from '../controllers/example.controller.js';
const logger = Logger.forContext('tools/example.tool.ts');
const GetDataArgs = z.object({
param: z.string().optional().describe('可选参数'),
});
type GetDataArgsType = z.infer<typeof GetDataArgs>;
async function handleGetData(args: GetDataArgsType) {
try {
logger.debug('工具get_data被调用', args);
const result = await exampleController.getData({
param: args.param,
});
return {
content: [{ type: 'text' as const, text: result.content }],
};
} catch (error) {
logger.error('工具get_data失败', error);
return formatErrorForMcpTool(error);
}
}
export function register(server: McpServer) {
server.tool(
'get_data',
`从示例API获取数据,可选使用\`param\`。
使用此工具获取示例数据。返回格式化的Markdown数据.`,
GetDataArgs.shape,
handleGetData,
);
}
在src/cli/中创建一个CLI命令:
// src/cli/example.cli.ts
import { program } from 'commander';
import { Logger } from '../utils/logger.util.js';
import * as exampleController from '../controllers/example.controller.js';
import { handleCliError } from '../utils/error-handler.util.js';
const logger = Logger.forContext('cli/example.cli.ts');
program
.command('get-data')
.description('获取示例数据')
.option('--param <value>', '可选参数')
.action(async (options) => {
try {
logger.debug('CLI get-data被调用', options);
const result = await exampleController.getData({
param: options.param,
});
console.log(result.content);
} catch (error) {
handleCliError(error);
}
});
更新入口点以注册新的组件:
// 在src/cli/index.ts中
import '../cli/example.cli.js';
// 在src/index.ts(对于工具)
import exampleTool from './tools/example.tool.js';
// 然后在registerTools函数中:
exampleTool.register(server);
访问可视化的MCP检查器以测试您的工具并查看请求/响应详情:
npm run dev:server为开发启用调试日志:
# 设置环境变量
DEBUG=true npm run dev:server
# 或在~/.mcp/configs.json中配置
准备发布您的自定义MCP服务器时:
npm run buildnpm run start:servernpm publish{
"screenshotone": {
"environments": {
"DEBUG": "true",
"SCREENSHOTONE_ACCESS_KEY": "值"
}
}
}
注意:为了兼容性,如果未找到screenshotone键,服务器还将识别完整包名(screenshotone-mcp-server)或无范围包名(screenshotone-mcp-server)下的配置。然而,建议新配置使用短的screenshotone键。