春天艾mcp蒙古
使用Spring Boot、MongoDB和AI语言模型(Ollama或OpenAI)的全栈AI驱动查询系统。该系统由两个主要部分组成:
- mcp客户端:用于用户查询的REST API客户端。支持Ollama(本地)和OpenAI(云)提供程序进行LLM响应,并与mcp服务器通信进行结构化数据查询。
- mcp服务器:提供工具(API)以查找与人员相关的详细信息。从MongoDB查询人员数据。
- AI提供商:
- 奥拉玛 (默认):用于自然语言处理的本地LLM服务器。免费并在本地运行。 - 开放人工智能:基于GPT模型的云AI服务。需要API密钥。
- MongoDB:将人员数据存储在
person收藏。
序列图
sequenceDiagram
participant User
participant Client as mcp-client
participant LLM as AI Provider
(Ollama/OpenAI)
participant Server as mcp-server
participant DB as MongoDB
User->>Client: (1) Sends query for Person Data
Client->>Server: (2) Fetch all MCP Tools
Server-->>Client: (3) List of MCP Tools as JSON
Client->>LLM: (4) Request with User Query + MCP Tools
LLM-->>Client: (5) Response with Tool calls (if needed)
Client->>Server: (6) Request to Execute Tool (if needed)
Server->>DB: (7) Query person collection
DB-->>Server: (8) Person Data Result
Server-->>Client: (9) Tool Execution Result as JSON
Client->>LLM: (10) Final prompt with Tool Execution Result
LLM-->>Client: (11) NLP Based Response
Client-->>User: (12) Final answer as Text逐步解释
- 用户发送人员数据查询:
- 用户向mcp-client API发起请求(例如,“查找所有44岁以上的人”)。
- 客户端获取所有MCP工具:
- mcp客户端从mcp服务器请求可用于回答查询的可用工具(API)列表。
- 服务器以JSON格式返回MCP工具列表:
- mcp服务器以一个描述可用工具及其功能的JSON列表进行响应。
- 客户端使用用户查询+MCP工具向LLM发送请求:
- mcp客户端将用户查询和工具列表发送给选定的AI提供商(Ollama或OpenAI)进行推理和规划。
- LLM通过工具调用进行响应(如果需要):
- 人工智能提供商分析查询和工具,并可以用指令进行响应,以调用mcp-server上的特定工具(API)。
- 客户端请求服务器执行工具(如果需要):
- mcp客户端向mcp服务器发送请求以执行所需的工具(例如findPeopleOlderThan(年龄=44))。
- 服务器查询数据库中的人员集合:
- mcp服务器查询MongoDB人员集合以获取相关数据。
- DB返回人员数据结果:
- MongoDB将查询结果返回给mcp服务器。
- 服务器返回JSON格式的工具执行结果:
- mcp服务器将工具执行结果发送回mcp客户端。
- 客户端向LLM发送带有工具执行结果的最终提示:
- mcp客户端将工具结果和原始查询发送给AI提供者,以生成最终的自然语言响应。
- LLM返回基于NLP的响应:
- 人工智能提供商根据数据生成用户友好的自然语言答案,并将其返回给mcp客户端。
- 客户端将最终答案作为文本返回给用户:
- mcp客户端将最终答案发送回用户。
MongoDB架构(人)
MongoDB有一个名为 person 存储有关人员的详细信息。 每份文件 person 集合具有以下结构:
{
"_id": "ObjectId",
"firstName": "string",
"middleName": "string",
"lastName": "string",
"age": "int",
"street": "string",
"city": "string",
"state": "string",
"zipCode": "string"
}AI提供者配置
mcp客户端支持两个AI提供商:
Ollama(默认)
- 本地,免费:在您的机器上运行
- 模型:llama3.2(配置为支持工具调用)
- 设置:通过Docker Compose自动启动
- 不需要API密钥
开放人工智能
- 基于云的:需要互联网连接
- 模型:gpt-4o-mini(已配置)
- 设置:需要API密钥
- 设置环境变量:
export OPENAI_API_KEY=your-api-key-here在运行时选择提供程序
默认情况下,所有查询都使用 奥拉玛.要使用OpenAI,请添加 "provider": "openai" 根据您的要求:
使用Ollama(默认):
{
"query": "Find all people older than age 44"
}使用OpenAI:
{
"query": "Find all people older than age 44",
"provider": "openai"
}支持用于工具调用的Olama模型
以下Ollama型号支持函数/工具调用:
- ✅ 骆驼3.2 (3B,1B)-默认,平衡良好
- ✅ 骆驼3.1 (8B、70B)-出色的功能调用
- ✅ qwen2.5 (7B+)-非常好的支持
- ✅ 米斯特拉尔-尼莫 (12B)-良好的支持
- ❌ 米斯特拉尔:7b -有限/无支持
要使用其他型号,请更新 mcp-client/src/main/resources/application.yml:
spring:
ai:
ollama:
chat:
options:
model: llama3.1 # Change model here发展
先决条件
- Java 17+
- Docker&Docker编写
- 梅文
生成项目
./mvnw clean install运行项目
- 使用Docker Compose运行MongoDB和Ollama。
docker compose up -dOllama在第一次运行时下载模型,因此可能需要一些时间。下次它会更快,因为它使用卷来缓存模型。
- 运行mcp服务器应用程序
通过IDE或命令行运行mcp服务器。
./mvnw spring-boot:run -pl mcp-server- 运行mcp客户端应用程序
通过IDE或命令行运行mcp客户端。
./mvnw spring-boot:run -pl mcp-client备注:要使用OpenAI提供程序,请设置OPENAI_API_KEY运行mcp客户端之前的环境变量: ``sh export OPENAI_API_KEY=your-api-key-here ./mvnw spring-boot:run -pl mcp-client``
构建和运行(Docker中的所有服务)
./mvnw clean install && docker compose --profile client-server up -d --build这将构建项目并在Docker容器中启动MongoDB、Ollama、mcp客户端和mcp服务器。它还公开了端口8080(mcp服务器)和8082(mcp客户端),以便您可以通过localhost访问它们。
执行mcp服务器工具(可选)
您可以使用模型上下文协议检查器手动执行mcp服务器工具。
npx @modelcontextprotocol/inspector java -jar mcp-server/target/mcp-server-1.0.0.jar 例子
使用Ollama查询(默认)
使用默认Ollama提供程序发送查询:
curl --location 'http://localhost:8082/query' \
--header 'Content-Type: application/json; charset=UTF-8' \
--header 'Accept: */*' \
--data '{
"query": "Find all people older than age 44"
}'使用OpenAI进行查询
使用OpenAI提供程序发送查询:
curl --location 'http://localhost:8082/query' \
--header 'Content-Type: application/json; charset=UTF-8' \
--header 'Accept: */*' \
--data '{
"query": "Find all people older than age 44",
"provider": "openai"
}'样本响应:
{
"response": "Here are the people who are older than 44 years old that I found:\n\n* Andrew U Lewis, age 45, living at 999 Hickory Ln, Dallas, TX, zip code 75202.\n* Henry K Ward, age 45, living at 5100 Linden Ave, Fort Worth, TX, zip code 76106."
}更多示例查询
按城市查找人员:
{
"query": "Who lives in Dallas?"
}按州查找人员:
{
"query": "Show me all people from Texas"
}查找年龄范围内的人:
{
"query": "Find people between 30 and 40 years old"
}按姓氏查找:
{
"query": "Find all people with last name Smith"
}