|
| 1 | +[[MCP]] |
| 2 | += Model Context Protocol (MCP) |
| 3 | + |
| 4 | +The link:https://modelcontextprotocol.io/introduction[Model Context Protocol (MCP)] is an open protocol that standardizes how applications provide context to Large Language Models (LLMs). |
| 5 | +MCP provides an unified way to connect AI models to different data sources and tools, making integration seamless and consistent. |
| 6 | +It helps you build agents and complex workflows on top of LLMs. LLMs frequently need to integrate with data and tools, and MCP provides: |
| 7 | +- A growing list of pre-built integrations that your LLM can directly plug into |
| 8 | +- The flexibility to switch between LLM providers and vendors |
| 9 | + |
| 10 | +== Spring AI MCP |
| 11 | + |
| 12 | +NOTE: Spring AI MCP is an experimental project and subject to change. |
| 13 | + |
| 14 | +link:https://github.com/spring-projects-experimental/spring-ai-mcp[Spring AI MCP] is an experimental project that provides Java and Spring Framework integration for the Model Context Protocol. |
| 15 | +It enables Spring AI applications to interact with different data sources and tools, through a standardized interface, supporting both synchronous and asynchronous communication patterns. |
| 16 | + |
| 17 | +image::https://github.com/spring-projects-experimental/spring-ai-mcp/blob/main/spring-ai-mcp-architecture.jpg?raw=true[SpringAIMCP, 800] |
| 18 | + |
| 19 | +The Spring AI MCP implements a modular architecture with the following components: |
| 20 | + |
| 21 | +- Spring AI Application: Uses Spring AI framework to build Generative AI applications that want to access data through MCP |
| 22 | +- Spring MCP Clients: Spring AI implementation of the MCP protocol that maintain 1:1 connections with servers |
| 23 | +- MCP Servers: Lightweight programs that each expose specific capabilities through the standardized Model Context Protocol |
| 24 | +- Local Data Sources: Your computer's files, databases, and services that MCP servers can securely access |
| 25 | +- Remote Services: External systems available over the internet (e.g., through APIs) that MCP servers can connect to |
| 26 | + |
| 27 | +The architecture supports a wide range of use cases, from simple file system access to complex multi-model AI interactions with database and internet connectivity. |
| 28 | + |
| 29 | +== Getting Started |
| 30 | + |
| 31 | +Add the SDK to your Maven project: |
| 32 | + |
| 33 | + |
| 34 | +[tabs] |
| 35 | +====== |
| 36 | +Maven:: |
| 37 | ++ |
| 38 | +[source,xml,indent=0,subs="verbatim,quotes"] |
| 39 | +---- |
| 40 | +<dependency> |
| 41 | + <groupId>org.springframework.experimental</groupId> |
| 42 | + <artifactId>spring-ai-mcp-spring</artifactId> |
| 43 | + <version>0.1.0</version> |
| 44 | +</dependency> |
| 45 | +---- |
| 46 | +
|
| 47 | +Gradle:: |
| 48 | ++ |
| 49 | +[source,groovy,indent=0,subs="verbatim,quotes"] |
| 50 | +---- |
| 51 | +dependencies { |
| 52 | + implementation 'org.springframework.experimental:spring-ai-mcp-spring:0.1.0' |
| 53 | +} |
| 54 | +---- |
| 55 | +====== |
| 56 | + |
| 57 | +TIP: Refer to the xref:getting-started.adoc#repositories[Repositories] section to add the Spring Milestone Repository to your build file. |
| 58 | + |
| 59 | +The latter builds on top of mcp-core to provide some useful Spring AI abstractions, such as `McpFunctionCallback`. |
| 60 | + |
| 61 | +Now create an `McpClient` to regester the MCP server tools with your ChatClient and let the LLM call them: |
| 62 | + |
| 63 | +[source,java] |
| 64 | +---- |
| 65 | +// https://github.com/modelcontextprotocol/servers/tree/main/src/brave-search |
| 66 | +var stdioParams = ServerParameters.builder("npx") |
| 67 | + .args("-y", "@modelcontextprotocol/server-brave-search") |
| 68 | + .addEnvVar("BRAVE_API_KEY", System.getenv("BRAVE_API_KEY")) |
| 69 | + .build(); |
| 70 | +
|
| 71 | +var mcpClient = McpClient.sync(new StdioServerTransport(stdioParams)); |
| 72 | +
|
| 73 | +var init = mcpClient.initialize(); |
| 74 | +
|
| 75 | +var chatClient = chatClientBuilder |
| 76 | + .defaultFunctions(mcpClient.listTools(null) |
| 77 | + .tools() |
| 78 | + .stream() |
| 79 | + .map(tool -> new McpFunctionCallback(mcpClient, tool)) |
| 80 | + .toArray(McpFunctionCallback[]::new)) |
| 81 | + .build(); |
| 82 | +
|
| 83 | +String response = chatClient |
| 84 | + .prompt("Does Spring AI supports the Model Context Protocol? Please provide some references.") |
| 85 | + .call().content(); |
| 86 | +---- |
| 87 | + |
| 88 | + |
| 89 | +== Example Demos |
| 90 | + |
| 91 | +Explore these MCP examples in the link:https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol[spring-ai-examples/model-context-protocol] repository: |
| 92 | + |
| 93 | +- link:https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/sqlite/simple[SQLite Simple] - Demonstrates LLM integration with a database |
| 94 | +- link:https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/sqlite/chatbot[SQLite Chatbot] - Interactive chatbot with SQLite database interaction |
| 95 | +- https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/filesystem[Filesystem] - Enables LLM interaction with local filesystem folders and files |
| 96 | +- https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/brave[Brave] - Enables natural language interactions with Brave Search, allowing you to perform internet searches. |
| 97 | + |
| 98 | + |
0 commit comments