-
Notifications
You must be signed in to change notification settings - Fork 1
Guide on MCP
hmdroh edited this page Jun 11, 2025
·
1 revision
You get a json where it has: json.jsonrpc === "2.0" json.method === 'initialized'
We create a sendResponse method
response = {
jsonrpc: '2.0',
id: id, // json.id
result: result,
};the result above should have this:
{
protocolVersion: "2025-03-26",
capabilities: {
tools: { listChanged: true },
},
serverInfo,
}
serverInfo info is json object has name and version: 1.0.0:
{name: "Coffee shop Server",
version: "1.0.0",
}
======================
npx @modelcontextprotocol/inspector
ui will open.
STDIO should be selected
command: node
arguments: location of the script ./dist/index.js
click connet and see if everything works fine:
const tools = [
{
name: "getDrinkNames",
description: "Get the names of the drinks in the shop",
inputSchema: { type: "object", properties: {} }
},
{
name: "getDrinkInfo",
description: "Get more info about a specific drink",
inputSchema: { type: "object", properties: { name: { type: "string" } }, required: ["name"] }
},
];
if (json.method === "tools/list") {
sendResponse(json.id, {
tools: tools.map((tool) => ({
name: tool.name,
description: tool.description,
inputSchema: tool.inputSchema,
})),
});
}Try it out and see if works.
Step 4: calling a tool: We add an execute to each tool Also note the error when the tool is not found.
const tools = [
{
name: "getDrinkNames",
description: "Get the names of the drinks in the shop",
inputSchema: { type: "object", properties: {} },
execute: async (args: any) => {
return {
content: [
{
type: "text",
text: JSON.stringify({ names: drinks.map((drink) => drink.name) }),
},
],
};
},
},
{
name: "getDrinkInfo",
description: "Get more info about a specific drink",
inputSchema: { type: "object", properties: { name: { type: "string" } }, required: ["name"] },
execute: async (args: any) => {
const drink = drinks.find((drink) => drink.name === args.name);
return {
content: [
{
type: "text",
text: JSON.stringify(drink || { error: "Drink not found" }),
},
],
};
},
},
];
if (json.method === "tools/call") {
const tool = tools.find((tool) => tool.name === json.params.name);
if (tool) {
const toolResponse = await tool.execute(json.params.arguments);
sendResponse(json.id, toolResponse);
} else {
sendResponse(json.id, {
error: {
code: -32602,
message: `MCP error -32602: Tool ${json.params.name} not found`,
},
});
}
}lets create menu:
const resources = [
{
uri: "menu://app", /// uri should match in below
name: "menu",
get: async () => { // is what gets returned from this func
return {
contents: [
{
uri: "menu://app", /// uri should match in above
text: JSON.stringify(drinks),
},
],
};
},
},
];also update the initialize method code:
if (json.method === "initialize") {
sendResponse(json.id, {
protocolVersion: "2025-03-26",
capabilities: {
tools: { listChanged: true },
resources: { listChanged: true }, // add list changed to tell if it change it should update
},
serverInfo,
});
}then you can add the method:
if (json.method === "resources/list") {
sendResponse(json.id, {
resources: resources.map((resource) => ({
uri: resource.uri,
name: resource.name,
})),
});
}
if (json.method === "resources/read") {
const uri = json.params.uri;
const resource = resources.find((resource) => resource.uri === uri);
if (resource) {
sendResponse(json.id, await resource.get());
} else {
sendResponse(json.id, {
error: { code: -32602, message: "Resource not found" },
});
}
}