|
| 1 | +/** |
| 2 | + * `SkillsClientExtension` — the client side of the MCP Skills extension |
| 3 | + * (`io.modelcontextprotocol/skills`, SEP-2640) as a {@linkcode ClientExtension}. |
| 4 | + * It wraps `skills/list`, `skills/get` and `resources/directory/read`, each |
| 5 | + * refused unless the server declared support, and `read`, which fetches a |
| 6 | + * skill file with `resources/read` and verifies it against the skill's entry. |
| 7 | + * |
| 8 | + * ```ts |
| 9 | + * const skills = new SkillsClientExtension(); |
| 10 | + * const client = new Client(info, { extensions: [skills] }); |
| 11 | + * await client.connect(transport); |
| 12 | + * |
| 13 | + * const { skills: entries } = await skills.list(); |
| 14 | + * const manifest = await skills.read(entries[0], entries[0].uri); |
| 15 | + * ``` |
| 16 | + * |
| 17 | + * One extension instance serves one client: `install` binds it. |
| 18 | + */ |
| 19 | + |
| 20 | +import type { BlobResourceContents, RequestOptions, TextResourceContents } from '@modelcontextprotocol/core-internal'; |
| 21 | +import { SdkError, SdkErrorCode } from '@modelcontextprotocol/core-internal'; |
| 22 | +import type { |
| 23 | + GetSkillResult, |
| 24 | + ListSkillsParams, |
| 25 | + ListSkillsResult, |
| 26 | + ReadResourceDirectoryResult, |
| 27 | + Skill, |
| 28 | + SkillsExtensionCapability |
| 29 | +} from '@modelcontextprotocol/core-internal/ext/skills'; |
| 30 | +import { |
| 31 | + getSkillResultSchema, |
| 32 | + listSkillsResultSchema, |
| 33 | + readResourceDirectoryResultSchema, |
| 34 | + skillDigest, |
| 35 | + skillFileBytes, |
| 36 | + SKILLS_EXTENSION_ID, |
| 37 | + skillsExtensionCapabilitySchema |
| 38 | +} from '@modelcontextprotocol/core-internal/ext/skills'; |
| 39 | + |
| 40 | +import type { Client } from '../../client/client'; |
| 41 | +import type { ClientExtension } from '../../client/extension'; |
| 42 | + |
| 43 | +const verificationFailure = (uri: string, reason: string): never => { |
| 44 | + throw new SdkError(SdkErrorCode.InvalidResult, `Skill file ${uri} failed verification: ${reason}`); |
| 45 | +}; |
| 46 | + |
| 47 | +const rawBytes = (content: TextResourceContents | BlobResourceContents): Uint8Array => |
| 48 | + 'blob' in content ? Uint8Array.from(atob(content.blob), char => char.codePointAt(0) ?? 0) : skillFileBytes(content.text); |
| 49 | + |
| 50 | +export class SkillsClientExtension implements ClientExtension { |
| 51 | + readonly id = SKILLS_EXTENSION_ID; |
| 52 | + #client: Client | undefined; |
| 53 | + |
| 54 | + install(client: Client): void { |
| 55 | + if (this.#client !== undefined) throw new Error('SkillsClientExtension is already installed on a client'); |
| 56 | + this.#client = client; |
| 57 | + } |
| 58 | + |
| 59 | + get client(): Client { |
| 60 | + if (this.#client === undefined) throw new SdkError(SdkErrorCode.NotConnected, 'SkillsClientExtension is not installed on a client'); |
| 61 | + return this.#client; |
| 62 | + } |
| 63 | + |
| 64 | + /** The extension settings the server declared, or `undefined` when it declared none. */ |
| 65 | + get capability(): SkillsExtensionCapability | undefined { |
| 66 | + const declared = this.client.getServerCapabilities()?.extensions?.[SKILLS_EXTENSION_ID]; |
| 67 | + const parsed = skillsExtensionCapabilitySchema.safeParse(declared); |
| 68 | + return parsed.success ? parsed.data : undefined; |
| 69 | + } |
| 70 | + |
| 71 | + /** `skills/list`: one page of entries. Pass `nextCursor` back as `cursor` for the next. */ |
| 72 | + async list(params?: ListSkillsParams, options?: RequestOptions): Promise<ListSkillsResult> { |
| 73 | + this.#require('skills/list'); |
| 74 | + return (await this.client.request( |
| 75 | + { method: 'skills/list', params: { ...params } }, |
| 76 | + listSkillsResultSchema, |
| 77 | + options |
| 78 | + )) as ListSkillsResult; |
| 79 | + } |
| 80 | + |
| 81 | + /** `skills/get`: the entry for the skill whose `SKILL.md` is at `uri`, listed or not. */ |
| 82 | + async get(uri: string, options?: RequestOptions): Promise<GetSkillResult> { |
| 83 | + this.#require('skills/get'); |
| 84 | + return (await this.client.request({ method: 'skills/get', params: { uri } }, getSkillResultSchema, options)) as GetSkillResult; |
| 85 | + } |
| 86 | + |
| 87 | + /** `resources/directory/read`: the direct children of a directory resource. Refused unless the server declared `directoryRead`. */ |
| 88 | + async readDirectory(uri: string, cursor?: string, options?: RequestOptions): Promise<ReadResourceDirectoryResult> { |
| 89 | + this.#require('resources/directory/read'); |
| 90 | + if (this.capability?.directoryRead !== true) { |
| 91 | + throw new SdkError(SdkErrorCode.CapabilityNotSupported, 'Server does not support resources/directory/read'); |
| 92 | + } |
| 93 | + const params = { uri, ...(cursor !== undefined && { cursor }) }; |
| 94 | + return (await this.client.request( |
| 95 | + { method: 'resources/directory/read', params }, |
| 96 | + readResourceDirectoryResultSchema, |
| 97 | + options |
| 98 | + )) as ReadResourceDirectoryResult; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Reads one of `skill`'s files and verifies it against the entry: the file |
| 103 | + * must be listed, and its size and digest must match. Throws `InvalidResult` |
| 104 | + * on any mismatch. A `"dynamic"` skill has nothing to verify against, so its |
| 105 | + * files are returned as read. Comparing a `SKILL.md`'s frontmatter with the |
| 106 | + * entry needs a YAML parser and is left to the host. |
| 107 | + */ |
| 108 | + async read(skill: Skill, uri: string, options?: RequestOptions): Promise<TextResourceContents | BlobResourceContents> { |
| 109 | + const listed = skill.resources === 'dynamic' ? undefined : skill.resources.find(resource => resource.uri === uri); |
| 110 | + if (skill.resources !== 'dynamic' && listed === undefined) verificationFailure(uri, `not listed in ${skill.uri}`); |
| 111 | + |
| 112 | + const { contents } = await this.client.readResource({ uri }, options); |
| 113 | + const content = contents.find(item => item.uri === uri) ?? verificationFailure(uri, 'not in the resources/read result'); |
| 114 | + if (listed === undefined) return content; |
| 115 | + |
| 116 | + const bytes = rawBytes(content); |
| 117 | + if (bytes.byteLength !== listed.size) verificationFailure(uri, `size ${bytes.byteLength}, entry says ${listed.size}`); |
| 118 | + if ((await skillDigest(bytes)) !== listed.digest) verificationFailure(uri, 'digest mismatch'); |
| 119 | + return content; |
| 120 | + } |
| 121 | + |
| 122 | + #require(method: string): void { |
| 123 | + if (this.capability === undefined) { |
| 124 | + throw new SdkError( |
| 125 | + SdkErrorCode.CapabilityNotSupported, |
| 126 | + `Server does not support the ${SKILLS_EXTENSION_ID} extension (${method})` |
| 127 | + ); |
| 128 | + } |
| 129 | + if (this.client.getServerCapabilities()?.resources === undefined) { |
| 130 | + throw new SdkError(SdkErrorCode.CapabilityNotSupported, `Server declares ${SKILLS_EXTENSION_ID} without resources (${method})`); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments