forked from MicrosoftDocs/mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-search.ts
More file actions
36 lines (32 loc) · 1.34 KB
/
code-search.ts
File metadata and controls
36 lines (32 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { Command } from 'commander';
import type { CliContext } from '../context.js';
import { formatCodeSearchResults } from '../formatters/search-results.js';
import { resolveEndpoint } from '../utils/options.js';
import { ensureTrailingNewline } from '../utils/text.js';
interface CodeSearchCommandOptions {
language?: string;
json?: boolean;
}
export function registerCodeSearchCommand(program: Command, context: CliContext): void {
program
.command('code-search')
.description('Search Microsoft Learn code samples through the Learn MCP server.')
.argument('<query>', 'Search query.')
.option('--language <name>', 'Preferred language filter to pass to Learn.')
.option('--json', 'Output raw JSON instead of formatted text.')
.action(async (query: string, options: CodeSearchCommandOptions) => {
const endpoint = resolveEndpoint(program.opts<{ endpoint?: string }>().endpoint, context.env);
const client = context.createClient({
endpoint,
clientName: 'mslearn',
clientVersion: context.version,
});
try {
const payload = await client.searchCodeSamples(query, options.language);
const output = options.json ? payload : formatCodeSearchResults(payload);
context.writeOut(ensureTrailingNewline(output));
} finally {
await client.close();
}
});
}