Class SnmpMcpServer

java.lang.Object
org.snmp4j.mcp.SnmpMcpServer

public class SnmpMcpServer extends Object
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
  • Constructor Details

    • SnmpMcpServer

      public SnmpMcpServer()
      Creates a new MCP server with the default configuration.
    • SnmpMcpServer

      public SnmpMcpServer(SnmpMcpServer.ServerConfiguration config)
      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

      public void start() throws IOException
      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 of StdioServerTransportProvider configured with an MCP-specific JSON mapper.
      Returns:
      an instance of McpServerTransportProvider used for managing server communication over standard I/O.
    • createMcpJsonMapper

      protected io.modelcontextprotocol.json.McpJsonMapper createMcpJsonMapper()
      Creates an instance of McpJsonMapper for handling JSON operations specific to MCP (Managed Communication Protocol) server interactions. The implementation utilizes a JacksonMcpJsonMapperSupplier to provide the JSON mapper.
      Returns:
      a new McpJsonMapper instance
    • stop

      public void stop()
      Stops the server and releases all resources.
    • registerTool

      public boolean registerTool(McpTool tool)
      Registers a new tool with the server.
      Parameters:
      tool - the tool to register
      Returns:
      true if the tool was registered successfully, false otherwise
    • unregisterTool

      public boolean unregisterTool(String toolId)
      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

      public McpTool getTool(String toolId)
      Gets a tool by its ID.
      Parameters:
      toolId - the ID of the tool to get
      Returns:
      the tool, or null if not found
    • getAllTools

      public List<McpTool> getAllTools()
      Gets all registered tools.
      Returns:
      a list of all registered tools
    • registerContext

      public boolean registerContext(McpContext context)
      Registers a new context with the server.
      Parameters:
      context - the context to register
      Returns:
      true if the context was registered successfully, false otherwise
    • unregisterContext

      public boolean unregisterContext(String contextId)
      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

      public McpContext getContext(String contextId)
      Gets a context by its ID.
      Parameters:
      contextId - the ID of the context to get
      Returns:
      the context, or null if not found
    • getAllContexts

      public List<McpContext> 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

      public void setConfig(SnmpMcpServer.ServerConfiguration config)
      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

      public static void main(String[] args)
      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 resource
      name - the human-readable name of the resource
      title - the human-readable title of the resource
      description - the description of the resource
      mimeType - the MIME type of the resource content
      contentSupplier - supplier function that provides the resource content
      Returns:
      true if the resource was registered successfully, false otherwise
    • removeResource

      public boolean removeResource(String uri)
      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

      public List<String> getAllResourceUris()
      Gets all registered resource URIs.
      Returns:
      a list of all registered resource URIs
    • getResourceName

      public String getResourceName(String uri)
      Gets the name of a resource by its URI.
      Parameters:
      uri - the URI of the resource
      Returns:
      the resource name, or null if not found
    • hasResource

      public boolean hasResource(String uri)
      Checks if a resource is registered.
      Parameters:
      uri - the URI of the resource
      Returns:
      true if the resource is registered, false otherwise
    • loadModules

      public void loadModules(String[] modulePatterns) throws IOException
      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

      public boolean unloadModule(String moduleName)
      Unloads a MIB module from memory and notifies clients of the resource list change.
      Parameters:
      moduleName - the MIB module name to unload
      Returns:
      true if the module was loaded and is now removed, false otherwise
    • unloadModuleWithDependencies

      public int unloadModuleWithDependencies(String moduleName)
      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.