-
Notifications
You must be signed in to change notification settings - Fork 309
Add Learn MCP Server tool-calling sample #642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pdebruin
wants to merge
4
commits into
microsoft:main
Choose a base branch
from
pdebruin:pdebruin/learn-mcp-tool-calling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
34cfc64
Add Learn MCP Server tool-calling sample
pdebruin a750a02
Add C# Learn MCP Server tool-calling sample
pdebruin 4fcdc75
Add C# Learn MCP Server sample and harden JSON parsing
pdebruin 9376e63
Update samples/js/learn-mcp-tool-calling/app.js
pdebruin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,244 @@ | ||
| // Foundry Local + Learn MCP Server: Local AI Doc Assistant | ||
| // Uses Foundry Local for on-device inference and Learn MCP Server for doc retrieval. | ||
|
|
||
| import { FoundryLocalManager } from 'foundry-local-sdk'; | ||
| import * as readline from 'readline'; | ||
|
|
||
| // --- MCP endpoint --- | ||
| const MCP_ENDPOINT = 'https://learn.microsoft.com/api/mcp'; | ||
|
|
||
| // --- Tool definitions (OpenAI function-calling schema) --- | ||
| const tools = [ | ||
| { | ||
| type: 'function', | ||
| function: { | ||
| name: 'search_docs', | ||
| description: 'Search Microsoft Learn documentation for a given query. Returns relevant documentation content with titles and URLs. Use this tool whenever the user asks about a Microsoft product, service, SDK, API, or technology.', | ||
| parameters: { | ||
| type: 'object', | ||
| properties: { | ||
| query: { | ||
| type: 'string', | ||
| description: 'The search query about a Microsoft product or technology' | ||
| } | ||
| }, | ||
| required: ['query'] | ||
| } | ||
| } | ||
| } | ||
| ]; | ||
|
|
||
| // --- Tool implementation: call Learn MCP Server --- | ||
| async function searchDocs(query) { | ||
| console.log(` [Searching Learn MCP Server for: "${query}"]`); | ||
|
|
||
| // MCP uses JSON-RPC over streamable HTTP | ||
| const response = await fetch(MCP_ENDPOINT, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| 'Accept': 'application/json, text/event-stream' | ||
| }, | ||
| body: JSON.stringify({ | ||
| jsonrpc: '2.0', | ||
| id: 1, | ||
| method: 'tools/call', | ||
| params: { | ||
| name: 'microsoft_docs_search', | ||
| arguments: { query } | ||
| } | ||
| }) | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| return { error: `MCP request failed: ${response.status} ${response.statusText}` }; | ||
| } | ||
|
|
||
| const contentType = response.headers.get('content-type') || ''; | ||
|
|
||
| // Handle SSE/streaming response | ||
| if (contentType.includes('text/event-stream')) { | ||
| const text = await response.text(); | ||
| const lines = text.split('\n'); | ||
| for (const line of lines) { | ||
| if (line.startsWith('data: ')) { | ||
| try { | ||
| const data = JSON.parse(line.slice(6)); | ||
| if (data.result) { | ||
| return formatSearchResults(data.result); | ||
| } | ||
| } catch { /* skip non-JSON lines */ } | ||
| } | ||
| } | ||
| return { error: 'No result found in SSE response' }; | ||
| } | ||
|
|
||
| // Handle direct JSON response | ||
| const data = await response.json(); | ||
| if (data.result) { | ||
| return formatSearchResults(data.result); | ||
| } | ||
| return { error: 'Unexpected response format', raw: JSON.stringify(data).slice(0, 500) }; | ||
| } | ||
|
|
||
| function formatSearchResults(result) { | ||
| // MCP tool results come as content arrays | ||
| const content = result.content || []; | ||
| const results = []; | ||
|
|
||
| for (const item of content) { | ||
| if (item.type === 'text') { | ||
| // The text may be a JSON string containing search results | ||
| try { | ||
| const parsed = JSON.parse(item.text); | ||
| if (parsed.results && Array.isArray(parsed.results)) { | ||
| for (const r of parsed.results.slice(0, 3)) { | ||
| let entry = `## ${r.title}`; | ||
| if (r.contentUrl) entry += `\nSource: ${r.contentUrl}`; | ||
| entry += `\n${r.content}`; | ||
| results.push(entry); | ||
| } | ||
| continue; | ||
| } | ||
| } catch { /* not JSON, use as-is */ } | ||
| results.push(item.text); | ||
| } | ||
| } | ||
|
|
||
| if (results.length === 0) { | ||
| return { message: 'No documentation found for this query.' }; | ||
| } | ||
|
|
||
| // Truncate to ~2000 chars to fit in model context window | ||
| let combined = results.join('\n\n---\n\n'); | ||
| if (combined.length > 2000) { | ||
| combined = combined.slice(0, 2000) + '\n\n[Truncated]'; | ||
| } | ||
|
|
||
| return { | ||
| documentation: combined, | ||
| source: 'Microsoft Learn (learn.microsoft.com)' | ||
| }; | ||
| } | ||
|
|
||
| const toolFunctions = { | ||
| search_docs: async (args) => searchDocs(args.query) | ||
| }; | ||
|
|
||
| // --- Tool-calling loop --- | ||
| async function processToolCalls(messages, response, chatClient) { | ||
| let choice = response.choices[0]?.message; | ||
|
|
||
| while (choice?.tool_calls?.length > 0) { | ||
| messages.push(choice); | ||
|
|
||
| for (const toolCall of choice.tool_calls) { | ||
| const functionName = toolCall.function.name; | ||
| const args = JSON.parse(toolCall.function.arguments); | ||
| console.log(` Tool call: ${functionName}(${JSON.stringify(args)})`); | ||
|
|
||
| const fn = toolFunctions[functionName]; | ||
| if (!fn) { | ||
| messages.push({ | ||
| role: 'tool', | ||
| tool_call_id: toolCall.id, | ||
| content: JSON.stringify({ error: `Unknown tool: ${functionName}` }) | ||
| }); | ||
| continue; | ||
| } | ||
|
|
||
| const result = await fn(args); | ||
| messages.push({ | ||
| role: 'tool', | ||
| tool_call_id: toolCall.id, | ||
| content: JSON.stringify(result) | ||
| }); | ||
| } | ||
|
|
||
| // Let model answer naturally on follow-up (don't force tool_choice) | ||
| const savedToolChoice = chatClient.settings.toolChoice; | ||
| chatClient.settings.toolChoice = undefined; | ||
| response = await chatClient.completeChat(messages, tools); | ||
| chatClient.settings.toolChoice = savedToolChoice; | ||
| choice = response.choices[0]?.message; | ||
| } | ||
|
|
||
| return choice?.content ?? ''; | ||
| } | ||
|
|
||
| // --- Main application --- | ||
| const manager = FoundryLocalManager.create({ | ||
| appName: 'learn_doc_assistant', | ||
| logLevel: 'info' | ||
| }); | ||
|
|
||
| let currentEp = ''; | ||
| await manager.downloadAndRegisterEps((epName, percent) => { | ||
| if (epName !== currentEp) { | ||
| if (currentEp !== '') process.stdout.write('\n'); | ||
| currentEp = epName; | ||
| } | ||
| process.stdout.write(`\r ${epName.padEnd(30)} ${percent.toFixed(1).padStart(5)}%`); | ||
| }); | ||
| if (currentEp !== '') process.stdout.write('\n'); | ||
|
|
||
| const model = await manager.catalog.getModel('phi-4-mini'); | ||
|
|
||
| await model.download((progress) => { | ||
| process.stdout.write(`\rDownloading model: ${progress.toFixed(2)}%`); | ||
| }); | ||
| console.log('\nModel downloaded.'); | ||
|
|
||
| await model.load(); | ||
| console.log('Model loaded and ready.'); | ||
|
|
||
| const chatClient = model.createChatClient(); | ||
| chatClient.settings.toolChoice = { type: 'required' }; | ||
|
|
||
| const messages = [ | ||
| { | ||
| role: 'system', | ||
| content: | ||
| 'You are a Microsoft Learn documentation assistant. ' + | ||
| 'You MUST ALWAYS call the search_docs tool before answering ANY question. ' + | ||
| 'NEVER answer from your own knowledge. ' + | ||
| 'If the user asks about any Microsoft product, service, or technology, call search_docs first. ' + | ||
| 'Base your answer ONLY on the documentation returned by the tool. ' + | ||
| 'Include source URLs when available.' | ||
| } | ||
| ]; | ||
|
|
||
| const rl = readline.createInterface({ | ||
| input: process.stdin, | ||
| output: process.stdout | ||
| }); | ||
|
|
||
| const askQuestion = (prompt) => | ||
| new Promise((resolve) => rl.question(prompt, resolve)); | ||
|
|
||
| console.log( | ||
| '\nLearn Doc Assistant ready! Ask about any Microsoft product or technology.' | ||
| ); | ||
| console.log('Type \'quit\' to exit.\n'); | ||
|
|
||
| while (true) { | ||
| const userInput = await askQuestion('You: '); | ||
| if ( | ||
| userInput.trim().toLowerCase() === 'quit' || | ||
| userInput.trim().toLowerCase() === 'exit' | ||
| ) { | ||
| break; | ||
| } | ||
|
|
||
| messages.push({ role: 'user', content: userInput }); | ||
|
|
||
| const response = await chatClient.completeChat(messages, tools); | ||
| const answer = await processToolCalls(messages, response, chatClient); | ||
|
|
||
| messages.push({ role: 'assistant', content: answer }); | ||
| console.log(`\nAssistant: ${answer}\n`); | ||
| } | ||
|
|
||
| await model.unload(); | ||
| console.log('Model unloaded. Goodbye!'); | ||
| rl.close(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "name": "learn-mcp-tool-calling", | ||
| "version": "1.0.0", | ||
| "type": "module", | ||
| "main": "app.js", | ||
| "scripts": { | ||
| "start": "node app.js" | ||
| }, | ||
| "dependencies": { | ||
| "foundry-local-sdk": "latest" | ||
| }, | ||
| "optionalDependencies": { | ||
| "foundry-local-sdk-winml": "latest" | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.