模型上下文协议客户端

一个基于Spring AI和Kotlin的MCP(模型上下文协议)客户端实现,用于构建和连接AI应用。

  • by upeter
  • Kotlin
  • 混合部署
  • 1 星标
  • 更新于
在 GitHub 查看 upeter/jvm-mcp-talk

使用说明

JVM会话中MCP(模型上下文协议)的资源

JVM/Kotlin演示项目 MCP(模型上下文协议) 使用 春季AI.

此仓库包含:

  • spring-ai/:Spring Boot应用程序充当 MCP客户端 以及“主要”人工智能应用程序。
  • spring-mcp-server/:Spring Boot应用程序充当 MCP服务器 公开工具/资源/提示/完成。
  • chatclient-kmp/:Kotlin多平台(编写桌面) UI客户端 这与 spring-ai 通过HTTP。
  • docker-compose.yaml + docker-entrypoint-initdb.d/:PostgreSQL+ pg向量 用作矢量存储的数据库。

快速启动(此仓库设置的配置)

检查表:

  • 启动pgvector数据库
  • 开始 spring-mcp-server 带有个人资料 default,streaming (必填)
  • 开始 spring-ai
  • (可选)启动 chatclient-kmp

1) 数据库(pgvector)

数据库通过Docker Compose启动并监听 localhost:5430 (映射到容器5432)。

docker compose up -d

初始化脚本位于 docker-entrypoint-initdb.d/.

2) MCP服务器(spring-mcp-server)--所需配置文件

使应用程序工作 照原样,使用Spring配置文件启动MCP服务器:

  • default (常用设置+OpenAI+pgvector)
  • streaming (流式HTTP传输)
cd spring-mcp-server
./mvnw spring-boot:run -Dspring-boot.run.profiles=default,streaming

默认情况下,在 streaming 服务器监听的配置文件 端口 8080.

3) Spring AI应用程序(spring-ai)

这是主要的应用程序。它继续运行 端口 8082.

它期望通过环境变量获得一个OpenAI API密钥。

export OPENAI_API_KEY=...  # required
cd spring-ai
./mvnw spring-boot:run

4) 桌面用户界面(chatclient-kmp)(可选)

编写调用的桌面客户端 spring-ai (HTTP API)。

cd chatclient-kmp
./gradlew :run

这些碎片是如何组合在一起的

运行时拓扑

  • chatclient-kmp (桌面用户界面)
    • 电话→ spring-ai 上 http://localhost:8082
  • spring-ai (Spring AI“应用程序”)
    • 电话→ spring-mcp-server 作为MCP 客户端
    • 此仓库中的默认传输: 可流式传输的HTTP
  • spring-mcp-server (MCP服务器)
    • uses → OpenAI(聊天+嵌入)
    • uses → Postgres/pgvector矢量存储在 jdbc:postgresql://localhost:5430/vector_store

港口

  • Postgres/pgvector: localhost:5430
  • MCP服务器: localhost:8080 (使用时 streaming 或 sse 配置文件)
  • Spring AI应用程序: localhost:8082

模块概述

docker-compose.yaml + docker-entrypoint-initdb.d/

  • 跑动 ankane/pgvector:latest
  • 创建数据库 vector_store 带用户名/密码 user/password
  • 初始化脚本/SQL从以下位置以只读方式装载 ./docker-entrypoint-initdb.d

注意:compose文件包含一个注释掉的持久卷行。如果您希望在重启过程中保持持久性,请启用 pgvector-data 体积映射。

spring-mcp-server/ (MCP服务器)

Spring Boot MCP服务器,使用 spring-ai-starter-mcp-server-webmvc.

关键功能(启用于 application-default.properties):

  • 工具能力: spring.ai.mcp.server.capabilities.tool=true
  • 资源能力: spring.ai.mcp.server.capabilities.resource=true
  • 快速能力: spring.ai.mcp.server.capabilities.prompt=true
  • 完成能力: spring.ai.mcp.server.capabilities.completion=true

还配置了:

  • OpenAI聊天模型: gpt-4o
  • OpenAI嵌入模型: text-embedding-3-small
  • pg向量表: public.talks

spring-ai/ (MCP客户端+应用程序)

Spring Boot应用程序使用:

  • spring-ai-starter-mcp-client
  • OpenAI聊天/嵌入
  • pg矢量存储

MCP客户端配置 spring-ai/src/main/resources/application.properties:

  • 流式HTTP连接(在此仓库中默认启用):
    • spring.ai.mcp.client.streamable-http.connections.conference-advisor-server.url=http://localhost:8080
    • spring.ai.mcp.client.streamable=true
  • 本地STDIO服务器配置(可用,但不是默认配置):
    • spring.ai.mcp.client.stdio.servers-configuration=classpath:/mcp-config.json
  • SSE连接(可用,默认已注释掉):
    • #spring.ai.mcp.client.sse.connections.conference-advisor-server.url=http://localhost:8080

chatclient-kmp/ (Kotlin多平台用户界面)

编写桌面应用程序(mainClass = "org.course.llm.chatapp.MainKt")使用Ktor客户端。

它是作为一个简单的前端 spring-ai.


spring-mcp-server 配置选项(重要)

MCP服务器支持通过Spring配置文件进行多种传输。此回购包括:

  • default (常用设置): spring-mcp-server/src/main/resources/application-default.properties
  • streaming: spring-mcp-server/src/main/resources/application-streaming.properties
  • sse: spring-mcp-server/src/main/resources/application-sse.properties
  • stdio: spring-mcp-server/src/main/resources/application-stdio.properties

选项A——通过本地(STDIO) mcp-config.json

这是经典的“本地MCP”设置,客户端将MCP服务器作为子进程启动,并通过STDIO进行通信。

在这个repo中:

  • 这 客户端 STDIO服务器注册表为 spring-ai/src/main/resources/mcp-config.json.
  • spring-ai 通过以下方式指向它:
    • spring.ai.mcp.client.stdio.servers-configuration=classpath:/mcp-config.json

要通过STDIO运行MCP服务器本身,请使用以下命令启动它 stdio 轮廓:

cd spring-mcp-server
./mvnw spring-boot:run -Dspring-boot.run.profiles=default,stdio

STDIO模式注意事项:

  • 这 stdio 配置文件禁用Spring横幅和控制台日志记录,并设置 spring.main.web-application-type=none.
  • STDIO通常在MCP服务器由客户端(例如检查器或桌面客户端)启动时使用。

选项B——流式传输/可流式传输HTTP(默认使用 spring-ai)

这是连接的配置 spring-ai 马上。

服务器端(spring-mcp-server):

  • 轮廓: streaming
  • 关键属性:
    • spring.ai.mcp.server.stdio=false
    • spring.ai.mcp.server.protocol=STREAMABLE
    • spring.ai.mcp.server.port=8080

运行它(这是 必需的 按原样设置仓库):

cd spring-mcp-server
./mvnw spring-boot:run -Dspring-boot.run.profiles=default,streaming

客户端(spring-ai):

  • spring.ai.mcp.client.streamable=true
  • spring.ai.mcp.client.streamable-http.connections.conference-advisor-server.url=http://localhost:8080

选项C-SSE(服务器发送事件)

服务器端(spring-mcp-server):

  • 轮廓: sse
  • 关键属性:
    • spring.ai.mcp.server.protocol=SSE
    • spring.ai.mcp.server.keep-alive-interval=20s
    • spring.ai.mcp.server.port=8080

运行它:

cd spring-mcp-server
./mvnw spring-boot:run -Dspring-boot.run.profiles=default,sse

客户端(spring-ai):

  • 在中启用和配置SSE连接 spring-ai/src/main/resources/application.properties:
    • 取消批注 spring.ai.mcp.client.sse.connections.conference-advisor-server.url=http://localhost:8080
    • 如果您只需要SSE,请禁用流式传输(保持配置一致)

MCP检查员

您可以使用MCP检查器连接到服务器。

npx @modelcontextprotocol/inspector

提示:

  • 根据您测试的是STDIO还是基于HTTP的传输,选择所需的连接类型(例如,通过代理)。

配置和秘密

开放人工智能

两者 spring-ai 和 spring-mcp-server 使用:

  • spring.ai.openai.api-key=${OPENAI_API_KEY}

在shell环境中(或通过IDE运行配置)提供一次。

数据库

两个服务器模块默认为:

  • jdbc:postgresql://localhost:5430/vector_store
  • 用户/密码: user/password

故障排除

  • 无法从访问MCP服务器 spring-ai

    • 确保 spring-mcp-server 正在端口8080上运行,并以 default,streaming.
    • 确保没有其他东西使用端口8080。
  • 矢量存储错误/缺少表

    • 此repo设置 spring.ai.vectorstore.pgvector.initialize-schema=false.
    • 确保init SQL在 docker-entrypoint-initdb.d/ 或者为本地实验启用模式初始化。
  • STDIO模式不起作用

    • 这 stdio 配置文件禁用日志记录/横幅是有原因的——控制台输出会破坏STDIO协议流。

开发技巧

  • 你可以跑 spring-ai 和 spring-mcp-server 从IDE作为两个单独的运行配置。
    • spring-mcp-server:活动配置文件 default,streaming
    • spring-ai:不需要特殊配置文件
  • 对于DB,保持 docker compose up -d 在后台运行。