Search Company
search_companySearch for Colombo Stock Exchange companies by name or symbol. Fuzzy matching returns the three closest matches for accurate results.
Instructions
Search for Colombo Stock Exchange companies by name or symbol. Uses fuzzy matching to return the 3 closest matches.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | Company name or symbol to search (e.g., 'JKH' or 'John Keells') |
Implementation Reference
- src/index.ts:345-374 (handler)The handler function for the 'search_company' tool. It takes a 'query' string, calls searchCompanies() for fuzzy matching, and returns the top 3 matching companies (id, symbol, name) as JSON.
async ({ query }) => { const results = searchCompanies(query); if (results.length === 0) { return { content: [{ type: "text", text: `No companies found matching "${query}". Try a different search term.` }] }; } const formattedResults = results.map(company => ({ id: company.id, symbol: company.symbol, name: company.name })); return { content: [{ type: "text", text: JSON.stringify({ query: query, count: results.length, companies: formattedResults, note: "Top 3 matches using fuzzy search" }, null, 2) }] }; } - src/index.ts:338-343 (schema)Schema and registration definition for the 'search_company' tool. Defines title, description, and input schema expecting a 'query' string (minimum 1 char).
{ title: "Search Company", description: "Search for Colombo Stock Exchange companies by name or symbol. Uses fuzzy matching to return the 3 closest matches.", inputSchema: { query: z.string().min(1).describe("Company name or symbol to search (e.g., 'JKH' or 'John Keells')") } - src/index.ts:336-375 (registration)Tool registration via server.registerTool() call with name 'search_company', schema, and handler function.
server.registerTool( "search_company", { title: "Search Company", description: "Search for Colombo Stock Exchange companies by name or symbol. Uses fuzzy matching to return the 3 closest matches.", inputSchema: { query: z.string().min(1).describe("Company name or symbol to search (e.g., 'JKH' or 'John Keells')") } }, async ({ query }) => { const results = searchCompanies(query); if (results.length === 0) { return { content: [{ type: "text", text: `No companies found matching "${query}". Try a different search term.` }] }; } const formattedResults = results.map(company => ({ id: company.id, symbol: company.symbol, name: company.name })); return { content: [{ type: "text", text: JSON.stringify({ query: query, count: results.length, companies: formattedResults, note: "Top 3 matches using fuzzy search" }, null, 2) }] }; } ); - src/index.ts:74-111 (helper)The searchCompanies() helper function. Accepts a query string, uses Levenshtein distance for fuzzy matching across company symbols, names, and individual name words, and returns the top 3 closest matches.
function searchCompanies(query: string): Company[] { const searchTerm = query.toLowerCase(); // Calculate similarity scores for all companies const companiesWithScores = companies.map(company => { const symbolLower = company.symbol.toLowerCase(); const nameLower = company.name.toLowerCase(); // Check for exact matches first (score 0 is best) if (symbolLower === searchTerm || nameLower === searchTerm) { return { company, score: 0 }; } // Check for substring matches (prioritize these) if (symbolLower.includes(searchTerm) || nameLower.includes(searchTerm)) { return { company, score: 1 }; } // Calculate fuzzy matching scores const symbolDistance = levenshteinDistance(searchTerm, symbolLower); const nameDistance = levenshteinDistance(searchTerm, nameLower); // Also check against individual words in company name const nameWords = nameLower.split(/\s+/); const wordDistances = nameWords.map(word => levenshteinDistance(searchTerm, word)); const minWordDistance = Math.min(...wordDistances); // Use the minimum distance as the score const minDistance = Math.min(symbolDistance, nameDistance, minWordDistance); return { company, score: minDistance }; }); // Sort by score (lower is better) and return top 3 companiesWithScores.sort((a, b) => a.score - b.score); return companiesWithScores.slice(0, 3).map(item => item.company); } - src/index.ts:46-72 (helper)The levenshteinDistance() helper function used by searchCompanies() to calculate fuzzy string similarity.
function levenshteinDistance(a: string, b: string): number { const matrix: number[][] = []; for (let i = 0; i <= b.length; i++) { matrix[i] = [i]; } for (let j = 0; j <= a.length; j++) { matrix[0][j] = j; } for (let i = 1; i <= b.length; i++) { for (let j = 1; j <= a.length; j++) { if (b.charAt(i - 1) === a.charAt(j - 1)) { matrix[i][j] = matrix[i - 1][j - 1]; } else { matrix[i][j] = Math.min( matrix[i - 1][j - 1] + 1, matrix[i][j - 1] + 1, matrix[i - 1][j] + 1 ); } } } return matrix[b.length][a.length]; }