agentleFS
phughesmcrphughesmcr/deno-mcp-template/.cursor/skills/implementing-mcp-resources/SKILL.md

implementing-mcp-resources

Skill33 starsChanged 8 months agoMITView on GitHub

Implement new MCP resources and resource templates in the deno-mcp-template project. Provides the exact file structure, type signatures, registration steps, and patterns for static resources, KV-backed resources, resource templates with URI patterns, and subscription-based updates. Use when adding a new resource, creating MCP resources, or asking how resources work in this project.

The file

We split this skill into the rules it gives an agent

7 rules. About 0 are common, 1 is distinctive, and 6 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 6

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Create a new file in src/mcp/resources/.

    line 30
  2. Very specific:

    Import the resource: import myResource from "./myResource.ts".

    line 128
  3. Very specific:

    Add it to the resources array:

    line 129
  4. Very specific:

    When resourceSubscribe: true in mcpServerDefinition (src/mcp/serverDefinition.ts), resources get subscription support:

    line 216
  5. Very specific:

    When the KV key changes, all subscribed clients receive resourceUpdated notifications

    line 220
  6. Distinctive:

    No manual notification code needed in tools — just mutate KV state

    line 221
  7. Very specific:

    Use text for text/JSON, blob for binary data.

    line 240

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Implementing MCP Resources

Workflow

Task Progress:
- [ ] Step 1: Create resource file in src/mcp/resources/
- [ ] Step 2: Define name, URI/template, config, and readCallback
- [ ] Step 3: Export as default ResourcePlugin or ResourceTemplatePlugin
- [ ] Step 4: Register in src/mcp/resources/mod.ts
- [ ] Step 5: (If KV-backed) Add URI-to-key mapping in kvKeys.ts
- [ ] Step 6: Run `deno task ci` to verify

Resource Types

TypeUse CaseURIPlugin Type
Static resourceFixed content at a known URI"hello://world"ResourcePlugin
KV-backed resourcePersistent, mutable state"counter://value"ResourcePlugin
Resource templateDynamic URI with variables"greetings://{name}"ResourceTemplatePlugin

Static Resource Template

Create a new file in src/mcp/resources/.

import type { ResourceMetadata } from "@modelcontextprotocol/sdk/server/mcp.js";
import type { ReadResourceResult } from "@modelcontextprotocol/sdk/types.js";

import type { ResourcePlugin } from "$/shared/types.ts";

const name = "myResource";

const uri = "my-scheme://my-path";

const config: ResourceMetadata = {
  description: "What this resource provides",
  mimeType: "text/plain",  // or "application/json"
};

async function readCallback(): Promise<ReadResourceResult> {
  return {
    contents: [{
      uri,
      text: "Resource content here",
    }],
  };
}

const module: ResourcePlugin = {
  type: "resource",
  name,
  uri,
  config,
  readCallback,
};

export default module;

Resource Template (Dynamic URI)

For resources with variable URI segments like greetings://{name}:

import {
  type CompleteResourceTemplateCallback,
  type ResourceMetadata,
  ResourceTemplate,
} from "@modelcontextprotocol/sdk/server/mcp.js";
import type { ReadResourceResult } from "@modelcontextprotocol/sdk/types.js";

import type { ResourceTemplatePlugin } from "$/shared/types.ts";

const name = "myTemplate";

const completeName: CompleteResourceTemplateCallback = (value) => {
  const prefix = value.trim().toLowerCase();
  return suggestions.filter((s) => s.toLowerCase().startsWith(prefix)).slice(0, 5);
};

const template = new ResourceTemplate(
  "my-scheme://{variable}",
  {
    list: undefined,               // optional: callback to list all instances
    complete: { variable: completeName },  // optional: autocomplete per variable
  },
);

const config: ResourceMetadata = {
  mimeType: "text/plain",
};

async function readCallback(
  uri: URL,
  variables: Record<string, unknown>,
): Promise<ReadResourceResult> {
  const variable = variables.variable as string;
  return {
    contents: [{
      uri: uri.toString(),
      text: `Content for ${variable}`,
    }],
  };
}

const module: ResourceTemplatePlugin = {
  type: "template",
  name,
  template,
  config,
  readCallback,
};

export default module;

Registration

In src/mcp/resources/mod.ts:

  1. Import the resource: import myResource from "./myResource.ts";
  2. Add it to the resources array:
export const resources: AnyResourcePlugin[] = [
  // ... existing resources
  myResource,
];

The server dispatches based on resource.type:

  • "resource" calls registerResource(name, uri, config, readCallback)
  • "template" calls registerResource(name, template, config, readCallback)

Key Types

From src/shared/types.ts:

export type ResourcePlugin = {
  type: "resource";
  name: string;
  uri: string;
  config: ResourceMetadata;
  readCallback: ReadResourceCallback;
};

export type ResourceTemplatePlugin = {
  type: "template";
  name: string;
  template: ResourceTemplate;
  config: ResourceMetadata;
  readCallback: ReadResourceTemplateCallback;
};

export type AnyResourcePlugin = ResourcePlugin | ResourceTemplatePlugin;

KV-Backed Resources

For resources with persistent mutable state:

  1. Create a store file (e.g. myStore.ts) with KV read/write functions:
import { getKvStore } from "$/kv/mod.ts";

export const MY_KEY: Deno.KvKey = ["resource", "my-resource", "value"];

export async function getValue(): Promise<string> {
  const kv = await getKvStore();
  const entry = await kv.get<string>(MY_KEY);
  return entry.value ?? "default";
}

export async function setValue(value: string): Promise<void> {
  const kv = await getKvStore();
  await kv.set(MY_KEY, value);
}
  1. Use in the resource readCallback:
import { getValue } from "./myStore.ts";

async function readCallback(): Promise<ReadResourceResult> {
  const value = await getValue();
  return { contents: [{ uri, text: JSON.stringify({ value }) }] };
}
  1. Register the KV key mapping in src/mcp/resources/kvKeys.ts:
import { MY_URI } from "./myResource.ts";
import { MY_KEY } from "./myStore.ts";

export const RESOURCE_KV_KEYS: ReadonlyMap<string, Deno.KvKey> = new Map([
  [COUNTER_URI, COUNTER_KEY],
  [MY_URI, MY_KEY],  // add your mapping
]);

This enables automatic subscription notifications — when a tool mutates the KV key, the subscription tracker detects the change and notifies subscribed clients.

Subscription Flow

When resourceSubscribe: true in mcpServerDefinition (src/mcp/serverDefinition.ts), resources get subscription support:

  1. Client subscribes to a URI
  2. subscriptionTracker.subscribe() starts watching the mapped KV key
  3. When the KV key changes, all subscribed clients receive resourceUpdated notifications
  4. No manual notification code needed in tools — just mutate KV state

The subscription tracker (src/mcp/resources/subscriptionTracker.ts) handles ref-counting, KV watching, and notifier lifecycle automatically.

ReadResourceResult Format

Both resource types return the same shape:

{
  contents: [{
    uri: string,       // the resource URI
    text: string,      // text content (for text/plain or application/json)
    // OR
    blob: string,      // base64-encoded binary content
  }],
}

Use text for text/JSON, blob for binary data. Set mimeType in config accordingly.

Additional Resources

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.