TS

tzolov/spring-ai-mcp-blogpost

开发工具

本页由 AI 翻译,技术细节请以原文为准。 查看原文

31 stars 18 forks 质量 53 趋势 0

春季AI MCP入门博客文章的配套示例

概览

本仓库包含了 Spring AI 模型上下文协议(MCP)博客文章的示例代码,演示如何使用 Spring Boot 构建 MCP 服务器和客户端。 模型上下文协议(MCP)标准化了人工智能应用与外部工具和资源的交互方式。本示例展示了: - : 一个Spring Boot服务器,将天气预报功能暴露为MCP工具- : 一个连接MCP服务器并使用LLM(Anthropic Claude)的Spring Boot AI应用 - : 按坐标检索天气预报 - : 发送给客户端的结构化日志消息 - : 实时操作进度更新 - : 向客户端的大型语言模型请求AI生成内容 - :同时连接多个MCP服务器 - :同时支持Streamable-HTTP和STDIO传输 - :进程服务器通知和请求 - :与Anthropic Claude无缝集成 - Java 17 或更高版本 - Maven 3.6+ - Spring AI 1.1.0-SNAPSHOT - 节点。

概述(AI 翻译)

本仓库包含了 Spring AI 模型上下文协议(MCP)博客文章的示例代码,演示如何使用 Spring Boot 构建 MCP 服务器和客户端。

概述

模型上下文协议(MCP)标准化了人工智能应用与外部工具和资源的交互方式。本示例展示了:

  • MCP 天气服务器:一个春启服务器,将天气预报功能作为 MCP 工具暴露
  • MCP 天气客户端:一个 Spring Boot AI 应用,连接 MCP 服务器并使用 LLM(Anthropic Claude)

建筑

功能演示

MCP 服务器功能

  • 工具:按坐标检索天气预报
  • 日志:发送给客户端的结构化日志消息
  • 进展跟踪:运营的实时进展更新
  • 采样:向客户端的大型语言模型请求AI生成内容

MCP 客户端功能

  • 多服务器连接:同时连接多个MCP服务器
  • 传输支持:支持可流式HTTP和STDIO传输
  • 处理程序注释:进程服务器通知和请求
  • LLM 集成:与 Anthropic Claude 无缝集成

先修条件

本页由 AI 翻译,技术细节请以原文为准。

README

以下为原文 README 节选,完整内容请查看原文。

Spring AI MCP Weather Example

This repository contains the example code for the Spring AI Model Context Protocol (MCP) blog post, demonstrating how to build both MCP Servers and Clients using Spring Boot.

Overview

The Model Context Protocol (MCP) standardizes how AI applications interact with external tools and resources. This example showcases:

  • MCP Weather Server: A Spring Boot server that exposes weather forecast capabilities as MCP tools
  • MCP Weather Client: A Spring Boot AI application that connects to MCP servers and uses an LLM (Anthropic Claude)

Architecture

┌────────────────────────────────────────────────────────────┐
│                    MCP Weather Client                      │
│  ┌─────────────┐  ┌──────────────┐  ┌──────────────────┐   │
│  │  ChatClient │──│ MCP Client   │──│ Weather Server   │   │
│  │  (Claude)   │  │  Transport   │  │ (Streamable-HTTP)│   │
│  └─────────────┘  └──────────────┘  └──────────────────┘   │
│                           │                                │
│                   ┌──────────────┐  ┌──────────────────┐   │
│                   │ MCP Client   │──│  Brave Search    │   │
│                   │  Transport   │  │     (STDIO)      │   │
│                   └──────────────┘  └──────────────────┘   │
└────────────────────────────────────────────────────────────┘

Features Demonstrated

MCP Server Features

  • Tools: Weather forecast retrieval by coordinates
  • Logging: Structured log messages sent to clients
  • Progress Tracking: Real-time progress updates for operations
  • Sampling: Request AI-generated content from the client’s LLM

MCP Client Features

  • Multi-Server Connection: Connect to multiple MCP servers simultaneously
  • Transport Support: Both Streamable-HTTP and STDIO transports
  • Handler Annotations: Process server notifications and requests
  • LLM Integration: Seamless integration with Anthropic Claude

Prerequisites

  • Java 17 or higher
  • Maven 3.6+
  • Spring AI 1.1.0-SNAPSHOT
  • Node.js and NPM (for optional Brave Search integration)
  • Anthropic API key (for the client)
  • Brave Search API key (optional, for web search)

Project Structure

spring-ai-mcp-blogpost/
├── mcp-weather-server/     # MCP Server implementation
│   ├── src/main/java/
│   │   └── .../server/
│   │       ├── McpServerApplication.java
│   │       └── WeatherService.java
│   └── pom.xml
├── mcp-weather-client/     # MCP Client implementation
│   ├── src/main/java/
│   │   └── .../client/
│   │       ├── McpClientApplication.java
│   │       └── McpClientHandlers.java
│   └── pom.xml
└── pom.xml                 # Parent POM

Quick Start

1. Configure Maven Repository

Both projects use Spring AI snapshots. The required repository is already configured in the pom.xml files:


    central-portal-snapshots
    Central Portal Snapshots
在 GitHub 查看完整 README

安装

npx @modelcontextprotocol/inspector
新手?先看 mcpskill.org 教程 →

配置

{ "mcpServers": { "spring-ai-mcp-weather": { "command": "java", "args": [ "-Dspring.ai.mcp.server.stdio=true", "-Dspring.main.web-application-type=none", "-Dlogging.pattern.console=", "-jar", "/path/to/mcp-weather-server.jar" ] } } }