MCP server by cobra30299
MCP Server Java
A Model Context Protocol (MCP) server implementation in Java, demonstrating the core MCP features including tools, resources, and prompts.
Features
This MCP server provides:
Tools
- add: Add two numbers together
- multiply: Multiply two numbers
- get_current_time: Get the current server time
- greet: Greet a person by name
- ai_chat: Send prompts to Azure AI Foundry agent and get AI-powered responses
Resources
- server-info: Information about the MCP server
- documentation: Server documentation and usage examples
Prompts
- math_helper: Get help with mathematical calculations
- current_time: Get the current time
Prerequisites
- Java 17 or higher
- Maven 3.6+
Building the Server
Build the project using Maven:
mvn clean package
This will create an executable JAR file: target/mcp-server-java-1.0.0.jar
Running the Server
Run the server directly:
Export the Azure OpenAI API Key in Powershell :
$env:AZURE_OPENAI_API_KEY="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
Check that the environment variable is set
c:>echo $env:AZURE_OPENAI_API_KEY
🚀 Usage
Start HTTP Server:
java -jar target/mcp-server-java-1.0.0.jar --http
java -jar target/mcp-server-java-1.0.0.jar
Run Java Server:
Working Solution Step 1: Start the HTTP Server (in one terminal) java -jar target/mcp-server-java-1.0.0.jar --http (need http server to be running first)
Run Java Client:
Step 2: Run the Java Client (in another terminal) mvn exec:java "-Dexec.mainClass=com.example.mcp.client.McpJavaClient"
NPX Node Package eXecute
nxp command to test over web page
npx @modelcontextprotocol/inspector java -jar target/mcp-server-java-1.0.0.jar
create a npx test harness to check out and test the functionality !
Testing out the tools :
HTTP JSON API Server started successfully!
API Endpoints:
http://localhost:8080/tools - List available tools
http://localhost:8080/tools/add - Add two numbers
http://localhost:8080/tools/multiply - Multiply two numbers
http://localhost:8080/tools/time - Get current time
http://localhost:8080/tools/greet - Greet by name
http://localhost:8080/tools/ai_chat - Chat with AI agent
List all tools
curl -X GET http://localhost:8080/tools
Add two numbers
curl -X POST http://localhost:8080/tools/add -H "Content-Type: application/json" -d '{"a":"7","b":"51"}'
Multiply two numbers
curl -X POST http://localhost:8080/tools/multiply -H "Content-Type: application/json" -d '{"x":"7","y":"70"}'
Get current time
curl -X GET http://localhost:8080/tools/time
Greet someone
curl -X POST http://localhost:8080/tools/greet -H "Content-Type: application/json" -d '{"name":"Steve"}'
Chat with AI Agent
curl -X POST http://localhost:8080/tools/ai_chat -H "Content-Type: application/json" -d '{"prompt": "What is Azure AI Foundry?"}'
curl -X POST http://localhost:8080/tools/ai_chat -H "Content-Type: application/json" -d '{"prompt": "Hello"}'
Chat with AI Agent with Optional Parameters
curl -X POST http://localhost:8080/tools/ai_chat -H "Content-Type: application/json" -d '{"prompt": "Explain Model Context Protocol in simple terms", "max_tokens": 200, "temperature": 0.5 }'
curl -X POST http://localhost:8080/tools/ai_chat -H "Content-Type: application/json" -d '{"prompt": "Write a haiku about coding", "max_tokens": 200, "temperature": 0.5 }'
🚀 Usage
Start HTTP Server:
java -jar target/mcp-server-java-1.0.0.jar --http
Start STDIO Server:
java -jar target/mcp-server-java-1.0.0.jar
git hub integration
in command prompt in git directory:
git status
On branch main
nothing to commit, working tree clean
PS C:\Users\stethompson\Microsoft\MCP_Server_Java> git init
Reinitialized existing Git repository in C:/Users/stethompson/Microsoft/MCP_Server_Java/.git/
PS C:\Users\stethompson\Microsoft\MCP_Server_Java> git add .
PS C:\Users\stethompson\Microsoft\MCP_Server_Java> git remote add origin https://github.com/SteveThompson_msftcae/MCP_SERVER_JAVA.git
git remove -v (check remote exists)
PS C:\Users\stethompson\Microsoft\MCP_Server_Java> git branch -M main
PS C:\Users\stethompson\Microsoft\MCP_Server_Java> git push -u origin main
info: please complete authentication in your browser...
fatal: The request is not supported
Username for 'https://github.com':
(authenticated with code)
PS C:\Users\stethompson\Microsoft\MCP_Server_Java> git push -u origin main
Enumerating objects: 48, done.
Counting objects: 100% (48/48), done.
Delta compression using up to 8 threads
Compressing objects: 100% (31/31), done.
Writing objects: 100% (48/48), 23.38 KiB | 1.80 MiB/s, done.
Total 48 (delta 8), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (8/8), done.
To https://github.com/SteveThompson_msftcae/MCP_SERVER_JAVA.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.
With Claude Desktop
Add the server to your Claude Desktop configuration:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"mcp-server-java": {
"command": "java",
"args": [
"-jar",
"C:\\absolute\\path\\to\\target\\mcp-server-java-1.0.0.jar"
]
}
}
}
Replace the path with the absolute path to your JAR file.
With VS Code
The server is already configured in .vscode/mcp.json. After building:
- Open VS Code
- Press
Cmd+Shift+P(Mac) orCtrl+Shift+P(Windows) - Select "MCP: Add server..."
- The server will be available to connect
Development
Project Structure
mcp-server-java/
├── src/
│ └── main/
│ └── java/
│ └── com/
│ └── example/
│ └── mcp/
│ ├── Main.java # Entry point
│ ├── ToolsProvider.java # Tool implementations
│ ├── ResourcesProvider.java # Resource implementations
│ └── PromptsProvider.java # Prompt implementations
├── .vscode/
│ └── mcp.json # VS Code MCP configuration
├── pom.xml # Maven configuration
└── README.md # This file
Adding New Tools
To add a new tool:
- Open
src/main/java/com/example/mcp/ToolsProvider.java - Add the tool definition in
registerTools() - Implement the tool logic in the switch statement
Example:
// In the list tools handler
Tool myTool = new Tool();
myTool.setName("my_tool");
myTool.setDescription("Description of what my tool does");
// ... set input schema
tools.add(myTool);
// In the call tool handler
case "my_tool":
// Your implementation here
break;
Adding New Resources
To add a new resource:
- Open
src/main/java/com/example/mcp/ResourcesProvider.java - Add the resource in
registerResources() - Implement the read handler for your resource URI
Testing with MCP Inspector
You can test your server using the MCP Inspector:
npx @modelcontextprotocol/inspector java -jar target/mcp-server-java-1.0.0.jar
Debugging
The server uses SLF4J for logging. Logs are written to stderr and will appear in your terminal or the client's log viewer.
To enable verbose logging, you can adjust the logging level in your slf4j configuration.
Learn More
License
MIT License - See LICENSE file for details