Class SnmpMcpServer
java.lang.Object
org.snmp4j.mcp.SnmpMcpServer
Model Context Protocol Server implementation that provides a framework
for adding tools and contexts to extend server capabilities.
- Since:
- 0.0.1
- Version:
- 0.1.0
- Author:
- SNMP4J.org
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThe ServerConfiguration class provides configuration settings for a server application. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new MCP server with the default configuration.Creates a new MCP server with the specified configuration. -
Method Summary
Modifier and TypeMethodDescriptionbooleanaddResource(String uri, String name, String title, String description, String mimeType, Supplier<String> contentSupplier) Registers a new resource with the MCP server.protected io.modelcontextprotocol.json.McpJsonMapperCreates an instance ofMcpJsonMapperfor handling JSON operations specific to MCP (Managed Communication Protocol) server interactions.protected io.modelcontextprotocol.spec.McpServerTransportProviderCreates a transport provider for the MCP (Managed Communication Protocol) server.Gets all registered contexts.Gets all registered resource URIs.Gets all registered tools.Gets the server configuration.getContext(String contextId) Gets a context by its ID.intgetPort()Gets the server port.getResourceName(String uri) Gets the name of a resource by its URI.Gets a tool by its ID.booleanhasResource(String uri) Checks if a resource is registered.voidInitializes the server with default tools and contexts.booleanChecks if the server is running.voidloadModules(String[] modulePatterns) Loads and initializes specific modules based on the provided patterns.static voidThe main entry point for the MCP Server.booleanregisterContext(McpContext context) Registers a new context with the server.booleanregisterTool(McpTool tool) Registers a new tool with the server.booleanremoveResource(String uri) Removes a resource from the MCP server.voidUnloads all MIB modules from memory and notifies clients of the resource list change.voidSets the server configuration.voidsetPort(int port) Sets the server port.voidstart()Starts the server on the configured port.voidstop()Stops the server and releases all resources.booleanunloadModule(String moduleName) Unloads a MIB module from memory and notifies clients of the resource list change.intunloadModuleWithDependencies(String moduleName) Unloads a MIB module and all its dependents from memory, notifying clients of the resource list change.booleanunregisterContext(String contextId) Unregisters a context from the server.booleanunregisterTool(String toolId) Unregisters a tool from the server.
-
Constructor Details
-
SnmpMcpServer
public SnmpMcpServer()Creates a new MCP server with the default configuration. -
SnmpMcpServer
Creates a new MCP server with the specified configuration.- Parameters:
config- the server configuration
-
-
Method Details
-
initialize
public void initialize()Initializes the server with default tools and contexts. -
start
Starts the server on the configured port.- Throws:
IOException- if the server cannot be started
-
createMcpServerTransportProvider
protected io.modelcontextprotocol.spec.McpServerTransportProvider createMcpServerTransportProvider()Creates a transport provider for the MCP (Managed Communication Protocol) server. This method initializes and returns an instance ofStdioServerTransportProviderconfigured with an MCP-specific JSON mapper.- Returns:
- an instance of
McpServerTransportProviderused for managing server communication over standard I/O.
-
createMcpJsonMapper
protected io.modelcontextprotocol.json.McpJsonMapper createMcpJsonMapper()Creates an instance ofMcpJsonMapperfor handling JSON operations specific to MCP (Managed Communication Protocol) server interactions. The implementation utilizes aJacksonMcpJsonMapperSupplierto provide the JSON mapper.- Returns:
- a new
McpJsonMapperinstance
-
stop
public void stop()Stops the server and releases all resources. -
registerTool
Registers a new tool with the server.- Parameters:
tool- the tool to register- Returns:
- true if the tool was registered successfully, false otherwise
-
unregisterTool
Unregisters a tool from the server.- Parameters:
toolId- the ID of the tool to unregister- Returns:
- true if the tool was unregistered successfully, false if the tool was not found
-
getTool
-
getAllTools
-
registerContext
Registers a new context with the server.- Parameters:
context- the context to register- Returns:
- true if the context was registered successfully, false otherwise
-
unregisterContext
Unregisters a context from the server.- Parameters:
contextId- the ID of the context to unregister- Returns:
- true if the context was unregistered successfully, false if the context was not found
-
getContext
Gets a context by its ID.- Parameters:
contextId- the ID of the context to get- Returns:
- the context, or null if not found
-
getAllContexts
Gets all registered contexts.- Returns:
- a list of all registered contexts
-
setPort
public void setPort(int port) Sets the server port.- Parameters:
port- the port to use
-
getPort
public int getPort()Gets the server port.- Returns:
- the port
-
getConfig
Gets the server configuration.- Returns:
- the server configuration
-
setConfig
Sets the server configuration.- Parameters:
config- the server configuration
-
isRunning
public boolean isRunning()Checks if the server is running.- Returns:
- true if the server is running, false otherwise
-
main
The main entry point for the MCP Server.- Parameters:
args- command line arguments
-
addResource
public boolean addResource(String uri, String name, String title, String description, String mimeType, Supplier<String> contentSupplier) Registers a new resource with the MCP server.- Parameters:
uri- the URI identifier for the resourcename- the human-readable name of the resourcetitle- the human-readable title of the resourcedescription- the description of the resourcemimeType- the MIME type of the resource contentcontentSupplier- supplier function that provides the resource content- Returns:
- true if the resource was registered successfully, false otherwise
-
removeResource
Removes a resource from the MCP server.- Parameters:
uri- the URI of the resource to remove- Returns:
- true if the resource was removed successfully, false if the resource was not found
-
getAllResourceUris
-
getResourceName
-
hasResource
Checks if a resource is registered.- Parameters:
uri- the URI of the resource- Returns:
- true if the resource is registered, false otherwise
-
loadModules
Loads and initializes specific modules based on the provided patterns.- Parameters:
modulePatterns- an array of regular expression patterns used to match available modules.- Throws:
IOException- if an I/O error occurs during module loading.
-
unloadModule
Unloads a MIB module from memory and notifies clients of the resource list change.- Parameters:
moduleName- the MIB module name to unload- Returns:
trueif the module was loaded and is now removed,falseotherwise
-
unloadModuleWithDependencies
Unloads a MIB module and all its dependents from memory, notifying clients of the resource list change.- Parameters:
moduleName- the MIB module name to unload- Returns:
- the number of MIB objects removed (including the module itself), or zero if nothing was removed
-
resetModules
public void resetModules()Unloads all MIB modules from memory and notifies clients of the resource list change.
-