Sponsored by Deepsite.site

Descartes Java REPL and Deep Introspection For Your Java Apps

Created By
Wido Riezebos4 months ago
A Java-based Model Context Protocol (MCP) server that provides deep introspection, monitoring, debugging, and REPL capabilities for Java applications. Descartes enables AI assistants to interact with running Java processes through a comprehensive set of tools and resources. Kind of does for the Java backend what Playwright does for the frontend. Easy to add as a maven dependency to your Java application.
Content

Descartes MCP

A Java-based Model Context Protocol (MCP) server that provides deep introspection, monitoring, debugging, and REPL capabilities for Java applications. Descartes enables AI assistants to interact with running Java processes through a comprehensive set of tools and resources.

⚠️ SECURITY WARNING: This tool includes a Java REPL that allows arbitrary code execution. It should ONLY be used in development/debugging environments and NEVER exposed to untrusted networks or users. See Security Considerations for details.

Features

🛠️ Tools

  • JShell REPL: Interactive Java code execution with session management
  • Object Inspector: Deep object introspection without code execution
  • Process Inspector: Process and thread information monitoring
  • System Monitoring: Real-time system metrics and resource usage
  • Thread Analyzer: Thread state analysis and deadlock detection
  • Memory Analyzer: Heap usage, garbage collection, and memory pool analysis
  • Exception Analysis: Stack trace analysis and root cause identification
  • Logging Integration: Log4j2 integration for log capture and analysis

📊 Resources

  • Classpath Resource: Access to classpath information
  • System Properties: JVM and system property access
  • Metrics Resource: Application and JVM metrics
  • Thread Dumps: Detailed thread state snapshots
  • MBean Resource: JMX MBean access and monitoring
  • Application Context: Access to registered application objects

Requirements

  • Java 16 or higher (compiled with Java 23 for optimal performance)
  • Maven 3.6+
  • Node.js (for the MCP TCP adapter)

Quick Start

1. Clone and Build

gh repo clone widoriezebos/descartes-mcp
cd descartes-mcp
mvn clean package

2. Run the Example Server

mvn exec:java

This starts the MCP server on port 9080 with all available tools and resources registered.

3. Connect with an MCP Client

The repository includes a robust TCP adapter client in /config/mcp/ for easy integration:

Using the Included TCP Adapter

  1. Make the adapter executable (if needed):

    • Linux/macOS: chmod +x config/mcp/mcp-tcp-adapter.js
    • Windows: No action needed - Node.js handles execution
    • Alternative: Run directly with Node.js: node config/mcp/mcp-tcp-adapter.js
  2. Update the configuration file (config/mcp/mcpservers.json):

    "args": ["/absolute/path/to/descartes-mcp/config/mcp/mcp-tcp-adapter.js"]
    
  3. Copy to Claude Desktop configuration:

    • Copy mcpservers.json to your Claude Desktop config directory
    • The adapter will handle all connection management automatically
  4. Features of the TCP Adapter:

    • Automatic reconnection with exponential backoff
    • Message queuing during disconnections
    • Health monitoring and stale connection recovery
    • Never exits - handles all connection failures gracefully

See /config/mcp/README-adapter.md for detailed adapter documentation.

Build Commands

# Build the project
mvn clean compile

# Run tests (excludes concurrency tests by default)
mvn test

# Run concurrency tests only
mvn test -Pconcurrency-tests

# Run all tests including concurrency tests
mvn test -Pall-tests

# Package with dependencies
mvn clean package

# Run the example server
mvn exec:java

Integration Guide

Standalone Usage

The SimpleMCPServerExample class demonstrates standalone usage:

// Create settings and context
DefaultSettings settings = new DefaultSettings();
Map<String, Object> context = new HashMap<>();

// Add application objects to context
context.put("myapp.service", myService);
context.put("myapp.repository", myRepository);

// Create and configure server
MCPServer server = new MCPServer(settings, 9080, context);
server.setServerName("My Application MCP Server");
server.setServerVersion("1.0.0");

// Register tools
server.registerTool(new JShellTool(context));
server.registerTool(new SystemMonitoringTool());
server.registerTool(new ThreadAnalyzerTool());
// ... register other tools

// Register resources
ResourceRegistry registry = new ResourceRegistry("app");
registry.registerResource(new ClasspathResource());
registry.registerResource(new MetricsResource());
// ... register other resources
server.registerResource(registry);

// Start server
server.start();

Embedding in Your Application

  1. Add Dependency (when published to Maven Central):
<dependency>
    <groupId>com.bitsapplied.descartes</groupId>
    <artifactId>descartes-mcp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>
  1. Initialize in Your Application:
public class MyApplication {
    private MCPServer mcpServer;
    
    public void start() {
        // Your application initialization
        
        // Initialize MCP server
        Map<String, Object> context = new HashMap<>();
        context.put("app", this);
        context.put("dataSource", dataSource);
        
        mcpServer = new MCPServer(new DefaultSettings(), 9080, context);
        // Register tools and resources
        mcpServer.start();
        
        // Add shutdown hook
        Runtime.getRuntime().addShutdownHook(new Thread(mcpServer::stop));
    }
}

Important Configuration Note: Log4j2 Setup

For the LoggingIntegrationTool to capture logs, you need to configure the custom InMemoryAppender. When running outside of the test scope, copy /src/test/resources/log4j2.properties to your main resources directory, or add these essential lines to your existing log4j2.properties:

# Register the custom appender package
packages = com.bitsapplied.descartes.util

# Configure the In-Memory Appender
appender.inMemory.type = InMemoryAppender
appender.inMemory.name = INMEMORY
appender.inMemory.layout.type = PatternLayout
appender.inMemory.layout.pattern = %d{dd-MM-yyyy HH:mm:ss} %5p %c{1}:%L - %m%n
appender.inMemory.maxBufferSize = 500
appender.inMemory.truncateBackTo = 400
appender.inMemory.loggerFilter = <your application package(s) here>

# Add to root logger appenders
rootLogger.appenderRefs = console, inMemory
rootLogger.appenderRef.inMemory.ref = INMEMORY

Without this configuration, the LoggingIntegrationTool will not be able to capture and analyze application logs.

Architecture

Core Design

Descartes implements the Model Context Protocol (MCP) using a flexible, extensible architecture:

  • Generic Context Pattern: Tools and resources access application objects through a Map<String, Object> context, avoiding tight coupling to specific application types. This allows Descartes to integrate with any Java application without requiring modifications to the application's codebase.

  • JSON-RPC Communication: The server communicates using JSON-RPC 2.0 protocol over TCP sockets, handling requests for tool execution and resource retrieval. Each client connection is managed in a separate thread for concurrent operation.

  • Plugin Architecture: Tools implement the MCPTool interface and resources implement MCPResource, making it easy to add new capabilities without modifying the core server.

Session Management

The JShell integration provides sophisticated session management:

  • Isolated Execution Contexts: Each AI conversation gets its own JShell instance with separate variable namespaces, preventing cross-contamination between sessions.
  • Configurable Timeouts: Sessions automatically expire after inactivity (default: 30 minutes) to prevent memory leaks.
  • State Preservation: Variables and imports persist across multiple evaluations within the same session, enabling complex multi-step interactions.
  • Context Injection: Application objects from the context map can be automatically exposed to JShell sessions, allowing direct manipulation of live application state.
  • Concurrent Session Support: Multiple sessions can run simultaneously without interference.

Resource Access Pattern

Resources follow a URI-based access pattern with a pluggable registry system:

  • URI Scheme: Resources use custom URI schemes (default: app://) for namespacing
  • Dynamic Discovery: Resources are discovered at runtime through the registry
  • Read-Only Access: Resources provide read-only views of system state for safety
  • JSON Serialization: All resource data is automatically serialized to JSON for transport

Available resource endpoints:

  • app://classpath - JVM classpath entries and loaded classes
  • app://system-properties - System and JVM properties
  • app://metrics - Application performance metrics
  • app://threads - Thread dumps and thread state analysis
  • app://mbeans - JMX MBean attributes and operations
  • app://context - Application-specific objects from the context map

Security Considerations

⚠️ WARNING: This tool includes a full Java REPL (JShell) that allows arbitrary code execution with the same permissions as the host JVM.

  • ARBITRARY CODE EXECUTION: The JShell tools (jshell_repl, jshell_session_manager, object_inspector) can execute ANY Java code submitted to them. This is NOT a sandboxed environment - code runs with full JVM permissions.
  • PRODUCTION WARNING: This server should NEVER be exposed to untrusted networks or users in production environments unless you fully understand and accept the security implications. The JShell REPL can:
    • Access and modify any objects in the application context
    • Read/write files on the filesystem
    • Make network connections
    • Execute system commands via Runtime.exec()
    • Access sensitive data in memory
    • Modify application state at runtime
  • RECOMMENDED DEPLOYMENT:
    • Development and debugging environments only
    • Localhost connections only (default)
    • Behind a firewall with strict access controls if network access is required
    • With authentication/authorization layers if exposed beyond localhost
  • Resource Isolation: While resources provide read-only access, the JShell tools can modify any accessible application state

Example Tool Usage

JShell REPL

{
  "tool": "jshell_repl",
  "arguments": {
    "code": "System.out.println(\"Hello from JShell!\");",
    "session_id": "session-123"
  }
}

Object Inspector

{
  "tool": "object_inspector",
  "arguments": {
    "expression": "context.get(\"myService\")",
    "operation": "inspect"
  }
}

System Monitoring

{
  "tool": "system_monitoring",
  "arguments": {
    "include_memory": true,
    "include_cpu": true,
    "include_gc": true
  }
}

Development

Project Structure

descartes-mcp/
├── src/main/java/com/bitsapplied/descartes/
│   ├── MCPServer.java              # Core server implementation
│   ├── tools/                      # Tool implementations
│   ├── resources/                  # Resource providers
│   ├── util/                       # Utility classes
│   └── example/                    # Example usage
├── src/test/                       # Test suite
├── config/mcp/                     # MCP client adapter
│   ├── mcp-tcp-adapter.js         # TCP adapter for Claude Desktop
│   ├── mcpservers.json            # Client configuration
│   └── README-adapter.md          # Adapter documentation
└── pom.xml                         # Maven configuration

Testing

The project uses JUnit 5 with separate test profiles:

  • Default tests for rapid feedback
  • Concurrency tests in isolation
  • Comprehensive test suite via DescartesTestSuite

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Write tests for new functionality
  4. Ensure all tests pass: mvn test -Pall-tests
  5. Submit a pull request

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Support

For issues, questions, or contributions, please visit the GitHub repository.

Server Config

{
  "mcpServers": {
    "morpheus": {
      "command": "node",
      "args": [
        "/path/to/mcp/mcp-tcp-adapter.js"
      ],
      "env": {
        "MCP_HOST": "localhost",
        "MCP_PORT": "9080",
        "MCP_DEBUG": "false",
        "MCP_RECONNECT_MIN_DELAY": "500",
        "MCP_RECONNECT_MAX_DELAY": "5000",
        "MCP_HEALTH_CHECK_INTERVAL": "5000",
        "MCP_LOG_RATE_LIMIT_WINDOW": "60000",
        "MCP_LOG_RATE_LIMIT_MAX": "10"
      }
    }
  }
}
Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Tavily Mcp
DeepChatYour AI Partner on Desktop
Playwright McpPlaywright MCP server
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
CursorThe AI Code Editor
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
WindsurfThe new purpose-built IDE to harness magic
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
ChatWiseThe second fastest AI chatbot™
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
Serper MCP ServerA Serper MCP Server
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
Amap Maps高德地图官方 MCP Server