Lesson 4 of 6·10 min read

Resources & Prompts

Besides tools, MCP offers two more capabilities: Resources (providing data) and Prompts (predefined interaction patterns). Together they form a complete ecosystem for AI model integration.

Resources

Resources are data an MCP server makes available to the client. Unlike tools, which are actively called, resources are passive — they provide context.

Static Resources

server.resource(
  "docs://api-reference",
  "API documentation for the product",
  async () => ({
    contents: [{
      uri: "docs://api-reference",
      text: apiDocumentation,
      mimeType: "text/markdown"
    }]
  })
);

Dynamic Resources

server.resource(
  "data://sales/{period}",
  "Sales data for a time period",
  async (uri) => {
    const period = uri.pathname.split("/").pop();
    const data = await db.query(`SELECT * FROM sales WHERE period = ?`, [period]);
    return {
      contents: [{
        uri: uri.href,
        text: JSON.stringify(data),
        mimeType: "application/json"
      }]
    };
  }
);

Resource Types

TypeURI SchemaExample
Filesfile://file:///docs/readme.md
Databasedb://db://users/123
APIapi://api://weather/berlin
Configconfig://config://app/settings
CustomOwn schemacrm://contacts/active

Prompts

Prompts are predefined interaction patterns provided by the server. The client can request them and offer them to the user.

Define a Prompt

server.prompt(
  "code_review",
  "Perform code review with best practices",
  {
    code: z.string().describe("The code to review"),
    language: z.string().describe("Programming language"),
    focus: z.enum(["security", "performance", "readability"]).optional()
  },
  async ({ code, language, focus }) => ({
    messages: [{
      role: "user",
      content: {
        type: "text",
        text: [
          `Perform a code review for the following ${language} code.`,
          focus ? `Focus especially on: ${focus}` : "",
          `\n\n\`\`\`${language}\n${code}\n\`\`\``,
          "\nEvaluate: correctness, best practices, potential bugs, improvement suggestions."
        ].join("\n")
      }
    }]
  })
);

List and Use Prompts

// Client side
const { prompts } = await client.listPrompts();

// Request prompt
const result = await client.getPrompt({
  name: "code_review",
  arguments: {
    code: "function add(a, b) { return a + b }",
    language: "javascript",
    focus: "readability"
  }
});

Sampling

MCP supports sampling — the server can request the AI model via the client:

Server → Client: "Analyze this data"
Client → AI Model → Client → Server: "Analysis result"

This enables server-side workflows that use the AI model as a reasoning engine.

Context Management

Efficient Context Loading

// Only load relevant resources
const resources = await client.listResources();
const relevant = resources.filter(r =>
  r.uri.includes(currentTopic)
);
for (const resource of relevant) {
  const content = await client.readResource(resource.uri);
  context.push(content);
}

Mind the Context Window

  • Resources consume tokens in the context window
  • Only load relevant resources, not all of them
  • Use summaries instead of full documents for large resources

Practical tip: Resources are excellent for documentation, configuration, and reference data. Prompts standardize recurring interaction patterns. Use both to make your MCP server valuable beyond pure tool integration.