Class: Pikuri::Mcp::Servers::Connect

Inherits:
Tool
  • Object
show all
Defined in:
lib/pikuri/mcp/servers.rb

Overview

The mcp_connect tool. Private inner class, built only by #build_mcp_connect_tool. Tracks its own activation Set so re-connecting an active server returns a recoverable "Error: ..." (the tools are already in the toolset). Each agent owns its own instance and set — activation never propagates parent↔sub-agent (the no-inherit rule, SubAgent::SubAgentTool).

Sharing: P_one_agent — the activation Set answers "already connected" for one toolset, and the Agent::ExtensionContext it registers through belongs to one agent. Shared, it would tell agent B a server is connected while B's toolset lacks the tools. The Pikuri::Mcp::Servers behind it is the shareable half.

Constant Summary collapse

DESCRIPTION =
<<~DESC
  Connect to an MCP (Model Context Protocol) server, adding its tools to your toolset for the rest of this conversation.

  Usage:
  - The list of available server ids and their descriptions is in your system prompt under `<available_mcps>`.
  - Call with a single `id` that matches one of those entries. The server's tools become callable immediately after, namespaced as `<id>__<tool_name>` in your toolset.
  - On `Error: server '...' is already connected`, do NOT retry — the tools are already in your toolset; call them directly.
  - On `Error: unknown MCP server id ...`, pick an id that actually appears in `<available_mcps>`.
DESC

Instance Method Summary collapse

Constructor Details

#initialize(servers:, ctx:) ⇒ Connect

Returns a new instance of Connect.

Parameters:

  • servers (Servers)

    shared runtime this tool resolves ids against and registers tools through.

  • ctx (Pikuri::Agent::ExtensionContext)

    context of the agent into whose chat the activated tools are registered.



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/pikuri/mcp/servers.rb', line 398

def initialize(servers:, ctx:)
  activated = Set.new

  super(
    name: 'mcp_connect',
    description: DESCRIPTION,
    parameters: Pikuri::Tool::Parameters.build { |p|
      p.required_string :id,
                        'Id of the MCP server to connect to. ' \
                        'Must match one of the ids listed under ' \
                        '`<available_mcps>` in the system prompt, e.g. "maven-tools".'
    },
    execute: lambda { |id:|
      unless servers.live_ids.include?(id)
        available = servers.live_ids.empty? ? 'none' : servers.live_ids.join(', ')
        next "Error: unknown MCP server id #{id.inspect}. Available: #{available}."
      end

      if activated.include?(id)
        next "Error: server '#{id}' is already connected; its tools are in your toolset, call them directly."
      end

      count = servers.register_tools_with_agent(id, ctx)
      activated << id
      "Connected to MCP server '#{id}'. Added #{count} tool#{'s' if count != 1} to your toolset."
    }
  )
end