SKILL.md--- name: langchain4j-mcp-server-patterns description: Provides LangChain4j patterns for implementing MCP (Model Context Protocol) servers, creating Java AI tools, exposing tool calling capabilities, and integrating MCP clients with AI services. Use when building a Java MCP server, implementing tool calling in Java, connecting LangChain4j to external MCP servers, or securing tool exposure for agent workflows. allowed-tools: Read, Write, Edit, Bash, Glob, Grep, WebFetch --- # LangChain4j MCP Server Implementation Patterns ## Overview Use this skill to design and implement Model Context Protocol (MCP) integrations with LangChain4j. The main concerns are: - defining a clean tool, resource, and prompt surface - choosing the right transport and bootstrap model - filtering unsafe capabilities before exposing them to agents or applications Keep `SKILL.md` focused on the implementation flow. Use the bundled references for expanded examples and API-level detail. ## When to Use Use this skill when: - building a Java MCP server that exposes tools, resources, or prompts - integrating LangChain4j with one or more external MCP servers - wiring MCP support into a Spring Boot application - filtering available tools by tenant, user role, or runtime context - adding observability, resilience, and safe failure handling around MCP interactions - reviewing an MCP integration for prompt-injection and side-effect risks Typical trigger phrases include `langchain4j mcp`, `java mcp server`, `mcp tool provider`, `spring boot mcp`, and `connect langchain4j to mcp`. ## Instructions ### 1. Design the MCP surface before writing code Decide what the server should expose: - tools for actions with clear inputs and side effects - resources for read-only or structured data access - prompts only when a reusable template adds real value Keep names stable, descriptions concrete, and schemas small enough for a client or model to understand quickly. ### 2. Implement providers with narrow responsibilities Use separate classes for each concern: - tool provider for executable functions - resource provider for discoverable and readable data - prompt provider for reusable prompt templates Validate arguments before execution and return clear error messages for invalid input or unavailable dependencies. ### 3. Choose the transport intentionally Use: - stdio for local integrations, CLI tools, and sidecar processes - HTTP or SSE for remote or shared services Pin external server versions and document how the process is started, authenticated, and monitored. ### 4. Bridge MCP into LangChain4j carefully When consuming MCP servers from LangChain4j: - initialize clients during application startup - cache tool lists only when stale metadata is acceptable - filter tools by trust level, environment, or user permissions - fail closed for dangerous tools rather than exposing everything by default ### 5. Add