使用说明
🔍 MCP关键字搜索服务器
一种模型上下文协议(MCP)服务器,支持在文本文件中进行智能关键字搜索,并进行详细的匹配分析。
📋 目录
🌟 概述
此MCP服务器提供强大的关键字搜索功能,允许客户端搜索文本文件并检索有关匹配的详细信息,包括:
- 行号 关键字出现的位置
- 精确位置 线内每场比赛
- 全线上下文 每场比赛
- 总比赛次数 在整个文件中
- 区分大小写和不区分大小写 搜索模式
该服务器使用官方的模型上下文协议SDK构建,完全符合MCP标准,可以轻松集成到任何兼容MCP的客户端应用程序中。
✨ 特性
| 特性 | 描述 |
|---|---|
| 🎯 精确匹配 | 返回每个关键字出现的精确字符位置 |
| 📊 详细分析 | 提供全面的匹配统计数据和上下文 |
| 🔤 灵活搜索 | 支持区分大小写和不区分大小写模式 |
| 🚀 高性能 | 高效处理大型文本文件 |
| 🛡️ 错误处理 | 强大的错误管理,错误信息清晰 |
| 🔌 符合MCP标准 | 内置官方MCP SDK,实现无缝集成 |
| 📝 元数据 | 在结果中返回完整的文件路径和搜索参数 |
📂 项目结构
mcp-keyword-search-server/
│
├── 📄 package.json # Dependencies and scripts
├── 📄 tsconfig.json # TypeScript configuration
├── 📄 README.md # This file
├── 📄 .gitignore # Git ignore rules
├── 📄 LICENSE # MIT License
│
├── 📁 src/ # Source code
│ ├── 📄 index.ts # Main MCP server
│ └── 📁 tools/
│ └── 📄 searchKeyword.ts # Search implementation
│
├── 📁 test-files/ # Test data
│ ├── 📄 sample1.txt # General test cases
│ ├── 📄 sample2.txt # Programming keywords
│ └── 📄 sample3.txt # Edge cases
│
├── 📁 build/ # Compiled output (auto-generated)
│ └── ...
│
└── 📁 screenshots/ # Documentation images
├── 🖼️ search_keyword_in_file.png
└── 🖼️ Task1_Salesforce_Record.png
🚀 安装
先决条件
- Node.js 18.x或更高
- npm 9.x或更高
设置步骤
-
克隆存储库
git clone https://github.com/YOUR-USERNAME/mcp-keyword-search-server.git cd mcp-keyword-search-server -
安装依赖项
npm install -
构建项目
npm run build -
验证安装
node build/index.js您应该看到:
Keyword Search MCP Server running on stdio
💻 用法
运行服务器
# Development mode (with auto-rebuild)
npm run watch
# Production mode
node build/index.js
与MCP客户端集成
将此配置添加到MCP客户端配置文件中:
{
"mcpServers": {
"keyword-search": {
"command": "node",
"args": ["/absolute/path/to/mcp-keyword-search-server/build/index.js"]
}
}
}
🧪 MCP检验员测试
步骤1:安装MCP检查器
npm install -g @modelcontextprotocol/inspector
第二步:启动检查器
npm run inspector
或手动:
npx @modelcontextprotocol/inspector node build/index.js
步骤3:配置连接
在MCP Inspector web界面中:
- 运输类型: 选择
STDIO - 命令: 进入
node - 论据: 进入
build/index.js - 点击 连接
步骤4:测试工具
- 导航到 工具 标签
- 选择
search_keyword从可用的工具 - 填写参数(见以下示例)
- 点击 运行工具
📖 工具文档
search_keyword
在指定文件中搜索关键字,并返回所有匹配项和详细信息。
输入参数
| 参数 | 类型 | 必填 | 默认 | 说明 |
|---|---|---|---|---|
filepath |
string | ✅ 是 | - | 文本文件的绝对或相对路径 |
keyword |
string | ✅ 是 | - | 要搜索的关键字 |
caseSensitive |
boolean | ❌ 没有 | false |
启用区分大小写的匹配 |
输出格式
{
filepath: string; // Absolute path to the searched file
keyword: string; // The search keyword used
totalMatches: number; // Total number of matches found
matches: Array<{
lineNumber: number; // Line number (1-indexed)
lineContent: string; // Full content of the line
matchPositions: number[]; // Character positions of matches
}>;
}
📚 例子
示例1:基本搜索(不区分大小写)
输入:
{
"filepath": "./test-files/sample1.txt",
"keyword": "example",
"caseSensitive": false
}
输出:
{
"filepath": "/absolute/path/to/test-files/sample1.txt",
"keyword": "example",
"totalMatches": 15,
"matches": [
{
"lineNumber": 1,
"lineContent": "This is an example text file for testing the MCP keyword search server.",
"matchPositions": [11]
},
{
"lineNumber": 2,
"lineContent": "The word example appears multiple times in this document.",
"matchPositions": [9]
},
{
"lineNumber": 5,
"lineContent": "Example with a capital E at the beginning.",
"matchPositions": [0]
}
]
}
示例2:区分大小写的搜索
输入:
{
"filepath": "./test-files/sample2.txt",
"keyword": "function",
"caseSensitive": true
}
输出:
{
"filepath": "/absolute/path/to/test-files/sample2.txt",
"keyword": "function",
"totalMatches": 8,
"matches": [
{
"lineNumber": 15,
"lineContent": "The word \"function\" appears in many programming contexts.",
"matchPositions": [10]
},
{
"lineNumber": 35,
"lineContent": "function searchKeyword(text, keyword) {",
"matchPositions": [0]
}
]
}
示例3:单行中的多个匹配
输入:
{
"filepath": "./test-files/sample1.txt",
"keyword": "test",
"caseSensitive": false
}
输出:
{
"filepath": "/absolute/path/to/test-files/sample1.txt",
"keyword": "test",
"totalMatches": 12,
"matches": [
{
"lineNumber": 11,
"lineContent": "Multiple matches on the same line: test test test",
"matchPositions": [35, 40, 45]
}
]
}
🖼️ 截图
任务2:MCP检查器-关键字搜索工具
使用MCP Inspector完成关键字搜索功能的演示。
这表明:
- ✅ 工具界面
search_keyword实施 - ✅ 输入参数:
filepath,keyword,caseSensitive - ✅ 成功执行,输出详细
- ✅ 匹配结果,包括行号和位置
- ✅ 总匹配次数:找到15次
- ✅ 每场比赛的完整行上下文
任务1:Salesforce记录集成
MCP服务器与Salesforce集成,用于记录管理。
这表明:
- ✅ Salesforce MCP服务器连接
- ✅ 记录检索和操作
- ✅ 成功集成API
- ✅ 数据同步功能
🛠️ 发展
可用脚本
# Build the project
npm run build
# Watch mode (auto-rebuild on changes)
npm run watch
# Run MCP Inspector
npm run inspector
# Clean build directory
rm -rf build/
添加新工具
要向服务器添加其他工具,请执行以下操作:
- 在中创建新文件
src/tools/ - 实现您的工具逻辑
- 在中注册该工具
src/index.ts - 更新
ListToolsRequestSchema处理器 - 在中添加新案例
CallToolRequestSchema处理器
项目架构
MCP Server Architecture
│
├── Server Layer (src/index.ts)
│ ├── Request Handler: ListToolsRequestSchema
│ ├── Request Handler: CallToolRequestSchema
│ └── Transport: StdioServerTransport
│
├── Tools Layer (src/tools/)
│ └── searchKeyword.ts
│ ├── File Reading
│ ├── Keyword Matching
│ └── Result Formatting
│
└── Communication Protocol
├── Input: JSON-formatted tool parameters
└── Output: Structured match results
🧪 测试
手动测试
- 创建测试文件 在
test-files/目录 - 运行服务器 使用
node build/index.js - 使用MCP检查器 测试工具
- 验证输出 匹配预期结果
包含测试用例
| 测试文件 | 目的 | 要测试的关键字 |
|---|---|---|
sample1.txt |
一般功能 | 示例、测试、MCP |
sample2.txt |
编程术语 | 函数、类、导入 |
sample3.txt |
边缘案例 | 测试,特殊字符 |
🤝 贡献
欢迎投稿!以下是您可以提供帮助的方式:
- 🍴 分叉存储库
- 🌿 创建要素分支(
git checkout -b feature/amazing-feature) - 💾 提交您的更改(
git commit -m 'Add some amazing feature') - 📤 推到分支(
git push origin feature/amazing-feature) - 🎉 打开拉取请求
贡献指南
- 遵循TypeScript的最佳实践
- 为新功能添加测试
- 根据需要更新文档
- 在提交PR之前,确保所有测试都通过
- 使用有意义的提交消息
📄 许可证
此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。
MIT License
Copyright (c) 2024 [Your Name]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction...
🙏 致谢
- 与 模型上下文协议SDK
- 受到MCP应用程序中对强大文件搜索功能需求的启发
- 感谢Anthropic团队开发MCP标准
- 特别感谢开源社区的持续支持
🎯 作业提交
该项目是作为MCP服务器开发任务的一部分创建的,具有以下交付成果:
✅ 已完成的任务
- [x] 任务1: Salesforce MCP集成(见上图)
- [x] 任务2: 关键字搜索工具实现
- [x] MCP检验员测试和验证
- [x] 完整的源代码和文档
- [x] 测试文件和示例
- [x] 演示功能的屏幕截图
📦 提交内容
- GitHub存储库: 包含所有文件的完整源代码
- 屏幕截图: 工作实施的视觉证明
- 文档: 带示例的全面自述文件
- 测试文件: 验证样本数据
⭐ 如果你觉得这个项目有用,请考虑给它一颗星! ⭐
由以下材料制成❤️ 用于MCP开发