|
| 1 | +import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; |
| 2 | +import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; |
| 3 | +import { z } from 'zod'; |
| 4 | +import { fetchDocsList, fetchDocsPost } from './docs'; |
| 5 | +import { getRelevanceScore } from '@/utils/get-score'; |
| 6 | + |
| 7 | +export async function docsMcpServer() { |
| 8 | + const server = new McpServer({ |
| 9 | + name: 'langbase-docs-server', |
| 10 | + version: '0.1.0' |
| 11 | + }); |
| 12 | + |
| 13 | + server.tool( |
| 14 | + 'docs-route-finder', |
| 15 | + "Searches through all available documentation routes and returns relevant paths based on the user's query. This tool helps navigate the documentation by finding the most appropriate sections that match the search criteria.", |
| 16 | + { |
| 17 | + query: z.string() |
| 18 | + .describe(`A refined search term extracted from the user's question. |
| 19 | + For example, if user asks 'How do I create a pipe?', the query would be 'SDK Pipe'. |
| 20 | + This should be the specific concept or topic to search for in the documentation. |
| 21 | + Treat keyword add as create if user ask for Eg. 'How do I add memory to pipe?' the query should be 'create memory'`) |
| 22 | + }, |
| 23 | + async ({ query }) => { |
| 24 | + const docs = await fetchDocsList(); |
| 25 | + // search through the docs and return the most relevent path based on the query |
| 26 | + const docLines = docs.split('\n').filter(line => line.trim()); |
| 27 | + |
| 28 | + // Score and sort the documentation entries |
| 29 | + const scoredDocs = docLines |
| 30 | + .map(line => ({ |
| 31 | + line, |
| 32 | + score: getRelevanceScore(line, query) |
| 33 | + })) |
| 34 | + .sort((a, b) => b.score - a.score) |
| 35 | + .filter(doc => doc.score > 0) |
| 36 | + .slice(0, 3); // Get top 3 most relevant results |
| 37 | + |
| 38 | + const hasRelevantDocs = scoredDocs.length === 0; |
| 39 | + |
| 40 | + if (hasRelevantDocs) { |
| 41 | + return { |
| 42 | + content: [ |
| 43 | + { |
| 44 | + type: 'text', |
| 45 | + text: |
| 46 | + 'No relevant documentation found for the query: ' + |
| 47 | + query |
| 48 | + } |
| 49 | + ] |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + const results = scoredDocs.map(doc => doc.line).join('\n'); |
| 54 | + |
| 55 | + return { |
| 56 | + content: [ |
| 57 | + { |
| 58 | + type: 'text', |
| 59 | + text: results |
| 60 | + } |
| 61 | + ] |
| 62 | + }; |
| 63 | + } |
| 64 | + ); |
| 65 | + |
| 66 | + server.tool( |
| 67 | + 'sdk-documentation-fetcher', |
| 68 | + 'Fetches detailed SDK documentation, specializing in implementation guides for core features like pipes, memory, and tools. This is the primary source for the latest SDK documentation and should be consulted first for questions about creating or implementing SDK components. Use this tool for detailed step-by-step instructions on building pipes, configuring memory systems, and developing custom tools.', |
| 69 | + { |
| 70 | + url: z |
| 71 | + .string() |
| 72 | + .describe( |
| 73 | + 'URL of a specific SDK page to fetch. Format: /sdk/...' |
| 74 | + ) |
| 75 | + }, |
| 76 | + async ({ url }) => { |
| 77 | + const content = await fetchDocsPost( |
| 78 | + `https://langbase.com/docs${url}` |
| 79 | + ); |
| 80 | + return { |
| 81 | + content: [ |
| 82 | + { |
| 83 | + type: 'text', |
| 84 | + text: content |
| 85 | + } |
| 86 | + ] |
| 87 | + }; |
| 88 | + } |
| 89 | + ); |
| 90 | + |
| 91 | + server.tool( |
| 92 | + 'examples-tool', |
| 93 | + 'Fetches code examples and sample implementations from the documentation. Use this tool when users specifically request examples, sample code, or implementation demonstrations. This tool provides practical code snippets and complete working examples that demonstrate how to implement various features.', |
| 94 | + { |
| 95 | + url: z |
| 96 | + .string() |
| 97 | + .describe( |
| 98 | + 'URL of a specific examples page to fetch. Format: /examples/...' |
| 99 | + ) |
| 100 | + }, |
| 101 | + async ({ url }) => { |
| 102 | + const content = await fetchDocsPost( |
| 103 | + `https://langbase.com/docs${url}` |
| 104 | + ); |
| 105 | + return { |
| 106 | + content: [ |
| 107 | + { |
| 108 | + type: 'text', |
| 109 | + text: content |
| 110 | + } |
| 111 | + ] |
| 112 | + }; |
| 113 | + } |
| 114 | + ); |
| 115 | + |
| 116 | + server.tool( |
| 117 | + 'guide-tool', |
| 118 | + 'Fetches detailed guides and tutorials from the documentation. Use this tool when users explicitly request guides, tutorials, or how-to content. This tool provides step-by-step instructions and practical examples for implementing various features.', |
| 119 | + { |
| 120 | + url: z |
| 121 | + .string() |
| 122 | + .describe( |
| 123 | + 'URL of a specific guide page to fetch. Format: /guides/...' |
| 124 | + ) |
| 125 | + }, |
| 126 | + async ({ url }) => { |
| 127 | + const content = await fetchDocsPost( |
| 128 | + `https://langbase.com/docs${url}` |
| 129 | + ); |
| 130 | + return { |
| 131 | + content: [ |
| 132 | + { |
| 133 | + type: 'text', |
| 134 | + text: content |
| 135 | + } |
| 136 | + ] |
| 137 | + }; |
| 138 | + } |
| 139 | + ); |
| 140 | + |
| 141 | + server.tool( |
| 142 | + 'api-reference-tool', |
| 143 | + 'Fetches API reference documentation. Use this tool ONLY when the user explicitly asks about API endpoints, REST API calls, or programmatically creating/updating/deleting resources (like pipes, memory, etc.) through the API interface. For general SDK implementation questions, use the sdk-documentation-fetcher instead.', |
| 144 | + { |
| 145 | + url: z |
| 146 | + .string() |
| 147 | + .describe( |
| 148 | + 'URL of a specific api-reference page to fetch. Format: /api-reference/...' |
| 149 | + ) |
| 150 | + }, |
| 151 | + async ({ url }) => { |
| 152 | + const content = await fetchDocsPost( |
| 153 | + `https://langbase.com/docs${url}` |
| 154 | + ); |
| 155 | + return { |
| 156 | + content: [ |
| 157 | + { |
| 158 | + type: 'text', |
| 159 | + text: content |
| 160 | + } |
| 161 | + ] |
| 162 | + }; |
| 163 | + } |
| 164 | + ); |
| 165 | + |
| 166 | + async function main() { |
| 167 | + const transport = new StdioServerTransport(); |
| 168 | + |
| 169 | + try { |
| 170 | + await server.connect(transport); |
| 171 | + } catch (error) { |
| 172 | + console.error('Error connecting to transport:', error); |
| 173 | + process.exit(1); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + main().catch(error => { |
| 178 | + console.error('Something went wrong:', error); |
| 179 | + process.exit(1); |
| 180 | + }); |
| 181 | +} |
0 commit comments