55 lines
1.8 KiB
Markdown
55 lines
1.8 KiB
Markdown
|
|
# Task ID: 51
|
||
|
|
|
||
|
|
**Title:** Implement keyword extraction from tool calls
|
||
|
|
|
||
|
|
**Status:** done
|
||
|
|
|
||
|
|
**Dependencies:** None
|
||
|
|
|
||
|
|
**Priority:** medium
|
||
|
|
|
||
|
|
**Description:** Create a service that extracts keywords from tool names and arguments for the intercept fallback path when clients skip begin_session.
|
||
|
|
|
||
|
|
**Details:**
|
||
|
|
|
||
|
|
1. Create `/src/mcplocal/src/services/keyword-extractor.service.ts`:
|
||
|
|
```typescript
|
||
|
|
export class KeywordExtractorService {
|
||
|
|
extractKeywords(toolName: string, args: Record<string, any>): string[] {
|
||
|
|
const keywords: string[] = [];
|
||
|
|
|
||
|
|
// Extract from tool name (split on / and -)
|
||
|
|
// e.g., "home-assistant/get_entities" -> ["home", "assistant", "get", "entities"]
|
||
|
|
keywords.push(...this.extractFromName(toolName));
|
||
|
|
|
||
|
|
// Extract from argument values
|
||
|
|
// e.g., { domain: "light", entity_id: "light.kitchen" } -> ["light", "kitchen"]
|
||
|
|
keywords.push(...this.extractFromArgs(args));
|
||
|
|
|
||
|
|
// Deduplicate and sanitize
|
||
|
|
return [...new Set(keywords.map(k => this.sanitize(k)))];
|
||
|
|
}
|
||
|
|
|
||
|
|
private sanitize(keyword: string): string {
|
||
|
|
// Remove special characters, lowercase, limit length
|
||
|
|
return keyword.toLowerCase().replace(/[^a-z0-9]/g, '').slice(0, 50);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
2. Handle various argument types: strings, arrays, nested objects
|
||
|
|
|
||
|
|
3. Prevent injection by sanitizing extracted keywords
|
||
|
|
|
||
|
|
**Test Strategy:**
|
||
|
|
|
||
|
|
- Unit test: Extracts keywords from tool name with /
|
||
|
|
- Unit test: Extracts keywords from tool name with -
|
||
|
|
- Unit test: Extracts keywords from string argument values
|
||
|
|
- Unit test: Extracts keywords from array argument values
|
||
|
|
- Unit test: Handles nested object arguments
|
||
|
|
- Unit test: Sanitizes special characters
|
||
|
|
- Unit test: Deduplicates keywords
|
||
|
|
- Unit test: Handles empty arguments
|
||
|
|
- Unit test: Limits keyword length to prevent abuse
|