Class: Ask::MCP::Server::Stdio

Inherits:
Object
  • Object
show all
Defined in:
lib/ask/mcp/server/stdio.rb

Overview

MCP server over stdio transport.

Constant Summary collapse

MAX_RESULT_CACHE =
100

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, tools: [], capabilities: {}, resources: {}, prompts: {}, debug: false, tool_timeout: nil) ⇒ Stdio

Returns a new instance of Stdio.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ask/mcp/server/stdio.rb', line 15

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

#capabilitiesObject (readonly)

Returns the value of attribute capabilities.



13
14
15
# File 'lib/ask/mcp/server/stdio.rb', line 13

def capabilities
  @capabilities
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/ask/mcp/server/stdio.rb', line 13

def name
  @name
end

#promptsObject (readonly)

Returns the value of attribute prompts.



13
14
15
# File 'lib/ask/mcp/server/stdio.rb', line 13

def prompts
  @prompts
end

#resourcesObject (readonly)

Returns the value of attribute resources.



13
14
15
# File 'lib/ask/mcp/server/stdio.rb', line 13

def resources
  @resources
end

#toolsObject (readonly)

Returns the value of attribute tools.



13
14
15
# File 'lib/ask/mcp/server/stdio.rb', line 13

def tools
  @tools
end

Instance Method Details

#running?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/ask/mcp/server/stdio.rb', line 65

def running?
  @running
end

#startObject



31
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
# File 'lib/ask/mcp/server/stdio.rb', line 31

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

#stopObject



61
62
63
# File 'lib/ask/mcp/server/stdio.rb', line 61

def stop
  @running = false
end