|
| 1 | +module Mcp |
| 2 | + # Dispatches a single JSON-RPC 2.0 request (already parsed into a Hash with |
| 3 | + # string keys) to the appropriate MCP handler and returns a response Hash |
| 4 | + # ready to be serialized back to the client. |
| 5 | + module Server |
| 6 | + TOOLS = [ |
| 7 | + { |
| 8 | + 'name' => 'devdocs_list_docsets', |
| 9 | + 'description' => 'List documentation sets available on this DevDocs instance.', |
| 10 | + 'inputSchema' => { 'type' => 'object', 'properties' => {}, 'additionalProperties' => false }, |
| 11 | + }, |
| 12 | + { |
| 13 | + 'name' => 'devdocs_search', |
| 14 | + 'description' => 'Search entry names/paths within one downloaded DevDocs doc set.', |
| 15 | + 'inputSchema' => { |
| 16 | + 'type' => 'object', |
| 17 | + 'properties' => { |
| 18 | + 'slug' => { 'type' => 'string' }, |
| 19 | + 'query' => { 'type' => 'string' }, |
| 20 | + }, |
| 21 | + 'required' => %w(slug query), |
| 22 | + 'additionalProperties' => false, |
| 23 | + }, |
| 24 | + }, |
| 25 | + { |
| 26 | + 'name' => 'devdocs_get_page', |
| 27 | + 'description' => 'Fetch one entry from a DevDocs doc set as plain text.', |
| 28 | + 'inputSchema' => { |
| 29 | + 'type' => 'object', |
| 30 | + 'properties' => { |
| 31 | + 'slug' => { 'type' => 'string' }, |
| 32 | + 'path' => { 'type' => 'string' }, |
| 33 | + }, |
| 34 | + 'required' => %w(slug path), |
| 35 | + 'additionalProperties' => false, |
| 36 | + }, |
| 37 | + }, |
| 38 | + ].freeze |
| 39 | + |
| 40 | + def self.handle(request, app_settings) |
| 41 | + case request['method'] |
| 42 | + when 'initialize' |
| 43 | + respond(request, { |
| 44 | + 'protocolVersion' => '2024-11-05', |
| 45 | + 'capabilities' => { 'tools' => {} }, |
| 46 | + 'serverInfo' => { 'name' => 'devdocs-mcp', 'version' => '1.0.0' }, |
| 47 | + }) |
| 48 | + when 'tools/list' |
| 49 | + respond(request, { 'tools' => TOOLS }) |
| 50 | + when 'tools/call' |
| 51 | + call_tool(request, app_settings) |
| 52 | + else |
| 53 | + error(request, -32601, "Unsupported method: #{request['method']}") |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + def self.error(request, code, message) |
| 58 | + { 'jsonrpc' => '2.0', 'id' => request['id'], 'error' => { 'code' => code, 'message' => message } } |
| 59 | + end |
| 60 | + |
| 61 | + def self.call_tool(request, app_settings) |
| 62 | + params = request['params'] |
| 63 | + case params['name'] |
| 64 | + when 'devdocs_list_docsets' |
| 65 | + docsets = app_settings.docs.values |
| 66 | + as_text_result(request, docsets) |
| 67 | + when 'devdocs_search' |
| 68 | + entries = search_docset(app_settings, params['arguments']['slug'], params['arguments']['query']) |
| 69 | + as_text_result(request, entries) |
| 70 | + when 'devdocs_get_page' |
| 71 | + text = get_page(app_settings, params['arguments']['slug'], params['arguments']['path']) |
| 72 | + respond(request, { 'content' => [{ 'type' => 'text', 'text' => text }] }) |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + def self.get_page(app_settings, slug, path) |
| 77 | + db_path = File.join(app_settings.docs_path, slug, 'db.json') |
| 78 | + db = JSON.parse(File.read(db_path)) |
| 79 | + html = db[path] |
| 80 | + Nokogiri::HTML::DocumentFragment.parse(html).text.squeeze(' ').strip |
| 81 | + end |
| 82 | + |
| 83 | + def self.search_docset(app_settings, slug, query) |
| 84 | + index_path = File.join(app_settings.docs_path, slug, 'index.json') |
| 85 | + index = JSON.parse(File.read(index_path)) |
| 86 | + q = query.downcase |
| 87 | + index['entries'].select { |e| e['name'].downcase.include?(q) || e['path'].downcase.include?(q) } |
| 88 | + end |
| 89 | + |
| 90 | + def self.as_text_result(request, data) |
| 91 | + respond(request, { 'content' => [{ 'type' => 'text', 'text' => data.to_json }] }) |
| 92 | + end |
| 93 | + |
| 94 | + def self.respond(request, result) |
| 95 | + { 'jsonrpc' => '2.0', 'id' => request['id'], 'result' => result } |
| 96 | + end |
| 97 | + end |
| 98 | +end |
0 commit comments