Skip to main content
skill CX Codex

langchain4j-mcp-server-patterns — Claude Code skill: how to install

Provides design patterns for building Java MCP servers and connecting them to LangChain4j. Useful when exposing tools in Spring Boot and securing agent execution.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add giuseppe-trisciuoglio/developer-kit --skill langchain4j-mcp-server-patterns -a claude-code
Open on GitHub

What langchain4j-mcp-server-patterns does

This skill guides the design and setup of Model Context Protocol servers in Java. It separates implementation concerns into dedicated providers for tools, discoverable resources, and prompts, keeping schemas compact for fast model comprehension.

It covers transport selection between stdio for local sidecars and HTTP or SSE for remote deployments. For LangChain4j integration, it shows how to wire McpToolProvider into AI services while filtering dangerous capabilities by user role or environment.

External MCP servers represent an untrusted boundary. Because LangChain4j and MCP APIs evolve quickly, you will need to align class names and builders with the exact library versions in your project.

What it helps with

  • build a Java MCP server with stdio or SSE transport
  • integrate LangChain4j AI services with external MCP servers
  • filter privileged MCP tools by tenant or user role
  • handle MCP timeouts, retries, and connection failures safely

Example prompts for the agent

  • Show a Java MCP tool provider example using stdio transport
  • How do I filter out admin tools in LangChain4j McpToolProvider?
  • Wire LangChain4j to an external MCP server in a Spring Boot app

How to install langchain4j-mcp-server-patterns

1Skills CLI (Vercel)

npx skills add giuseppe-trisciuoglio/developer-kit --skill langchain4j-mcp-server-patterns -a claude-code

2Manually: put the files in place

~/.claude/skills/langchain4j-mcp-server-patterns/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/langchain4j-mcp-server-patterns/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about langchain4j-mcp-server-patterns

How to install langchain4j-mcp-server-patterns in Claude Code?

Run npx skills add giuseppe-trisciuoglio/developer-kit --skill langchain4j-mcp-server-patterns -a claude-code in your terminal.

What does langchain4j-mcp-server-patterns do?

It provides architectural patterns to implement Java MCP servers and consume them in LangChain4j. It covers tool registration, resource handling, and security filtering.

Which transport does this skill recommend for Java MCP servers?

Use stdio for CLI tools, local integrations, or sidecar processes. For shared or remote setups, configure HTTP or SSE transports.

Source text

SKILL.md · MIT
---
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 resilience and security controls

At minimum:
- bound execution time for external calls
- log server and tool identity for each failure
- sanitize content returned by external resources before using it downstream
- isolate privileged tools behind allowlists, qualifiers, or role checks

### 6. Validate the full workflow

Before shipping:
- verify tool discovery and invocation with a real MCP client
- test disconnected or slow server behavior
- confirm that tool filtering matches the intended authorization model
- check that prompts and resources do not leak secrets or unsafe instructions

## Examples

### Example 1: Minimal tool provider and stdio server bootstrap

```java
class WeatherToolProvider implements ToolProvider {

    @Override
    public List<ToolSpecification> listTools() {
        return List.of(
            ToolSpecification.builder()
                .name("get_weather")
                .description("Return the current weather for a city")
                .inputSchema(Map.of(
                    "type", "object",
                    "properties", Map.of(
                        "city", Map.of("type", "string")
                    ),
                    "required", List.of("city")
                ))
                .build()
        );
    }

    @Override
    public String executeTool(String name, String arguments) {
        return weatherService.lookup(arguments);
    }
}

MCPServer server = MCPServer.builder()
    .server(new StdioServer.Builder())
    .addToolProvider(new WeatherToolProvider())
    .build();

server.start();
```

Use this pattern for local tool execution or a sidecar process started by another application.

### Example 2: Expose MCP tools to a LangChain4j AI service with filtering

```java
McpToolProvider toolProvider = McpToolProvider.builder()
    .mcpClients(mcpClients)
    .failIfOneServerFails(false)
    .filter((client, tool) -> !tool.name().startsWith("admin_"))
    .build();

Assistant assistant = AiServices.builder(Assistant.class)
    .chatModel(chatModel)
    .toolProvider(toolProvider)
    .build();
```

Use this pattern when you want LangChain4j to consume external MCP servers while still enforcing trust boundaries.

## Best Practices

- Keep each tool focused, deterministic, and well-described.
- Prefer explicit schemas over free-form string arguments.
- Separate read-only resources from tools with side effects.
- Filter or disable privileged tools by default.
- Pin external MCP server packages or container versions.
- Capture metrics for connection failures, invocation latency, and tool error rates.
- Store longer protocol details and framework-specific wiring in `references/` instead of expanding `SKILL.md` indefinitely.

## Constraints and Warnings

- External MCP servers are untrusted integration boundaries and may expose malicious or misleading content.
- Do not forward raw resource content directly into autonomous tool execution without validation.
- Some LangChain4j and MCP APIs evolve quickly; adapt class names and builders to the versions already used in the project.
- Long-running or stateful tools need explicit timeout, cancellation, and cleanup behavior.
- Stdio-based servers require process lifecycle management and robust logging.

## References

- `references/examples.md`
- `references/api-reference.md`

## Related Skills

- `prompt-engineering`
- `spring-ai` 
- `clean-architecture`

© giuseppe-trisciuoglio · MIT · giuseppe-trisciuoglio/developer-kit

Similar skills

All skills for Agent setup: memory, context, orchestration →

From the same set96

aws-rds-spring-boot-integration A skill that walks through wiring AWS RDS into Spring Boot: HikariCP pooling, SSL, IAM auth, Secrets Manager. CX Codex +1 3,700installs aws-sdk-java-v2-dynamodb Provides Amazon DynamoDB patterns using AWS SDK for Java 2.x and the Enhanced Client. Use it for type-safe CRUD, queries, transactions, and Spring Boot integration. CX Codex +1 3,700installs langchain4j-testing-strategies Guides Java tests for LangChain4j, from mocked ChatModel responses to RAG workflows and Testcontainers-based integration checks. CX Codex +1 3,700installs aws-sdk-java-v2-s3 Hands your agent ready-made Amazon S3 patterns for AWS SDK for Java 2.x: buckets, object uploads, multipart, presigned URLs. CX Codex +1 3,700installs langchain4j-vector-stores-configuration A skill that shows how to wire a LangChain4J vector store for RAG: connections, embedding dimensions, metadata filters. CX Codex +1 3,700installs unit-test-application-events Unit test patterns for Spring events: verify publishing through ApplicationEventPublisher and exercise @EventListener handlers without booting the context. CX Codex +1 3,700installs unit-test-config-properties Provides JUnit 5 patterns to unit test Spring Boot @ConfigurationProperties using ApplicationContextRunner without starting a full application context. CX Codex +1 3,700installs unit-test-caching Unit test patterns for Spring Cache annotations: cache hits and misses, eviction, SpEL keys, all without booting a full Spring context. CX Codex +1 3,700installs

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor