Skip to content

Commit 5a04324

Browse files
softchrisChrisCopilot
authored
docs: lesson 7 + 8 on mcp (#108)
* first draft * adding support for client with llm * adding lesson 7 * completing lesson 7 * completing lesson 8 * finishing lesson 8 * first commit on lesson 9 * Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Apply suggestions from code review * adding grammar and spelling improvements * adding more text to ch9 * adding final touches to ch8, als more content for ch9 * adding new lesson entries to README, removing ch9 as it will live in separate branch * changes based on review * small fixes --------- Co-authored-by: Chris <chnoring@microsoft.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 9d0393d commit 5a04324

40 files changed

Lines changed: 5801 additions & 0 deletions

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ Throughout this course you'll find many code examples and exercises, so we encou
9191
| 4 | [Structured output](./lessons/04-structured-output) | Learn structured output, how to extract data from prompts, and present it in various formats, such as JSON, for easier consumption. |
9292
| 5 | [Retrieval augmented generation (RAG)](./lessons/05-rag) | Learn the basics of RAG, how to integrate external data, and how to leverage it for more relevant, accurate AI responses. |
9393
| 6 | [Tool calling/Function calling](./lessons/06-tool-calling) | Learn how to give your LLM extra capbilities, bring your own functions |
94+
| 7 | [MCP, Model Context Protocol ](./lessons/07-mcp/) | Teaches how to get started with MCP to standardize how to expose prompts, resources and tools |
95+
| 8 | [MCP advanced ](./lessons/08-mcp-advanced/README.md) | Learn how to improve your MCP app by improving clients with LLM and more |
9496

9597
New lessons will be added to the course over time, so stay tuned!
9698

app/public/characters.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@
4444
"avatar": "ada-avatar.jpeg",
4545
"voice": 2
4646
},
47+
{
48+
"title": "Scipio Africanus",
49+
"name": "scipio",
50+
"description": "You are Scipio Africanus, a Roman general known for defeating Hannibal in the Second Punic War. Limit your responses to only the time you live in, you don't know anything else. You only want to talk about military strategy and possibly new ideas you have.",
51+
"page": "scipio.html",
52+
"image": "scipio.png",
53+
"avatar": "scipio-avatar.jpeg",
54+
"voice": 1
55+
},
56+
{
57+
"title": "Hedy Lamarr",
58+
"name": "hedy",
59+
"description": "You are Hedy Lamarr, a famous actress and inventor. Limit your responses to only the time you live in, you don't know anything else. You only want to talk about your inventions and possibly new ideas you have.",
60+
"page": "hedy.html",
61+
"image": "hedy.jpeg",
62+
"avatar": "hedy-avatar.jpeg",
63+
"voice": 2
64+
}
4765
{
4866
"title": "Amelia Earhart",
4967
"name": "ada",

app/public/images/hedy-avatar.jpeg

141 KB
Loading

app/public/images/hedy.jpeg

133 KB
Loading
99.2 KB
Loading

app/public/images/scipio.png

1.86 MB
Loading

lessons/07-mcp/README.md

Lines changed: 494 additions & 0 deletions
Large diffs are not rendered by default.

lessons/07-mcp/assets/scipio.png

1.86 MB
Loading
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
2+
import { CallToolResultSchema } from "@modelcontextprotocol/sdk/types.js";
3+
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
4+
import { OpenAI } from 'openai';
5+
const transport = new StdioClientTransport({
6+
command: "node",
7+
args: ["build/index.js"]
8+
});
9+
const client = new Client({
10+
name: "example-client",
11+
version: "1.0.0"
12+
});
13+
await client.connect(transport);
14+
function toToolSchema(method, schema) {
15+
return {
16+
name: method,
17+
description: `This is a tool that does ${method}`,
18+
parameters: schema,
19+
};
20+
}
21+
// list tools
22+
const { tools } = await client.listTools();
23+
tools.forEach((tool) => {
24+
console.log(`Tool: ${tool.name}`);
25+
console.log(`Description: ${tool.description}`);
26+
console.log(`Input schema: ${JSON.stringify(tool.inputSchema)}`);
27+
});
28+
const toolsForLLM = tools.map((tool) => {
29+
return toToolSchema(tool.name, tool.inputSchema);
30+
});
31+
console.log("Schema for tool", JSON.stringify(toolsForLLM, null, 2));
32+
// TODO feed this to an llm as functions
33+
const openai = new OpenAI({
34+
baseURL: "https://models.inference.ai.azure.com", // might need to change to this url in the future: https://models.github.ai/inference
35+
apiKey: process.env.GITHUB_TOKEN,
36+
});
37+
const messages = [
38+
{
39+
role: "system",
40+
content: `You are a helpful assistant. You can call functions to perform tasks. Make sure to parse the function call and arguments correctly.`
41+
}, {
42+
role: "user",
43+
content: "Add 5 and 10",
44+
name: "example-user" // Adding the required 'name' property for user role
45+
}
46+
];
47+
openai.chat.completions.create({
48+
model: 'gpt-4o',
49+
messages: messages,
50+
functions: toolsForLLM
51+
}).then((result) => {
52+
console.log("Result", result.choices[0].message);
53+
}).catch((error) => {
54+
console.error("Error:", error);
55+
});
56+
const result = await openai.chat.completions.create({
57+
model: 'gpt-4o',
58+
messages: messages,
59+
functions: toolsForLLM
60+
});
61+
for (const choice of result.choices) {
62+
// console.log("Result", choice.message);
63+
let functionCall = choice.message?.function_call;
64+
let functionName = functionCall?.name;
65+
console.log("Function call: ", functionName);
66+
let args = functionCall?.arguments;
67+
await client.callTool({
68+
name: functionName ?? "",
69+
arguments: typeof args === "string" ? JSON.parse(args) : args ?? {}
70+
}, CallToolResultSchema).then((result) => {
71+
console.log("Result from tool: ", result);
72+
});
73+
}
74+
// call tool
75+
// const result = await client.callTool({
76+
// name: "add",
77+
// arguments: {
78+
// a: 5,
79+
// b: 10
80+
// }
81+
// });
82+
// console.log(`Result: ${JSON.stringify(result)}`);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {};

0 commit comments

Comments
 (0)