module WarpEngine::AdminApi::Mcp
The Admin API as an MCP server: every Operation the caller may run becomes a tool. Needs the mcp gem in the hostβs Gemfile.
Constants
- INSTRUCTIONS
-
The instructions the MCP server gives the client.
Public Instance Methods
Source
# File lib/warp_engine/admin_api/mcp.rb, line 14 def available? return true if defined?(::MCP::Server) require "mcp" true rescue LoadError false end
Whether the mcp gem is loaded or loadable; requires it when needed.
# File lib/warp_engine/admin_api/mcp.rb, line 65 def call(operation, arguments, principal:, base_url:) result = Runner.call(operation, principal: principal, params: arguments, base_url: base_url, via: :mcp) response(result.payload) rescue Error => e response(e.body.merge(status: e.status).as_json, error: true) end
Source
# File lib/warp_engine/admin_api/mcp.rb, line 79 def description(operation) [ operation.summary, operation.description, "#{operation.verb.to_s.upcase} #{operation.route_path}; needs #{operation.required_scope}." ] .compact.join("\n\n").sub("needs any.", "needs any admin token.") end
The tool description of operation: summary, description, route and needed scope.
Source
# File lib/warp_engine/admin_api/mcp.rb, line 24 def ensure_available! return if available? raise ConfigurationError, "c.admin_api.mcp needs the mcp gem: add `gem \"mcp\"` to the host's Gemfile" end
Raises ConfigurationError unless available?.
Source
# File lib/warp_engine/admin_api/mcp.rb, line 31 def operations_for(principal) config = AdminApi.config Registry.operations.select do |operation| operation.mcp? && !config.mcp_excluded?(operation.resource.name) && Runner.permitted?(operation, principal) end end
The operations offered as tools to principal: MCP-enabled, not excluded, permitted.
# File lib/warp_engine/admin_api/mcp.rb, line 73 def response(payload, error: false) ::MCP::Tool::Response.new([ { type: "text", text: JSON.generate(payload) } ], error: error, structured_content: payload) end
An MCP::Tool::Response with payload as JSON text and structured content.
# File lib/warp_engine/admin_api/mcp.rb, line 39 def server(principal:, base_url:) tools = operations_for(principal).map { |operation| tool(operation, principal, base_url) } ::MCP::Server.new(name: "warp_engine", title: "WarpEngine Admin", version: WarpEngine::VERSION, instructions: INSTRUCTIONS, tools: tools) end
Builds an MCP::Server with the tools of principal, whose calls see base_url.
# File lib/warp_engine/admin_api/mcp.rb, line 46 def tool(operation, principal, base_url) ::MCP::Tool.define( name: operation.tool_name, title: operation.summary, description: description(operation), input_schema: operation.input_schema, annotations: { read_only_hint: operation.read?, destructive_hint: operation.destructive?, idempotent_hint: operation.idempotent?, open_world_hint: false } ) do |server_context: nil, **arguments| Mcp.call(operation, arguments, principal: principal, base_url: base_url) end end
Defines the MCP::Tool of operation, run as principal, with MCP hints from the operation.