Model Context Protocol (MCP) Introduction
MCP 是一种开放协议,它标准化了应用程序如何向LLMs提供上下文。将 MCP 想象成 AI 应用的 USB-C 端口。就像 USB-C 提供了一种标准化的方式将您的设备连接到各种外围设备和配件一样,MCP 提供了一种标准化的方式将 AI 模型连接到不同的数据源和工具。
Why MCP?
MCP 帮助您在LLMs之上构建代理和复杂的流程。LLMs通常需要与数据和工具集成,MCP 提供:
- 一个不断增长的预构建集成列表,LLM可以直接接入
- 可在LLM提供商和供应商之间切换的灵活性
- 最佳实践:确保基础设施内数据安全
Architecture
在其核心,MCP 遵循客户端-服务器架构,其中主机应用程序可以连接到多个服务器:
CleanShot 2025-03-06 at 09.45.44@2x.png
- MCP Hosts: 如 Claude 桌面、IDE 或希望通过 MCP 访问数据的 AI 工具的程序
- MCP Clients: 与服务器保持 1:1 连接的协议客户端
- MCP Servers:轻量级程序,每个程序通过标准化的模型上下文协议公开特定的功能
- Local Data Sources: 您的计算机文件、数据库以及 MCP 服务器可以安全访问的服务
- Remote Services:可通过互联网(例如,通过 API)访问的外部系统(例如,MCP 服务器可以连接到)
Build A MCP Server
我们将构建一个服务器,该服务器公开两个工具: get-alerts
和 get-forecast
。然后,我们将服务器连接到 MCP 主机.
Core MCP Concepts
MCP 服务器可以提供三种主要类型的能力:
- Resources:客户端可读取的数据(如 API 响应或文件内容)
- Tools:可以被LLM调用(需用户同意)的功能
- Prompts:帮助用户完成特定任务的预写模板
Java Example
System Requirements
- Java 17 或更高版本
- Spring Boot 3.3.x 或更高版本
Dependencies
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-server-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
</dependencies>
Application Properties
logging:
pattern:
console:
spring:
main:
banner-mode: off
Code
@Service
public class WeatherService {
private final RestClient restClient;
public WeatherService() {
this.restClient = RestClient.builder()
.baseUrl("https://api.weather.gov")
.defaultHeader("Accept", "application/geo+json")
.defaultHeader("User-Agent", "WeatherApiClient/1.0 (your@email.com)")
.build();
}
@Tool(description = "Get weather forecast for a specific latitude/longitude")
public String getWeatherForecastByLocation(
double latitude, // Latitude coordinate
double longitude // Longitude coordinate
) {
// Returns detailed forecast including:
// - Temperature and unit
// - Wind speed and direction
// - Detailed forecast description
}
@Tool(description = "Get weather alerts for a US state")
public String getAlerts(
@ToolParam(description = "Two-letter US state code (e.g. CA, NY") String state)
) {
// Returns active alerts including:
// - Event type
// - Affected area
// - Severity
// - Description
// - Safety instructions
}
// ......
}
@SpringBootApplication
public class McpServerApplication {
public static void main(String[] args) {
SpringApplication.run(McpServerApplication.class, args);
}
@Bean
public ToolCallbackProvider weatherTools(WeatherService weatherService) {
return MethodToolCallbackProvider.builder().toolObjects(weatherService).build();
}
}
Test With Java Client
- Manually
var stdioParams = ServerParameters.builder("java")
.args("-jar", "/ABSOLUTE/PATH/TO/PARENT/FOLDER/mcp-weather-stdio-server-0.0.1-SNAPSHOT.jar")
.build();
var stdioTransport = new StdioClientTransport(stdioParams);
var mcpClient = McpClient.sync(stdioTransport).build();
mcpClient.initialize();
ListToolsResult toolsList = mcpClient.listTools();
CallToolResult weather = mcpClient.callTool(
new CallToolRequest("getWeatherForecastByLocation",
Map.of("latitude", "47.6062", "longitude", "-122.3321")));
CallToolResult alert = mcpClient.callTool(
new CallToolRequest("getAlerts", Map.of("state", "NY")));
mcpClient.closeGracefully();
- Auto
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-client-spring-boot-starter</artifactId>
</dependency>
spring.ai.mcp.client.stdio.servers-configuration=file:PATH/TO/claude_desktop_config.json