Sponsored by Deepsite.site

Pure Java 21 MCP Server

Created By
aliaksandr-haurylau-godel6 months ago
Content

Pure Java 21 MCP Server

This project is an implementation of a Model Context Protocol (MCP) server in pure Java 21, strictly adhering to the (conceptual) 2025-03-26 MCP specification. It is built from scratch without external MCP-specific SDKs.

Prerequisites

  • Java 21 SDK: Ensure you have Java 21 (or later) installed and configured.
  • Gson Library: This server uses the Gson library for JSON parsing and serialization. You will need to download the Gson JAR file.
    • You can typically download it from Maven Central. Search for "Gson" or com.google.code.gson:gson. Download the JAR (e.g., gson-2.10.1.jar or a later version).
    • Place the downloaded Gson JAR (e.g., gson-2.10.1.jar) in a known location, for example, a lib directory within this project (lib/gson-2.10.1.jar). The compilation and run commands below assume this location.

Project Structure

  • src/: Contains the Java source code.
    • main/java/io/modelcontextprotocol/server/: Root package.
      • jsonrpc/: Classes for JSON-RPC message structures, parsing, and error handling.
      • messages/: Classes for MCP-specific message payloads (initialize, tool definitions, etc.).
      • tools/: Implementation of tools (e.g., EchoTool).
      • transport/: Transport layer handling (e.g., StdioTransport).
      • McpServer.java: Core server logic.
      • Main.java: Application entry point.
  • lib/: (Recommended) Create this directory to store the Gson JAR.
  • mcp-server.log: Log file generated by the server during operation.
  • .gitignore: Specifies intentionally untracked files that Git should ignore.
  • README.md: This file.

Compilation

  1. Create Directories:

    • Ensure you have a lib directory in the project root and place the Gson JAR file (e.g., gson-2.10.1.jar) there.
    • Create a bin directory in the project root to store the compiled class files:
      mkdir lib
      mkdir bin
      # Download Gson JAR and place it in lib/
      # For example: wget https://repo1.maven.org/maven2/com/google/code/gson/gson/2.10.1/gson-2.10.1.jar -P lib/
      
  2. Compile the Server: Open a terminal or command prompt in the project's root directory. Adjust lib/gson-2.10.1.jar if you are using a different version or path for Gson.

    Nix (Linux/macOS):

    javac -d bin -cp "lib/gson-2.10.1.jar:src/main/java" $(find src/main/java -name "*.java")
    

    Windows (Command Prompt):

    javac -d bin -cp "lib\gson-2.10.1.jar;src\main\java" $(powershell -Command "(Get-ChildItem -Recurse -Filter *.java src\main\java | Select-Object -ExpandProperty FullName) -join ';' ")
    

    If the $(powershell ...) command doesn't work directly in cmd.exe, you might need to list the files manually or use a different approach:

    javac -d bin -cp "lib\gson-2.10.1.jar;src\main\java" src\main\java\io\modelcontextprotocol\server\*.java src\main\java\io\modelcontextprotocol\server\jsonrpc\*.java src\main\java\io\modelcontextprotocol\server\messages\*.java src\main\java\io\modelcontextprotocol\server	ools\*.java src\main\java\io\modelcontextprotocol\server	ransport\*.java
    

    (Ensure all package paths are included if manually listing files).

Running the Server

After successful compilation, run the server from the project's root directory:

Nix (Linux/macOS):

java -cp "bin:lib/gson-2.10.1.jar" io.modelcontextprotocol.server.Main

Windows (Command Prompt):

java -cp "bin;lib\gson-2.10.1.jar" io.modelcontextprotocol.server.Main

The server will start and listen for JSON-RPC messages on standard input (STDIO).

Implemented Features

  • Transport: STDIO (Content-Length header framed messages).
  • JSON-RPC: Version 2.0 message parsing and serialization.
  • MCP Core:
    • initialize request: Server responds with its capabilities.
    • initialized notification: Client confirms initialization.
    • shutdown request: Server acknowledges and prepares to exit.
    • exit notification: Server terminates.
  • Tools:
    • Basic "Tool" capability.
    • tool/call request for the echoTool.
    • echoTool: A simple tool that echoes a message.
      • Input schema: { "type": "object", "properties": { "message": { "type": "string" } } }
      • Output schema: { "type": "object", "properties": { "response": { "type": "string" } } }
  • Logging: Server activity is logged to mcp-server.log (FINE level and above) and to the console (INFO level and above).

Example Interaction (via STDIO)

Messages are framed with Content-Length and \r\n\r\n before the JSON body.

1. Client -> Server: initialize Request

Content-Length: 123

{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"processId":1234,"capabilities":{}}}

(Note: params content like processId and capabilities are illustrative for initialize but are currently ignored by this server.)

2. Server -> Client: initialize Response

Content-Length: 297

{"jsonrpc":"2.0","id":1,"result":{"capabilities":{"tools":[{"name":"echoTool","description":"A simple tool that takes a 'message' string and returns it prefixed with 'Echo: '.","inputSchema":{"type":"object","properties":{"message":"string"}},"outputSchema":{"type":"object","properties":{"response":"string"}}}]}}}

(Actual Content-Length will vary based on exact formatting. The server calculates this.)

3. Client -> Server: initialized Notification

Content-Length: 40

{"jsonrpc":"2.0","method":"initialized"}

(No id for notifications. Server does not respond.)

4. Client -> Server: tool/call for echoTool

Content-Length: 107

{"jsonrpc":"2.0","id":2,"method":"tool/call","params":{"toolName":"echoTool","params":{"message":"Hello MCP!"}}}

5. Server -> Client: tool/result Response

Content-Length: 74

{"jsonrpc":"2.0","id":2,"result":{"response":"Echo: Hello MCP!"}}

6. Client -> Server: shutdown Request

Content-Length: 49

{"jsonrpc":"2.0","id":3,"method":"shutdown","params":{}}

7. Server -> Client: shutdown Response

Content-Length: 34

{"jsonrpc":"2.0","id":3,"result":null}

8. Client -> Server: exit Notification

Content-Length: 36

{"jsonrpc":"2.0","method":"exit","params":{}}

(Server then terminates. No response.)

Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
DeepChatYour AI Partner on Desktop
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
Tavily Mcp
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.
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
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.
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.
Serper MCP ServerA Serper MCP Server
Amap Maps高德地图官方 MCP Server
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.
CursorThe AI Code Editor
WindsurfThe new purpose-built IDE to harness magic
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
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"
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
Playwright McpPlaywright MCP server
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.