Package org.machanism.machai.mcp.server


package org.machanism.machai.mcp.server
Provides the synchronous Model Context Protocol (MCP) server implementation used to expose Machai function tools and prompts to MCP clients.

The package has three layers:

  • AbstractMcpServer holds the common project-directory and configuration behavior and defines the tool-registration and startup lifecycle.
  • GenericGenaiAdapter and its prompt-aware subclass translate Machai ToolFunction and ParamDescriptor definitions into MCP tool schemas, invocation handlers, prompt definitions, and prompt results. The stateless HTTP adapter also translates resource definitions into resource read handlers.
  • StdioMcpServer, HttpStatelessMcpServer, and HttpStreamableMcpServer supply STDIO, stateless HTTP, and streamable HTTP transports respectively. AbstractHttpMcpServer centralizes the Jetty connector, servlet, thread-pool, port, and startup setup shared by the HTTP implementations.

Tool and prompt execution

The server launcher discovers function-based tools through FunctionToolsLoader. The optional enabledTools configuration property limits registration; its values may be separated by spaces, commas, semicolons, or other whitespace. Tool parameters are represented as JSON Schema, required arguments are validated by the MCP client against that schema, and function results are serialized into MCP text content. Invocation failures are returned as error tool results. Prompt arguments are exposed as MCP prompt arguments; string, collection, and other serialized function results become prompt messages. Streamable exchanges additionally make their session identifier available to functions through the tool-function session parameter. Stateless exchanges do not have a session identifier.

Transport behavior

  • StdioMcpServer creates a single-session synchronous MCP server over standard input and output, with tools, prompts, and logging enabled.
  • HttpStatelessMcpServer serves synchronous MCP requests through a Jetty servlet without retaining server-side session state. It advertises tools, prompts, and resources; resource functions receive the requested URI, configured project directory, and configurator.
  • HttpStreamableMcpServer serves synchronous streamable HTTP requests through Jetty and preserves the exchange session context for tool and prompt calls.

HTTP startup errors are wrapped in McpServerStartupException. Implementations report the Machai MCP homepage and icon metadata where the transport supports server implementation metadata.

Starting the server

McpServer.main(String[]) is the command-line entry point. Use -n/--name and -v/--version for server metadata, -d/--projectDir for the project directory, and -c/--config for an explicit properties file. Without an explicit configuration path, McpServer attempts McpServer.MCP_CONFIG_FILE_NAME (mcp.properties) and tolerates that default file being absent. Without -p/--port, it starts StdioMcpServer; with a port, it starts HttpStatelessMcpServer, or HttpStreamableMcpServer when -s/--session is also supplied.

Since:
1.2.0
Author:
Viktor Tovstyi