Class: Ask::MCP::Server::Stdio
- Inherits:
-
Object
- Object
- Ask::MCP::Server::Stdio
- Defined in:
- lib/ask/mcp/server/stdio.rb
Overview
MCP server over stdio transport.
Constant Summary collapse
- MAX_RESULT_CACHE =
100- PROTOCOL_VERSION =
"2025-06-18"
Instance Attribute Summary collapse
-
#capabilities ⇒ Object
readonly
Returns the value of attribute capabilities.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#prompts ⇒ Object
readonly
Returns the value of attribute prompts.
-
#resources ⇒ Object
readonly
Returns the value of attribute resources.
-
#tools ⇒ Object
readonly
Returns the value of attribute tools.
Instance Method Summary collapse
-
#initialize(name:, tools: [], capabilities: {}, resources: {}, prompts: {}, debug: false, tool_timeout: nil) ⇒ Stdio
constructor
A new instance of Stdio.
- #running? ⇒ Boolean
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(name:, tools: [], capabilities: {}, resources: {}, prompts: {}, debug: false, tool_timeout: nil) ⇒ Stdio
Returns a new instance of Stdio.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/ask/mcp/server/stdio.rb', line 16 def initialize(name:, tools: [], capabilities: {}, resources: {}, prompts: {}, debug: false, tool_timeout: nil) @name = name @capabilities = capabilities @resources = resources @prompts = prompts @debug = debug @tool_timeout = tool_timeout @adapter = Adapters::ToolServer.new(tools || []) @initialized = false @running = false @shutdown_requested = false @result_cache = {} end |
Instance Attribute Details
#capabilities ⇒ Object (readonly)
Returns the value of attribute capabilities.
14 15 16 |
# File 'lib/ask/mcp/server/stdio.rb', line 14 def capabilities @capabilities end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
14 15 16 |
# File 'lib/ask/mcp/server/stdio.rb', line 14 def name @name end |
#prompts ⇒ Object (readonly)
Returns the value of attribute prompts.
14 15 16 |
# File 'lib/ask/mcp/server/stdio.rb', line 14 def prompts @prompts end |
#resources ⇒ Object (readonly)
Returns the value of attribute resources.
14 15 16 |
# File 'lib/ask/mcp/server/stdio.rb', line 14 def resources @resources end |
#tools ⇒ Object (readonly)
Returns the value of attribute tools.
14 15 16 |
# File 'lib/ask/mcp/server/stdio.rb', line 14 def tools @tools end |
Instance Method Details
#running? ⇒ Boolean
66 67 68 |
# File 'lib/ask/mcp/server/stdio.rb', line 66 def running? @running end |
#start ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/ask/mcp/server/stdio.rb', line 32 def start @running = true $stdout.sync = true # Graceful shutdown: on TERM/HUP, set flag and close stdin to # unblock the read loop so the current tool call can finish. trap("TERM") { graceful_shutdown } trap("HUP") { graceful_shutdown } debug_log "Server starting: #{@name} (PID #{Process.pid})" debug_log "Tools: #{@adapter.definitions.map { |d| d[:name] }.join(', ')}" while @running && !@shutdown_requested && (line = $stdin.gets) line = line.strip next if line.empty? process_line(line) end debug_log "stdin closed — exiting" rescue Errno::EBADF, IOError # stdin closed externally (e.g. from trap handler) rescue SignalException # SIGTERM or SIGHUP during blocked read ensure @running = false @shutdown_requested = false trap("TERM", "DEFAULT") trap("HUP", "DEFAULT") end |
#stop ⇒ Object
62 63 64 |
# File 'lib/ask/mcp/server/stdio.rb', line 62 def stop @running = false end |