-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMCP Basics .txt
More file actions
120 lines (99 loc) · 3.57 KB
/
MCP Basics .txt
File metadata and controls
120 lines (99 loc) · 3.57 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
Model Context Protocol Basics
[ Overview ]
What is the Model Context Protocol (MCP)?
MCP is an abstraction framework that functions as an api for an api. In other words, it allows me to define an interface upon which I can connect agents to in order to talk to many different backends (.e.g databases, third party applications such as Slack, or other services)
[ Components ]
There are two components in the Model Context Protocol framework
Client ----> Server ----> Service (slack, database, another API)
Client - functions as the agent making the remote call to the service
Server - defines the functions (contracts) that form the rules of engagement for being able to interface with the service it is abstracting; this "server" is essentially code that contains functions that are called by the client. They are no different from modules that are apart of a project; servers accepts request from clients using SSERs (server sent event requests)
[ Examples ]
Local Typescript Based MCP Server Loaded in Cursor (MCP Client)
# src/index.ts
// Create server instance
const server = new McpServer({
name: "weather",
version: "1.0.0",
});
// Register weather tools
server.tool(
"get-forecast", // tool name
"Get weather forecast for a location", // tool description
// description of parameters are very important for the LLM to know what to extract from the prompt to use as arguments to the tool function call
{
latitude: z.number().min(-90).max(90).describe("Latitude of the location"),
longitude: z
.number()
.min(-180)
.max(180)
.describe("Longitude of the location"),
},
async ({ latitude, longitude }) => {
// Get grid point data
const pointsUrl = `${NWS_API_BASE}/points/${latitude.toFixed(4)},${longitude.toFixed(4)}`;
const pointsData = await makeNWSRequest<PointsResponse>(pointsUrl);
if (!pointsData) {
return {
content: [
{
type: "text",
text: `Failed to retrieve grid point data for coordinates: ${latitude}, ${longitude}. This location may not be supported by the NWS API (only US locations are supported).`,
},
],
};
}
const forecastUrl = pointsData.properties?.forecast;
if (!forecastUrl) {
return {
content: [
{
type: "text",
text: "Failed to get forecast URL from grid point data",
},
],
};
}
// Get forecast data
const forecastData = await makeNWSRequest<ForecastResponse>(forecastUrl);
if (!forecastData) {
return {
content: [
{
type: "text",
text: "Failed to retrieve forecast data",
},
],
};
}
const periods = forecastData.properties?.periods || [];
if (periods.length === 0) {
return {
content: [
{
type: "text",
text: "No forecast periods available",
},
],
};
}
// Format forecast periods
const formattedForecast = periods.map((period: ForecastPeriod) =>
[
`${period.name || "Unknown"}:`,
`Temperature: ${period.temperature || "Unknown"}°${period.temperatureUnit || "F"}`,
`Wind: ${period.windSpeed || "Unknown"} ${period.windDirection || ""}`,
`${period.shortForecast || "No forecast available"}`,
"---",
].join("\n"),
);
const forecastText = `Forecast for ${latitude}, ${longitude}:\n\n${formattedForecast.join("\n")}`;
return {
content: [
{
type: "text",
text: forecastText,
},
],
};
},
);