Labsco
tersePrompts logo

fastMCP4J

โ˜… 8

from tersePrompts

Fast lightweight Java MCP server framework - Build Model Context Protocol servers with minimal boilerplate and full TypeScript SDK compatibility

๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅโœ“ VerifiedFreeQuick setup

FastMCP4J

Java library for building MCP servers โ€” annotation-driven, minimal dependencies

AI Agents โ†’ Share this skill with Claude for code generation

Lightweight. 12 dependencies. No containers.

Just annotate and run. See below โ†’

Note: Beta release (v0.3.1-beta) โ€” Multi-class modules, bash tools, telemetry. API stable.


Who This Is For

You want to...FastMCP4J
Expose Java tools to AI agentsโœ… Perfect fit
Build MCP servers quicklyโœ… Annotation-driven, minimal code
Add MCP to existing Spring appโœ… Drop-in, no framework lock-in
Lightweight MCP-only solutionโœ… 12 dependencies, not 50+
Fast startup & low memoryโœ… <500ms cold start, ~64MB

Icons

Add visual polish to your server, tools, resources, and prompts.

@McpServer(
    name = "my-server",
    icons = {
        "data:image/svg+xml;base64,...:image/svg+xml:64x64:light",
        "data:image/svg+xml;base64,...:image/svg+xml:64x64:dark"
    }
)
@McpTool(
    description = "My tool",
    icons = {"https://example.com/icon.png"}
)
public class MyServer { }

Resources & Prompts

@McpResource(uri = "config://settings")
public String getSettings() {
    return "{\"theme\": \"dark\"}";
}

@McpPrompt(name = "code-review")
public String codeReviewPrompt(@McpParam(description = "Code to review") String code) {
    return "Review this code:\n" + code;
}

Built-in Tools

Add ONE annotation, get complete functionality.

AnnotationTools You Get
@McpMemorylist, read, create, replace, insert, delete, rename
@McpTodoadd, list, updateStatus, updateTask, delete, clearCompleted
@McpPlannercreatePlan, listPlans, getPlan, addTask, addSubtask
@McpFileReadreadLines, readFile, grep, getStats
@McpFileWritewriteFile, appendFile, writeLines, deleteFile, createDirectory

Annotations Reference

AnnotationTargetPurpose
@McpServerTYPEDefine your MCP server
@McpToolMETHODExpose as callable tool
@McpResourceMETHODExpose as resource
@McpPromptMETHODExpose as prompt template
@McpParamPARAMETERAdd description, examples, constraints, defaults
@McpAsyncMETHODMake tool async (return Mono<?>)
@McpContextPARAMETERInject request context
@McpPreHookMETHODRun before tool call (params: toolName, order)
@McpPostHookMETHODRun after tool call (params: toolName, order)
@McpBashTYPEEnable bash/shell command execution tool
@McpTelemetryTYPEEnable metrics and tracing (params: enabled, exportConsole, exportOtlp, sampleRate)
@McpMemoryTYPEEnable memory tools
@McpTodoTYPEEnable todo/task management tools
@McpPlannerTYPEEnable planning tools
@McpFileReadTYPEEnable file reading tools
@McpFileWriteTYPEEnable file writing tools

@McpParam advanced options:

@McpTool(description = "Create task")
public String createTask(
    @McpParam(
        description = "Task name",
        examples = {"backup", "sync"},
        constraints = "Cannot be empty",
        defaultValue = "default",
        required = false
    ) String taskName
) { return "Created: " + taskName; }

Hooks โ€” Run Code Before/After Tools

Two hook types supported:

@McpPreHook โ€” Runs before tool is called. Receives Map<String, Object> arguments.

@McpPostHook โ€” Runs after tool completes. Receives Map<String, Object> arguments, Object result.

Use for logging, validation, authentication, audit trails, metrics.

@McpServer(name = "MyServer", version = "1.0")
public class MyServer {

    @McpTool(description = "Calculate")
    public int calculate(int x, int y) {
        return x + y;
    }

    // Run before ALL tools (*)
    @McpPreHook(toolName = "*", order = 1)
    void authenticate(Map<String, Object> args) {
        String token = (String) args.get("token");
        if (!isValid(token)) throw new SecurityException("Unauthorized");
    }

    // Run after specific tool only
    @McpPostHook(toolName = "calculate", order = 1)
    void logResult(Map<String, Object> args, Object result) {
        System.out.println("Result: " + result);
    }

    public static void main(String[] args) {
        FastMCP.server(MyServer.class).stdio().run();
    }
}

Hook options:

  • toolName โ€” Target specific tool name, or "*" for all tools. Empty = inferred from method name
  • order โ€” Execution priority (lower = first). Default: 0

Hook parameters:

  • Pre-hook: Map<String, Object> arguments โ€” Tool input arguments
  • Post-hook: Map<String, Object> arguments, Object result โ€” Input + output

Context Access โ€” Request Metadata

@McpContext โ€” Inject request context into your tool.

Access client info, session data, request metadata.

@McpServer(name = "MyServer", version = "1.0")
public class MyServer {

    @McpTool(description = "Get client info")
    public String getClientInfo(@McpContext Context context) {
        return "Client: " + context.getClientId();
    }

    @McpTool(description = "Get session ID")
    public String getSessionId(@McpContext Context context) {
        return "Session: " + context.getSessionId();
    }

    @McpTool(description = "Read file with context")
    public String readFile(@McpContext Context context, String path) {
        context.info("Reading file: " + path);

        // Access request headers (e.g., for auth)
        Map<String, String> headers = context.getRequestHeaders();
        String authHeader = headers.get("Authorization");

        // ... read file
        return "Content";
    }

    public static void main(String[] args) {
        FastMCP.server(MyServer.class).stdio().run();
    }
}

Context capabilities:

  • getClientId() โ€” Client identifier
  • getSessionId() โ€” Session identifier
  • getToolName() โ€” Current tool name
  • getRequestHeaders() โ€” Client request headers (e.g., auth tokens, custom headers)
  • info(String) โ€” Log info message
  • warning(String) โ€” Log warning
  • error(String) โ€” Log error
  • reportProgress(int, String) โ€” Report progress percentage
  • listResources() โ€” List available resources
  • listPrompts() โ€” List available prompts

New Features

@McpBash โ€” Shell Command Execution

Execute shell commands with OS-aware shell selection and built-in security guardrails.

@McpServer(name = "MyServer", version = "1.0")
@McpBash(
    timeout = 30,                          // Command timeout in seconds
    visibleAfterBasePath = "/sandbox/*",   // Whitelist allowed directories
    notAllowedPaths = {"/etc", "/root"}    // Blacklist dangerous paths
)
public class MyServer { }

Security features:

  • Directory validation (whitelist/blacklist)
  • Dangerous command blocking (rm -rf, wget, curl, ssh, etc.)
  • Directory traversal prevention
  • Cross-platform path handling (Windows/Unix)

Supported shells:

  • Windows: cmd.exe
  • macOS: /bin/zsh
  • Linux: /bin/bash

@McpTelemetry โ€” Metrics & Tracing

Collect metrics and traces for tool invocations.

@McpServer(name = "MyServer", version = "1.0")
@McpTelemetry(
    enabled = true,
    exportConsole = true,              // Human-readable output
    exportOtlp = false,                // OpenTelemetry export
    sampleRate = 1.0,                  // 100% sampling
    includeArguments = false,          // Don't log sensitive args
    metricExportIntervalMs = 60_000
)
public class MyServer { }

Collected metrics:

  • Tool invocation counters
  • Execution duration histograms
  • Error rates

Multi-Class Tool Organization

Split tools across multiple classes for better organization.

Manual modules (fast, explicit):

@McpServer(
    name = "MyServer",
    version = "1.0",
    modules = {StringTools.class, MathTools.class}
)

Package scanning (convenient):

@McpServer(
    name = "MyServer",
    version = "1.0",
    scanBasePackage = "com.example.tools"
)

Why FastMCP4J?

Less code

Raw MCP SDK: 35+ lines per tool FastMCP4J: ~8 lines per tool

Lightweight

FrameworkDependenciesBest For
Spring AI50+ jarsFull-stack AI apps
LangChain4j30+ jarsEnterprise AI pipelines
Quarkus AI40+ jarsCloud-native microservices
FastMCP4J12 jarsMCP servers only

Fast & focused

  • Cold start: <500ms
  • Tool invocation: <5ms
  • Memory: ~64MB
  • Purpose-built for MCP โ€” not a general AI framework

Documentation