Quarkus extension for integrating Model Context Protocol (MCP) client capabilities with LangChain4j
Enable MCP tools in LangChain4j AI services using declarative annotations. The @McpToolBox annotation controls which MCP servers provide tools to specific AI service methods, allowing fine-grained control over tool availability.
The @McpToolBox annotation is applied to methods in AI services annotated with @RegisterAiService. It specifies which MCP servers' tools should be available to that method.
package io.quarkiverse.langchain4j.mcp.runtime;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface McpToolBox {
/**
* MCP servers to use. Empty array means use all configured MCP servers.
*/
String[] value() default {};
}Parameters:
value() - Array of MCP server names to enable. If empty or not specified, all configured MCP servers will be used.Usage: Apply to methods in @RegisterAiService interfaces to enable MCP tools for that specific method.
Enable tools from a specific MCP server:
import io.quarkiverse.langchain4j.RegisterAiService;
import io.quarkiverse.langchain4j.mcp.runtime.McpToolBox;
import dev.langchain4j.service.UserMessage;
@RegisterAiService
public interface FileAssistant {
@McpToolBox("filesystem")
String manageFiles(@UserMessage String message);
}Configuration:
quarkus.langchain4j.mcp.filesystem.transport-type=stdio
quarkus.langchain4j.mcp.filesystem.command=npm,exec,@modelcontextprotocol/server-filesystem,/pathEnable tools from multiple specific MCP servers:
@RegisterAiService
public interface DevelopmentAssistant {
@McpToolBox({"github", "filesystem", "database"})
String developFeature(@UserMessage String request);
}Configuration:
# GitHub MCP server
quarkus.langchain4j.mcp.github.transport-type=streamable-http
quarkus.langchain4j.mcp.github.url=https://github-mcp.example.com/mcp
# Filesystem MCP server
quarkus.langchain4j.mcp.filesystem.transport-type=stdio
quarkus.langchain4j.mcp.filesystem.command=npm,exec,@modelcontextprotocol/server-filesystem,/workspace
# Database MCP server
quarkus.langchain4j.mcp.database.transport-type=stdio
quarkus.langchain4j.mcp.database.command=python,mcp-server-postgres.pyEnable tools from all configured MCP servers (empty array):
@RegisterAiService
public interface UniversalAssistant {
@McpToolBox // Empty = all servers
String handleAnyTask(@UserMessage String userMessage);
}Use different MCP server combinations for different methods:
@RegisterAiService
public interface SmartAssistant {
// Only filesystem tools
@McpToolBox("filesystem")
String readFiles(@UserMessage String message);
// Only GitHub tools
@McpToolBox("github")
String manageRepositories(@UserMessage String message);
// Both filesystem and GitHub tools
@McpToolBox({"filesystem", "github"})
String developCode(@UserMessage String message);
// All configured tools
@McpToolBox
String handleAnything(@UserMessage String message);
}If no @McpToolBox annotation is present, the method will NOT use any MCP tools:
@RegisterAiService
public interface BasicAssistant {
// No MCP tools available - only built-in capabilities
String chat(@UserMessage String message);
// MCP tools from "filesystem" server available
@McpToolBox("filesystem")
String chatWithFileAccess(@UserMessage String message);
}The extension automatically generates a ToolProvider bean that integrates all configured MCP clients. This can be controlled via configuration:
# Enable automatic tool provider generation (default: true)
quarkus.langchain4j.mcp.generate-tool-provider=trueWhen generate-tool-provider is true:
QuarkusMcpToolProvider bean@McpToolBox annotations filter which MCP clients are used per methodWhen generate-tool-provider is false:
ToolProvider implementation@McpToolBox annotations have no effectThe MCP extension integrates seamlessly with LangChain4j's AI service framework:
import io.quarkiverse.langchain4j.RegisterAiService;
import dev.langchain4j.service.SystemMessage;
import dev.langchain4j.service.UserMessage;
import dev.langchain4j.service.MemoryId;
@RegisterAiService
public interface CodeReviewAssistant {
@SystemMessage("You are a code review assistant with access to GitHub and filesystem tools.")
@McpToolBox({"github", "filesystem"})
String reviewPullRequest(
@MemoryId String userId,
@UserMessage String reviewRequest
);
}The AI service can now:
When expose-resources-as-tools is enabled, MCP resources are exposed as synthetic tools:
quarkus.langchain4j.mcp.expose-resources-as-tools=trueThis creates two additional tools:
list_resources - Lists available resources from MCP serversget_resource - Retrieves content of a specific resourceThese tools are available to any method with @McpToolBox:
@RegisterAiService
public interface ResourceAssistant {
@McpToolBox("content-server")
String accessContent(@UserMessage String query);
// Can now use list_resources and get_resource tools
}When OpenTelemetry is present, tool executions are automatically traced:
@RegisterAiService
public interface TracedAssistant {
@McpToolBox("filesystem")
String processFiles(@UserMessage String message);
// Tool calls are automatically traced with spans
}No additional configuration needed - tracing is automatic when OpenTelemetry is on the classpath.
@McpToolBox only works with the auto-generated ToolProvider (when generate-tool-provider=true)@McpToolBox must match configuration keys (e.g., quarkus.langchain4j.mcp.<name>)@McpToolBox enables all configured servers@McpToolBox have NO access to MCP toolscache-tool-list)Install with Tessl CLI
npx tessl i tessl/maven-io-quarkiverse-langchain4j--quarkus-langchain4j-mcp