What is MCP?

by Siratul804

133 downloads
Not rated
GitHub

About

A simple mcp server demonstrating resources, tools, and prompts

Details

Author
Siratul804
Downloads
133
Categories
Other, AI

- Expose static or parameterized data via resources (similar to GET endpoints).
- Provide computational/side-effect actions via tools (similar to POST endpoints).
- Create reusable prompt templates for LLM interactions.
- Built for the Model Context Protocol standard.
- Written in TypeScript for Node.js.

Use the provided TypeScript code examples to define resources, tools, and prompts. Import the necessary MCP library and create a server instance using server.resource(), server.tool(), and server.prompt().

What is MCP?

The Model Context Protocol (MCP) lets you build servers that expose data and functionality to LLM applications in a secure, standardized way. Think of it like a web API, but specifically designed for LLM interactions. MCP servers can:

- Expose data through resources (think of these sort of like GET endpoints; they are used to load information into the LLM's context)
- Provide functionality through tools (sort of like POST endpoints; they are used to execute code or otherwise produce a side effect)
- Define interaction patterns through prompts (reusable templates for LLM interactions)
- And more!

Resources

Resources are how you expose data to LLMs. They're similar to GET endpoints in a REST API - they provide data but shouldn't perform significant computation or have side effects:

// Static resource
server.resource(
  "config",
  "config://app",
  async (uri) => ({
    contents: [{
      uri: uri.href,
      text: "App configuration here"
    }]
  })
);

// Dynamic resource with parameters
server.resource(
"user-profile",
new ResourceTemplate("users://{userId}/profile", { list: undefined }),
async (uri, { userId }) => ({
contents: [{
uri: uri.href,
text: Profile data for user ${userId}
}]
})
);

Tools

Tools let LLMs take actions through your server. Unlike resources, tools are expected to perform computation and have side effects:

// Simple tool with parameters
server.tool(
  "calculate-bmi",
  {
    weightKg: z.number(),
    heightM: z.number()
  },
  async ({ weightKg, heightM }) => ({
    content: [{
      type: "text",
      text: String(weightKg / (heightM * heightM))
    }]
  })
);

// Async tool with external API call
server.tool(
"fetch-weather",
{ city: z.string() },
async ({ city }) => {
const response = await fetch(https://api.weather.com/${city});
const data = await response.text();
return {
content: [{ type: "text", text: data }]
};
}
);

Prompts

Prompts are reusable templates that help LLMs interact with your server effectively:

server.prompt(
  "review-code",
  { code: z.string() },
  ({ code }) => ({
    messages: [{
      role: "user",
      content: {
        type: "text",
        text: Please review this code:\n\n${code}
      }
    }]
  })
);

Contributing

Contributions are always welcome!

No reviews yet — be the first

Sign in to leave a review

Use Google, GitHub, or an email account so ratings stay tied to real people.

Email sign in

No reviews posted yet.