MCP Lucene 服务器
描述
MCP Lucene 服务器是基于模型上下文协议 (MCP) 的 Java 实现,旨在使用 Apache Lucene 提供高效的搜索和检索功能。此服务器允许您利用 Lucene 强大的索引和搜索功能来管理和查询文档。它使用 Spring Boot 构建,易于设置和部署。
特征
- **MCP 合规性:**实现核心模型上下文协议。
- **Lucene-Powered:**利用 Apache Lucene 进行全文搜索和索引。
- **RESTful API:**提供与服务器交互的 RESTful API。
- 文档管理:
- **Upsert:**在 Lucene 索引中添加或更新文档。
- **删除:**从 Lucene 索引中删除文档。
- **列表:**从索引中检索文档列表。
- 查询:
- 支持使用 Lucene 查询语法的复杂查询。
- 过滤:根据文档元数据过滤查询。
- **状态:**检查服务器状态。
- **Spring Boot:**使用 Spring Boot 构建,可轻松设置和部署。
- **Dockerization:**包括使用 Docker 对应用程序进行容器化的说明。
目录
入门
先决条件
- Java: Java 11 或更高版本。
- Maven: Maven 3.6.0 或更高版本。
- **Docker:**如果您计划使用 Docker 镜像,请安装 Docker 。
安装
- 克隆存储库:
git clone [https://github.com/your-username/mcp-lucene-server.git](https://github.com/your-username/mcp-lucene-server.git)
cd mcp-lucene-server
(将your-username
替换为您的 GitHub 用户名) - 使用 Maven 构建项目:
运行服务器
没有 Docker
- 运行 Spring Boot 应用程序:
java -jar target/mcp-lucene-server-0.0.1-SNAPSHOT.jar
( .jar
文件的确切名称可能会根据项目版本的不同而略有不同。) - 服务器将默认在端口
8080
上启动。
使用 Docker
- **确保已安装 Docker:**按照 Docker 官方网站上的说明进行操作: https://docs.docker.com/get-docker/
- **构建 Docker 镜像:**在终端中导航到项目的根目录并运行:
docker build -t mcp-lucene-server .
- 运行 Docker 容器:
docker run -p 8080:8080 mcp-lucene-server
这会将主机上的端口8080
映射到容器内的端口8080
。
用法
API 端点
服务器提供以下 API 端点:
GET /mcp/v1/status
POST /mcp/v1/upsert
- 更新(插入或更新)一个或多个文档。
- 请求正文:
{
"documents": [
{
"id": "doc1",
"text": "This is the text of document 1.",
"metadata": {
"category": "example",
"language": "english"
}
},
{
"id": "doc2",
"text": "This is document 2's text.",
"metadata": {
"category": "sample",
"language": "spanish"
}
}
]
}
POST /mcp/v1/query
- 查询 Lucene 索引。
- 请求正文:
{
"queries": [
{
"query": "document",
"top_k": 10,
"filter": {
"language": "english"
}
},
{
"query": "text search",
"filter": {
"category": "example"
}
}
]
}
query
:Lucene 查询字符串。top_k
:(可选)要返回的最大结果数(默认值:10)。filter
:(可选)要过滤的元数据字段和值的映射。
POST /mcp/v1/delete
- 从 Lucene 索引中删除文档。
- 请求正文:
{
"ids": ["doc1", "doc2"]
}
GET /mcp/v1/list
- 列出 Lucene 索引中的文档。
- 请求正文:
{
"ids": ["doc1", "doc2"]
}
示例
获取服务器状态:
curl http://localhost:8080/mcp/v1/status
更新插入文档:
curl -X POST
http://localhost:8080/mcp/v1/upsert
-H 'Content-Type: application/json'
-d '{
"documents": [
{
"id": "doc1",
"text": "This is the text of document 1.",
"metadata": {
"category": "example",
"language": "english"
}
},
{
"id": "doc2",
"text": "This is document 2''s text.",
"metadata": {
"category": "sample",
"language": "spanish"
}
}
]
}'
查询文件:
curl -X POST
http://localhost:8080/mcp/v1/query
-H 'Content-Type: application/json'
-d '{
"queries": [
{
"query": "document text",
"top_k": 5,
"filter": {
"language": "english"
}
}
]
}'
删除文档:
curl -X POST
http://localhost:8080/mcp/v1/delete
-H 'Content-Type: application/json'
-d '{
"ids": ["doc1"]
}'
列出文件:
curl -X POST
http://localhost:8080/mcp/v1/list
-H 'Content-Type: application/json'
-d '{
"ids": ["doc1", "doc2"]
}'
配置
可以使用 Spring Boot 的应用程序属性来配置服务器。以下是一些关键属性:
server.port
:服务器监听的端口(默认值:8080)。lucene.index.path
:Lucene 索引目录的路径。索引数据就存储在这里。如果未设置,则使用默认位置。强烈建议将其配置为持久存储位置。
您可以在src/main/resources
目录中的application.properties
或application.yml
文件中设置这些属性,或者使用环境变量来设置这些属性。
示例application.properties
:
服务器.端口=8080 lucene.index.path=/路径/到/lucene/索引
执照
该项目采用Apache 2.0 许可证进行授权。