春季MCP演示
这是一个将MCP客户端连接到MCP服务器的演示项目,两者都使用Spring MCP库实现。
客户端连接到正在运行的实例 没有,默认情况下使用Llama 3.1模型,但理论上可以使用 与MCP工具兼容的任何LLM模型。
Java版本
这是使用Java 24构建的。Java的较低版本也可能工作,但我还没有测试过。 它也是用Spring 3.5构建的。
应用程序
此演示应用程序包含一个连接到数据库的MCP服务器,用于 医院。它包含三个 桌子, doctors, patients,以及 appointments。我们想对这些表执行CRUD操作。也就是说,创造 患者和医生预约。 为此,我们将多个MCP工具暴露给MCP客户端,使其能够通过以下方式执行所需的数据库操作 来自用户的自然语言请求。
graph TD
User[User]
ClientApp[Client App]
Ollama[Ollama]
ServerApp[Server App]
DB[Database]
User -->|HTTP: port 8081| ClientApp
ClientApp -->|HTTP: port 11434| Ollama
ClientApp -->|HTTP: port 8080| ServerApp
ServerApp --> DB设置
安装程序需要4个组件
- PostgreSql数据库
- 正在执行的实例
- MCP服务器应用程序
- MCP客户端应用程序
没有
我们可以在docker中启动Ollama(不必进行码头化,但这是最简单的。)
docker run -d -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama数据库
MCP服务器包含对PostgreSQL数据库执行操作的工具。这 数据库可以通过运行以下命令启动
docker compose up --build它将使用Flyway在默认端口上构建、修改表和设置数据库 5432.
MCP服务器
MCP服务器可以从 mcp-server 模块目录,命令如下:
mvn spring-boot:run默认情况下,这将在端口上运行 8080.
MCP客户端
MCP客户端可以从 mcp-client 模块目录,命令如下:
mvn spring-boot:run客户端在端口上运行 8081.
使用应用程序
我们可以向我们的MCP应用程序(客户端)发送POST请求。 示例如下:
创建医生记录。
curl -X POST -H 'Content-Type: application/json' -d \
'{"message":"Create a doctor with first name Gregory, last name House. His specialization is diagnostics."}' \
http://localhost:8081/tool创建患者记录。
curl -X POST -H 'Content-Type: application/json' -d \
'{"message": "create a patient record with first name Bob and last name Jones. Email is bobjones@email.ccc, and the phone number is 090-123-567-432."}' \
http://localhost:8081/tool
创建预约。
curl -X POST -H 'Content-Type: application/json' -d \
'{"message":"Create an appointment for the doctor with id 1, and patient with id 1, where the time of the appointment is 2025/08/25 at 3:15 PM"}' \
http://localhost:8081/tool删除医生(逐级预约)
curl -X POST -H 'Content-Type: application/json' -d \
'{"message":"Delete the doctor record which has id 1."}' \
http://localhost:8081/tool