MCP Server SDK Usage
MCP Server SDK Setup
Add this dependency to build MCP servers:
<dependency>
<groupId>com.ajaxjs</groupId>
<artifactId>aj-mcp-server</artifactId>
<version>1.1</version>
</dependency>
We can find the latest version:
The server module includes:
McpServer
core processing engineFeatureMgr
for annotation-based feature discovery@Tool
,@Resource
,@Prompt
annotations- Transport implementations for HTTP/SSE and Stdio
Creating a Server
To create an MCP server, you need to:
- Define Service Classes: Create classes annotated with
@McpService
- Annotate Methods: Use
@Tool
,@Prompt
, or@Resource
annotations - Initialize Feature Manager: Scan packages for annotations
- Configure Transport: Set up HTTP/SSE or Stdio transport, and some details of server
- Start Server: Call
server.start()
Creating MCP Service Class
AJ-MCP automatically discovers, registers, and manages MCP features (tools, resources, and prompts) through annotation-based scanning.
This system enables developers to expose functionality simply by annotating methods with @Tool
, @Resource
, or @Prompt
annotations within classes
marked with @McpService
.
@McpService
public class MyServerFeatures {
@Tool(description = "Echoes a string")
public String echoString(@ToolArg(description = "Input string") String input) {
return input;
}
@Prompt(description = "Basic greeting prompt")
public PromptMessage greeting(@PromptArg(description = "Name") String name) {
PromptMessage message = new PromptMessage();
message.setRole(Role.USER);
message.setContent(new ContentText("Hello " + name));
return message;
}
}
Server Feature Management
The Feature Management system operates through a centralized FeatureMgr
class that coordinates package scanning, annotation processing, and feature
storage.The system uses reflection to discover annotated methods and stores feature metadata in concurrent hash maps for thread-safe runtime access.
Annotation System
The annotation system is built around several key annotations that mark classes and methods for MCP exposure:
Annotation | Target | Purpose |
---|---|---|
@McpService | Class | Marks a class for service discovery |
@Tool | Method | Exposes a method as an MCP tool |
@ToolArg | Parameter | Defines tool method parameters |
@Resource | Method | Exposes a method as an MCP resource |
@Prompt | Method | Exposes a method as an MCP prompt |
@PromptArg | Parameter | Defines prompt method parameters |
Server configuration includes annotation-driven feature discovery through FeatureMgr.init()
with package scanning FeatureMgr
. This automatically
discovers and registers @McpService
annotated classes containing @Tool
, @Resource
, and @Prompt
methods.
Initialize Feature Manager
The FeatureMgr.init()
method orchestrates the entire annotation discovery process. It begins by scanning specified packages for classes annotated
with @McpService
.
FeatureMgr mgr=new FeatureMgr();
mgr.init("com.foo.myproduct");.
Server Configuration
After feature manager initialization with package scanning, we can configure the server with:
- Server instance creation with transport layer setup
- Server configuration with name and version
- Connection timeout settings
Server configuration is handled through the ServerConfig class, which contains essential server metadata McpServerInitialize
. The configuration
includes server name, version, and supported protocol versions McpServerInitialize
.
During initialization, the server processes protocol version negotiation where it responds with the highest supported version or matches the client's
requested version if supported McpServerInitialize
.
FeatureMgr mgr=new FeatureMgr();
mgr.init("com.foo.myproduct");
McpServer server=new McpServer();
server.setTransport(new ServerStdio(server));
ServerConfig serverConfig=new ServerConfig();
serverConfig.setName("MY_MCP_Server");
serverConfig.setVersion("1.0");
server.setServerConfig(serverConfig);