micro-app-mcp 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- micro_app_mcp-0.1.0/LICENSE +21 -0
- micro_app_mcp-0.1.0/PKG-INFO +258 -0
- micro_app_mcp-0.1.0/README.md +232 -0
- micro_app_mcp-0.1.0/pyproject.toml +46 -0
- micro_app_mcp-0.1.0/setup.cfg +4 -0
- micro_app_mcp-0.1.0/src/micro_app_mcp/__init__.py +3 -0
- micro_app_mcp-0.1.0/src/micro_app_mcp/app/__init__.py +1 -0
- micro_app_mcp-0.1.0/src/micro_app_mcp/app/server.py +109 -0
- micro_app_mcp-0.1.0/src/micro_app_mcp/app/tools.py +186 -0
- micro_app_mcp-0.1.0/src/micro_app_mcp/config.py +54 -0
- micro_app_mcp-0.1.0/src/micro_app_mcp/knowledge/__init__.py +1 -0
- micro_app_mcp-0.1.0/src/micro_app_mcp/knowledge/base.py +34 -0
- micro_app_mcp-0.1.0/src/micro_app_mcp/knowledge/docs_loader.py +38 -0
- micro_app_mcp-0.1.0/src/micro_app_mcp/knowledge/github_loader.py +66 -0
- micro_app_mcp-0.1.0/src/micro_app_mcp/knowledge/text_splitter.py +55 -0
- micro_app_mcp-0.1.0/src/micro_app_mcp/knowledge/vectorizer.py +128 -0
- micro_app_mcp-0.1.0/src/micro_app_mcp/main.py +12 -0
- micro_app_mcp-0.1.0/src/micro_app_mcp/storage/__init__.py +1 -0
- micro_app_mcp-0.1.0/src/micro_app_mcp/storage/metadata.py +128 -0
- micro_app_mcp-0.1.0/src/micro_app_mcp/storage/vector_store.py +64 -0
- micro_app_mcp-0.1.0/src/micro_app_mcp/utils/__init__.py +1 -0
- micro_app_mcp-0.1.0/src/micro_app_mcp/utils/logger.py +37 -0
- micro_app_mcp-0.1.0/src/micro_app_mcp.egg-info/PKG-INFO +258 -0
- micro_app_mcp-0.1.0/src/micro_app_mcp.egg-info/SOURCES.txt +29 -0
- micro_app_mcp-0.1.0/src/micro_app_mcp.egg-info/dependency_links.txt +1 -0
- micro_app_mcp-0.1.0/src/micro_app_mcp.egg-info/entry_points.txt +2 -0
- micro_app_mcp-0.1.0/src/micro_app_mcp.egg-info/requires.txt +18 -0
- micro_app_mcp-0.1.0/src/micro_app_mcp.egg-info/top_level.txt +1 -0
- micro_app_mcp-0.1.0/tests/__init__.py +1 -0
- micro_app_mcp-0.1.0/tests/test_knowledge.py +58 -0
- micro_app_mcp-0.1.0/tests/test_tools.py +23 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sam Xu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: micro-app-mcp
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: MCP Server for micro-app knowledge base
|
|
5
|
+
Requires-Python: >=3.12
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Dist: fastmcp<2.0,>=0.1.0
|
|
9
|
+
Requires-Dist: fastapi>=0.109.0
|
|
10
|
+
Requires-Dist: langchain>=0.2.0
|
|
11
|
+
Requires-Dist: langchain-community>=0.2.0
|
|
12
|
+
Requires-Dist: chromadb>=0.4.0
|
|
13
|
+
Requires-Dist: playwright>=1.40.0
|
|
14
|
+
Requires-Dist: huggingface-hub>=0.20.0
|
|
15
|
+
Requires-Dist: sentence-transformers>=2.0.0
|
|
16
|
+
Requires-Dist: uvicorn>=0.27.0
|
|
17
|
+
Requires-Dist: pydantic>=2.5.0
|
|
18
|
+
Requires-Dist: python-dotenv>=1.0.0
|
|
19
|
+
Requires-Dist: PyGithub>=1.58.0
|
|
20
|
+
Requires-Dist: mcp>=1.26.0
|
|
21
|
+
Requires-Dist: html2text>=2020.1.16
|
|
22
|
+
Provides-Extra: dev
|
|
23
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
24
|
+
Requires-Dist: pytest-asyncio>=0.23.0; extra == "dev"
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# Micro-App MCP Server
|
|
28
|
+
|
|
29
|
+
MCP Server for micro-app knowledge base
|
|
30
|
+
|
|
31
|
+
## 项目简介
|
|
32
|
+
|
|
33
|
+
该项目是一个专门用于查询 micro-app 微前端框架知识库的 MCP Server。它整合了 GitHub 源码和在线文档内容,支持语义检索。当用户输入 `/micro` 时,LLM 会先检索知识库再回答问题。
|
|
34
|
+
|
|
35
|
+
## 核心功能
|
|
36
|
+
|
|
37
|
+
- **数据采集**: 自动从 GitHub 获取源码,从在线文档站点获取文档内容
|
|
38
|
+
- **知识向量化**: 将采集的文本内容进行分块、向量化存储
|
|
39
|
+
- **语义检索**: 提供基于向量相似度的语义检索能力
|
|
40
|
+
- **MCP 协议集成**: 暴露标准 MCP 工具接口,支持 Trae AI Agent 和 VSCode 智能体
|
|
41
|
+
|
|
42
|
+
## 技术栈
|
|
43
|
+
|
|
44
|
+
- **MCP 框架**: FastMCP
|
|
45
|
+
- **HTTP 框架**: FastAPI
|
|
46
|
+
- **依赖管理**: uv
|
|
47
|
+
- **LLM 框架**: LangChain
|
|
48
|
+
- **向量数据库**: ChromaDB
|
|
49
|
+
- **文档采集**: LangChain AsyncChromiumLoader
|
|
50
|
+
- **源码采集**: PyGithub
|
|
51
|
+
|
|
52
|
+
## 用法
|
|
53
|
+
### 方法一:PyPI包方式
|
|
54
|
+
|
|
55
|
+
#### 安装PyPI包
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
pip install micro-app-mcp
|
|
59
|
+
|
|
60
|
+
# 运行依赖:DocsLoader 用 Playwright 爬取文档网站,Playwright 需要浏览器(Chromium)才能渲染页面
|
|
61
|
+
python -m playwright install chromium
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
#### 运行
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
micro-app-mcp
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### 方法二:源码方式
|
|
71
|
+
|
|
72
|
+
#### 安装源码
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
# 克隆仓库
|
|
76
|
+
git clone <repository-url>
|
|
77
|
+
cd micro-app-mcp
|
|
78
|
+
|
|
79
|
+
# 安装依赖
|
|
80
|
+
uv sync
|
|
81
|
+
|
|
82
|
+
uv run python -m playwright install chromium
|
|
83
|
+
|
|
84
|
+
# 将本项目作为 editable 包安装,解决项目中自身包路径问题
|
|
85
|
+
uv run pip install -e .
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
#### 环境变量配置
|
|
89
|
+
|
|
90
|
+
复制 `.env.example` 文件为 `.env` 并根据需要修改配置:
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
cp .env.example .env
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
主要配置项:
|
|
97
|
+
|
|
98
|
+
- `EMBEDDING_MODEL`: 向量化模型类型,目前只支持 `local`
|
|
99
|
+
- `EMBEDDING_MODEL_NAME`: 本地向量化模型名称,默认 `BAAI/bge-small-zh-v1.5`
|
|
100
|
+
- `EMBEDDING_LAZY_LOAD`: 是否启用懒加载,默认 `true`(推荐开启,可减少启动内存占用)
|
|
101
|
+
- `GITHUB_TOKEN`: GitHub API Token,可选,用于提高 API 限流
|
|
102
|
+
- `DATA_DIR`: 数据存储路径,默认 `~/work_space/tmp/micro_app_mcp`
|
|
103
|
+
- `CACHE_DURATION_HOURS`: 智能缓存配置,默认 24 小时
|
|
104
|
+
- `DISPLAY_TIMEZONE`: 状态展示时区,默认 `Asia/Shanghai`
|
|
105
|
+
|
|
106
|
+
#### 本地服务器直接运行
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
uv run micro-app-mcp
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
#### MCP 服务器配置与运行(适合宿主项目配合开发调试)
|
|
113
|
+
|
|
114
|
+
```json
|
|
115
|
+
{
|
|
116
|
+
"mcpServers": {
|
|
117
|
+
"micro-app-mcp": {
|
|
118
|
+
"command": "uv",
|
|
119
|
+
"args": [
|
|
120
|
+
"run",
|
|
121
|
+
"python",
|
|
122
|
+
"src/micro_app_mcp/main.py"
|
|
123
|
+
],
|
|
124
|
+
"cwd": "/Users/twonian/work_space/trinapower/ai_agent_service/micro-app-mcp",
|
|
125
|
+
"env": {
|
|
126
|
+
"DATA_DIR": "~/work_space/tmp/micro_app_mcp",
|
|
127
|
+
"GITHUB_TOKEN": "YOUR_GITHUB_TOKEN"
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
#### 测试
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
# 运行测试
|
|
138
|
+
uv run pytest
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### 方法三:IDE智能体方式
|
|
142
|
+
|
|
143
|
+
#### 安装依赖
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
uvx --from micro-app-mcp python -m playwright install chromium
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
#### MCP 服务器配置
|
|
150
|
+
|
|
151
|
+
###### 方法一:在 Trae AI Agent 中配置 MCP 服务器
|
|
152
|
+
|
|
153
|
+
- 打开 Trae AI Agent 配置文件(`config.json`)
|
|
154
|
+
- 添加以下内容:
|
|
155
|
+
|
|
156
|
+
```json
|
|
157
|
+
{
|
|
158
|
+
"mcpServers": {
|
|
159
|
+
"micro-app-knowledge": {
|
|
160
|
+
"command": "uvx",
|
|
161
|
+
"args": ["--from", "micro-app-mcp", "micro-app-mcp"],
|
|
162
|
+
"env": {
|
|
163
|
+
"DATA_DIR": "~/work_space/tmp/micro_app_mcp"
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
###### 方法二:在 VSCode 中配置 MCP 服务器
|
|
171
|
+
|
|
172
|
+
- 打开 VSCode 智能体配置文件(`mcp.json`)
|
|
173
|
+
- 添加以下内容:
|
|
174
|
+
|
|
175
|
+
```json
|
|
176
|
+
{
|
|
177
|
+
"mcpServers": {
|
|
178
|
+
"micro-app-knowledge": {
|
|
179
|
+
"command": "uvx",
|
|
180
|
+
"args": ["--from", "micro-app-mcp", "micro-app-mcp"],
|
|
181
|
+
"env": {
|
|
182
|
+
"DATA_DIR": "~/work_space/tmp/micro_app_mcp"
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
#### LLM调用MCP工具内在逻辑
|
|
190
|
+
|
|
191
|
+
当用户输入以 `/micro` 开头或包含 `/micro` 的消息时,会优先调用统一入口工具 `micro_app_command`,再按意图分发:
|
|
192
|
+
|
|
193
|
+
- 状态类请求:调用 `get_knowledge_base_status`
|
|
194
|
+
- 更新类请求:调用 `update_knowledge_base`
|
|
195
|
+
- 其他请求:调用 `search_micro_app_knowledge`
|
|
196
|
+
|
|
197
|
+
推荐的工具编排方式:
|
|
198
|
+
|
|
199
|
+
1. 先调用 `get_knowledge_base_status`(只读状态)
|
|
200
|
+
2. 当 `is_stale=true` 或用户明确要求更新时,再调用 `update_knowledge_base`
|
|
201
|
+
3. 最后调用 `search_micro_app_knowledge` 进行检索
|
|
202
|
+
|
|
203
|
+
可用 MCP 工具:
|
|
204
|
+
|
|
205
|
+
- `get_knowledge_base_status`: 返回本地时区的 `last_updated`/`next_recommended_update_at`,以及对应 UTC 字段、`is_stale`、`should_skip_update`、`document_count` 等状态信息
|
|
206
|
+
- `update_knowledge_base(force=False)`: 显式更新知识库(写操作)
|
|
207
|
+
- `search_micro_app_knowledge(query, top_k=15)`: 语义检索(只读)
|
|
208
|
+
|
|
209
|
+
#### 示例
|
|
210
|
+
|
|
211
|
+
1. `/micro 获取知识库状态`
|
|
212
|
+
2. `/micro 强制更新知识库 force=true`
|
|
213
|
+
3. `/micro <你的检索问题>`
|
|
214
|
+
|
|
215
|
+
## 项目结构
|
|
216
|
+
|
|
217
|
+
```
|
|
218
|
+
micro-app-mcp/
|
|
219
|
+
├── pyproject.toml # 项目配置
|
|
220
|
+
├── uv.lock # 依赖锁定
|
|
221
|
+
├── README.md # 项目说明
|
|
222
|
+
├── src/
|
|
223
|
+
│ └── micro_app_mcp/
|
|
224
|
+
│ ├── __init__.py
|
|
225
|
+
│ ├── main.py # FastMCP 入口
|
|
226
|
+
│ ├── config.py # 配置管理
|
|
227
|
+
│ ├── mcp/
|
|
228
|
+
│ │ ├── __init__.py
|
|
229
|
+
│ │ ├── server.py # MCP Server 定义
|
|
230
|
+
│ │ └── tools.py # MCP 工具实现
|
|
231
|
+
│ ├── knowledge/
|
|
232
|
+
│ │ ├── __init__.py
|
|
233
|
+
│ │ ├── base.py # 知识处理基类
|
|
234
|
+
│ │ ├── github_loader.py # GitHub 源码采集
|
|
235
|
+
│ │ ├── docs_loader.py # 文档采集 (Playwright)
|
|
236
|
+
│ │ ├── text_splitter.py # 文本分块
|
|
237
|
+
│ │ └── vectorizer.py # 向量化处理
|
|
238
|
+
│ ├── storage/
|
|
239
|
+
│ │ ├── __init__.py
|
|
240
|
+
│ │ ├── vector_store.py # ChromaDB 封装
|
|
241
|
+
│ │ └── metadata.py # 元数据管理
|
|
242
|
+
│ └── utils/
|
|
243
|
+
│ ├── __init__.py
|
|
244
|
+
│ └── logger.py # 日志工具
|
|
245
|
+
├── tests/
|
|
246
|
+
│ ├── __init__.py
|
|
247
|
+
│ ├── test_tools.py
|
|
248
|
+
│ └── test_knowledge.py
|
|
249
|
+
└── .env.example # 环境变量示例
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
## 贡献
|
|
253
|
+
|
|
254
|
+
欢迎提交 Issue 和 Pull Request!
|
|
255
|
+
|
|
256
|
+
## 许可证
|
|
257
|
+
|
|
258
|
+
MIT
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
# Micro-App MCP Server
|
|
2
|
+
|
|
3
|
+
MCP Server for micro-app knowledge base
|
|
4
|
+
|
|
5
|
+
## 项目简介
|
|
6
|
+
|
|
7
|
+
该项目是一个专门用于查询 micro-app 微前端框架知识库的 MCP Server。它整合了 GitHub 源码和在线文档内容,支持语义检索。当用户输入 `/micro` 时,LLM 会先检索知识库再回答问题。
|
|
8
|
+
|
|
9
|
+
## 核心功能
|
|
10
|
+
|
|
11
|
+
- **数据采集**: 自动从 GitHub 获取源码,从在线文档站点获取文档内容
|
|
12
|
+
- **知识向量化**: 将采集的文本内容进行分块、向量化存储
|
|
13
|
+
- **语义检索**: 提供基于向量相似度的语义检索能力
|
|
14
|
+
- **MCP 协议集成**: 暴露标准 MCP 工具接口,支持 Trae AI Agent 和 VSCode 智能体
|
|
15
|
+
|
|
16
|
+
## 技术栈
|
|
17
|
+
|
|
18
|
+
- **MCP 框架**: FastMCP
|
|
19
|
+
- **HTTP 框架**: FastAPI
|
|
20
|
+
- **依赖管理**: uv
|
|
21
|
+
- **LLM 框架**: LangChain
|
|
22
|
+
- **向量数据库**: ChromaDB
|
|
23
|
+
- **文档采集**: LangChain AsyncChromiumLoader
|
|
24
|
+
- **源码采集**: PyGithub
|
|
25
|
+
|
|
26
|
+
## 用法
|
|
27
|
+
### 方法一:PyPI包方式
|
|
28
|
+
|
|
29
|
+
#### 安装PyPI包
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pip install micro-app-mcp
|
|
33
|
+
|
|
34
|
+
# 运行依赖:DocsLoader 用 Playwright 爬取文档网站,Playwright 需要浏览器(Chromium)才能渲染页面
|
|
35
|
+
python -m playwright install chromium
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
#### 运行
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
micro-app-mcp
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### 方法二:源码方式
|
|
45
|
+
|
|
46
|
+
#### 安装源码
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
# 克隆仓库
|
|
50
|
+
git clone <repository-url>
|
|
51
|
+
cd micro-app-mcp
|
|
52
|
+
|
|
53
|
+
# 安装依赖
|
|
54
|
+
uv sync
|
|
55
|
+
|
|
56
|
+
uv run python -m playwright install chromium
|
|
57
|
+
|
|
58
|
+
# 将本项目作为 editable 包安装,解决项目中自身包路径问题
|
|
59
|
+
uv run pip install -e .
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
#### 环境变量配置
|
|
63
|
+
|
|
64
|
+
复制 `.env.example` 文件为 `.env` 并根据需要修改配置:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
cp .env.example .env
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
主要配置项:
|
|
71
|
+
|
|
72
|
+
- `EMBEDDING_MODEL`: 向量化模型类型,目前只支持 `local`
|
|
73
|
+
- `EMBEDDING_MODEL_NAME`: 本地向量化模型名称,默认 `BAAI/bge-small-zh-v1.5`
|
|
74
|
+
- `EMBEDDING_LAZY_LOAD`: 是否启用懒加载,默认 `true`(推荐开启,可减少启动内存占用)
|
|
75
|
+
- `GITHUB_TOKEN`: GitHub API Token,可选,用于提高 API 限流
|
|
76
|
+
- `DATA_DIR`: 数据存储路径,默认 `~/work_space/tmp/micro_app_mcp`
|
|
77
|
+
- `CACHE_DURATION_HOURS`: 智能缓存配置,默认 24 小时
|
|
78
|
+
- `DISPLAY_TIMEZONE`: 状态展示时区,默认 `Asia/Shanghai`
|
|
79
|
+
|
|
80
|
+
#### 本地服务器直接运行
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
uv run micro-app-mcp
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
#### MCP 服务器配置与运行(适合宿主项目配合开发调试)
|
|
87
|
+
|
|
88
|
+
```json
|
|
89
|
+
{
|
|
90
|
+
"mcpServers": {
|
|
91
|
+
"micro-app-mcp": {
|
|
92
|
+
"command": "uv",
|
|
93
|
+
"args": [
|
|
94
|
+
"run",
|
|
95
|
+
"python",
|
|
96
|
+
"src/micro_app_mcp/main.py"
|
|
97
|
+
],
|
|
98
|
+
"cwd": "/Users/twonian/work_space/trinapower/ai_agent_service/micro-app-mcp",
|
|
99
|
+
"env": {
|
|
100
|
+
"DATA_DIR": "~/work_space/tmp/micro_app_mcp",
|
|
101
|
+
"GITHUB_TOKEN": "YOUR_GITHUB_TOKEN"
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
#### 测试
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
# 运行测试
|
|
112
|
+
uv run pytest
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### 方法三:IDE智能体方式
|
|
116
|
+
|
|
117
|
+
#### 安装依赖
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
uvx --from micro-app-mcp python -m playwright install chromium
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
#### MCP 服务器配置
|
|
124
|
+
|
|
125
|
+
###### 方法一:在 Trae AI Agent 中配置 MCP 服务器
|
|
126
|
+
|
|
127
|
+
- 打开 Trae AI Agent 配置文件(`config.json`)
|
|
128
|
+
- 添加以下内容:
|
|
129
|
+
|
|
130
|
+
```json
|
|
131
|
+
{
|
|
132
|
+
"mcpServers": {
|
|
133
|
+
"micro-app-knowledge": {
|
|
134
|
+
"command": "uvx",
|
|
135
|
+
"args": ["--from", "micro-app-mcp", "micro-app-mcp"],
|
|
136
|
+
"env": {
|
|
137
|
+
"DATA_DIR": "~/work_space/tmp/micro_app_mcp"
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
###### 方法二:在 VSCode 中配置 MCP 服务器
|
|
145
|
+
|
|
146
|
+
- 打开 VSCode 智能体配置文件(`mcp.json`)
|
|
147
|
+
- 添加以下内容:
|
|
148
|
+
|
|
149
|
+
```json
|
|
150
|
+
{
|
|
151
|
+
"mcpServers": {
|
|
152
|
+
"micro-app-knowledge": {
|
|
153
|
+
"command": "uvx",
|
|
154
|
+
"args": ["--from", "micro-app-mcp", "micro-app-mcp"],
|
|
155
|
+
"env": {
|
|
156
|
+
"DATA_DIR": "~/work_space/tmp/micro_app_mcp"
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
#### LLM调用MCP工具内在逻辑
|
|
164
|
+
|
|
165
|
+
当用户输入以 `/micro` 开头或包含 `/micro` 的消息时,会优先调用统一入口工具 `micro_app_command`,再按意图分发:
|
|
166
|
+
|
|
167
|
+
- 状态类请求:调用 `get_knowledge_base_status`
|
|
168
|
+
- 更新类请求:调用 `update_knowledge_base`
|
|
169
|
+
- 其他请求:调用 `search_micro_app_knowledge`
|
|
170
|
+
|
|
171
|
+
推荐的工具编排方式:
|
|
172
|
+
|
|
173
|
+
1. 先调用 `get_knowledge_base_status`(只读状态)
|
|
174
|
+
2. 当 `is_stale=true` 或用户明确要求更新时,再调用 `update_knowledge_base`
|
|
175
|
+
3. 最后调用 `search_micro_app_knowledge` 进行检索
|
|
176
|
+
|
|
177
|
+
可用 MCP 工具:
|
|
178
|
+
|
|
179
|
+
- `get_knowledge_base_status`: 返回本地时区的 `last_updated`/`next_recommended_update_at`,以及对应 UTC 字段、`is_stale`、`should_skip_update`、`document_count` 等状态信息
|
|
180
|
+
- `update_knowledge_base(force=False)`: 显式更新知识库(写操作)
|
|
181
|
+
- `search_micro_app_knowledge(query, top_k=15)`: 语义检索(只读)
|
|
182
|
+
|
|
183
|
+
#### 示例
|
|
184
|
+
|
|
185
|
+
1. `/micro 获取知识库状态`
|
|
186
|
+
2. `/micro 强制更新知识库 force=true`
|
|
187
|
+
3. `/micro <你的检索问题>`
|
|
188
|
+
|
|
189
|
+
## 项目结构
|
|
190
|
+
|
|
191
|
+
```
|
|
192
|
+
micro-app-mcp/
|
|
193
|
+
├── pyproject.toml # 项目配置
|
|
194
|
+
├── uv.lock # 依赖锁定
|
|
195
|
+
├── README.md # 项目说明
|
|
196
|
+
├── src/
|
|
197
|
+
│ └── micro_app_mcp/
|
|
198
|
+
│ ├── __init__.py
|
|
199
|
+
│ ├── main.py # FastMCP 入口
|
|
200
|
+
│ ├── config.py # 配置管理
|
|
201
|
+
│ ├── mcp/
|
|
202
|
+
│ │ ├── __init__.py
|
|
203
|
+
│ │ ├── server.py # MCP Server 定义
|
|
204
|
+
│ │ └── tools.py # MCP 工具实现
|
|
205
|
+
│ ├── knowledge/
|
|
206
|
+
│ │ ├── __init__.py
|
|
207
|
+
│ │ ├── base.py # 知识处理基类
|
|
208
|
+
│ │ ├── github_loader.py # GitHub 源码采集
|
|
209
|
+
│ │ ├── docs_loader.py # 文档采集 (Playwright)
|
|
210
|
+
│ │ ├── text_splitter.py # 文本分块
|
|
211
|
+
│ │ └── vectorizer.py # 向量化处理
|
|
212
|
+
│ ├── storage/
|
|
213
|
+
│ │ ├── __init__.py
|
|
214
|
+
│ │ ├── vector_store.py # ChromaDB 封装
|
|
215
|
+
│ │ └── metadata.py # 元数据管理
|
|
216
|
+
│ └── utils/
|
|
217
|
+
│ ├── __init__.py
|
|
218
|
+
│ └── logger.py # 日志工具
|
|
219
|
+
├── tests/
|
|
220
|
+
│ ├── __init__.py
|
|
221
|
+
│ ├── test_tools.py
|
|
222
|
+
│ └── test_knowledge.py
|
|
223
|
+
└── .env.example # 环境变量示例
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
## 贡献
|
|
227
|
+
|
|
228
|
+
欢迎提交 Issue 和 Pull Request!
|
|
229
|
+
|
|
230
|
+
## 许可证
|
|
231
|
+
|
|
232
|
+
MIT
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "micro-app-mcp"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "MCP Server for micro-app knowledge base"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.12"
|
|
7
|
+
dependencies = [
|
|
8
|
+
"fastmcp>=0.1.0,<2.0",
|
|
9
|
+
"fastapi>=0.109.0",
|
|
10
|
+
"langchain>=0.2.0",
|
|
11
|
+
"langchain-community>=0.2.0",
|
|
12
|
+
"chromadb>=0.4.0",
|
|
13
|
+
"playwright>=1.40.0",
|
|
14
|
+
"huggingface-hub>=0.20.0",
|
|
15
|
+
"sentence-transformers>=2.0.0",
|
|
16
|
+
"uvicorn>=0.27.0",
|
|
17
|
+
"pydantic>=2.5.0",
|
|
18
|
+
"python-dotenv>=1.0.0",
|
|
19
|
+
"PyGithub>=1.58.0",
|
|
20
|
+
"mcp>=1.26.0",
|
|
21
|
+
"html2text>=2020.1.16",
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
[project.optional-dependencies]
|
|
25
|
+
dev = ["pytest>=7.0.0", "pytest-asyncio>=0.23.0"]
|
|
26
|
+
|
|
27
|
+
[build-system]
|
|
28
|
+
requires = ["setuptools>=61.0"]
|
|
29
|
+
build-backend = "setuptools.build_meta"
|
|
30
|
+
|
|
31
|
+
[tool.setuptools]
|
|
32
|
+
package-dir = { "" = "src" } # 告诉 setuptools:根包在 src/ 下,决定 wheel 包的目录结构没有包含 src 目录本身
|
|
33
|
+
|
|
34
|
+
[tool.setuptools.packages.find]
|
|
35
|
+
where = ["src"] # 在 src/ 目录下查找包,有配置 package-dir,此处可省略
|
|
36
|
+
include = ["micro_app_mcp"] # 只包含 micro_app_mcp 包
|
|
37
|
+
|
|
38
|
+
[project.scripts]
|
|
39
|
+
micro-app-mcp = "micro_app_mcp.main:main"
|
|
40
|
+
|
|
41
|
+
[tool.pytest.ini_options]
|
|
42
|
+
asyncio_mode = "auto" # 自动检测异步测试
|
|
43
|
+
testpaths = ["tests"]
|
|
44
|
+
python_files = ["test_*.py"]
|
|
45
|
+
python_classes = ["Test*"]
|
|
46
|
+
python_functions = ["test_*"]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""MCP 模块"""
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"""FastMCP Server 定义"""
|
|
2
|
+
|
|
3
|
+
from fastmcp import FastMCP
|
|
4
|
+
from micro_app_mcp.app.tools import (
|
|
5
|
+
get_knowledge_base_status as status_tool,
|
|
6
|
+
search_micro_app_knowledge as search_tool,
|
|
7
|
+
update_knowledge_base as update_tool
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
# 创建 FastMCP 实例,包含 /micro 触发机制
|
|
11
|
+
mcp = FastMCP(
|
|
12
|
+
"micro-app-knowledge-server",
|
|
13
|
+
instructions=(
|
|
14
|
+
"当用户消息以 /micro 开头或包含 /micro 时,优先调用 micro_app_command 工具。"
|
|
15
|
+
"micro_app_command 会自动分发:"
|
|
16
|
+
"1) 状态类请求 -> get_knowledge_base_status;"
|
|
17
|
+
"2) 更新/同步类请求 -> update_knowledge_base;"
|
|
18
|
+
"3) 其他请求 -> search_micro_app_knowledge。"
|
|
19
|
+
)
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _normalize_micro_command(command: str) -> str:
|
|
24
|
+
"""移除 /micro 前缀,得到实际命令内容"""
|
|
25
|
+
return command.replace("/micro", "", 1).strip()
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _is_status_command(command: str) -> bool:
|
|
29
|
+
"""判断是否为状态查询命令"""
|
|
30
|
+
keywords = ("状态", "status", "更新时间", "是否过期", "过期", "stale")
|
|
31
|
+
lowered = command.lower()
|
|
32
|
+
return "get_knowledge_base_status" in lowered or any(k in lowered for k in keywords)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _is_update_command(command: str) -> bool:
|
|
36
|
+
"""判断是否为更新命令"""
|
|
37
|
+
keywords = ("更新", "同步", "重建", "refresh", "update")
|
|
38
|
+
lowered = command.lower()
|
|
39
|
+
return "update_knowledge_base" in lowered or any(k in lowered for k in keywords)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _is_force_update(command: str) -> bool:
|
|
43
|
+
"""判断是否要求强制更新"""
|
|
44
|
+
lowered = command.lower()
|
|
45
|
+
return "force=true" in lowered or "强制" in lowered
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@mcp.tool()
|
|
49
|
+
async def micro_app_command(command: str, top_k: int = 15) -> object:
|
|
50
|
+
"""
|
|
51
|
+
统一处理 /micro 命令并自动分发到对应工具。
|
|
52
|
+
|
|
53
|
+
Args:
|
|
54
|
+
command: 用户输入命令(可包含 /micro 前缀)
|
|
55
|
+
top_k: 搜索时返回结果数量
|
|
56
|
+
|
|
57
|
+
Returns:
|
|
58
|
+
状态字典或结果文本
|
|
59
|
+
"""
|
|
60
|
+
normalized = _normalize_micro_command(command)
|
|
61
|
+
|
|
62
|
+
if _is_status_command(normalized):
|
|
63
|
+
return await status_tool()
|
|
64
|
+
|
|
65
|
+
if _is_update_command(normalized):
|
|
66
|
+
return await update_tool(force=_is_force_update(normalized))
|
|
67
|
+
|
|
68
|
+
query = normalized or command
|
|
69
|
+
return await search_tool(query, top_k)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@mcp.tool()
|
|
73
|
+
async def search_micro_app_knowledge(query: str, top_k: int = 15) -> str:
|
|
74
|
+
"""
|
|
75
|
+
语义检索 micro-app 知识库。
|
|
76
|
+
|
|
77
|
+
Args:
|
|
78
|
+
query: 用户查询内容
|
|
79
|
+
top_k: 返回最相关的结果数量,默认 15
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
格式化的检索结果,包含源码和文档片段
|
|
83
|
+
"""
|
|
84
|
+
return await search_tool(query, top_k)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@mcp.tool()
|
|
88
|
+
async def get_knowledge_base_status() -> dict:
|
|
89
|
+
"""
|
|
90
|
+
获取知识库状态。
|
|
91
|
+
|
|
92
|
+
Returns:
|
|
93
|
+
状态信息,包含更新时间、过期状态、文档数量等
|
|
94
|
+
"""
|
|
95
|
+
return await status_tool()
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
@mcp.tool()
|
|
99
|
+
async def update_knowledge_base(force: bool = False) -> str:
|
|
100
|
+
"""
|
|
101
|
+
触发知识库更新。
|
|
102
|
+
|
|
103
|
+
Args:
|
|
104
|
+
force: 是否强制更新,强制更新会重新采集所有数据
|
|
105
|
+
|
|
106
|
+
Returns:
|
|
107
|
+
更新结果摘要
|
|
108
|
+
"""
|
|
109
|
+
return await update_tool(force)
|