← 返回列表
全部 Skill›测试与安全 Testing & Security›testing

testing

作者 @modelcontextprotocol · 语言 en
Write, place and run tests for a server in this repo. Use when choosing between driving the server through an MCP client and calling its functions directly; when writing a protocol-level test for a TypeScript or Python server; when choosing the command that runs one suite or a coverage report; or when a test is skipped or passes against a stale build.
测试与安全 Testing & Security测试通用执行命令访问网络待翻译
最近源活动
—
检测 SKILL.md 语言
en
星标 ★
92,114
分支 Fork
0
安装选项
提示
指令
使用 AI 助手安装
将下面的提示复制到你正在使用的 AI 助手中。
下载完整 Skill 目录(含全部配套文件)。 下载压缩包 ↓
SKILL.md 原文预览

Testing

The rules are in AGENTS.md under **Always test new

or modified code**: new behavior comes with tests, every TypeScript file clears

the per-file coverage gate, test at the protocol level where you can, and keep

expected error output off the console. This skill explains the harness each

server has, how to write a test with it, which command runs what, and how to

clear the per-file coverage gate.

Driving a running server by hand, with the Inspector or an LLM client, is

/client-smoke. It is evidence for a PR, not a substitute for a test.

Every server is tested in-process, and every file is gated. The four

TypeScript servers link a real Client to the server over an in-memory

transport (#4854); the three Python servers link a ClientSession to the real

serve() over in-memory streams (#4855). Both languages hold each file to a

per-file coverage floor, in CI and in npm run local:gate.

Where a test goes

| Server | Directory | File name | Runner |

| --- | --- | --- | --- |

| everything, filesystem, memory, sequentialthinking | src/<server>/__tests__/ | <subject>.test.ts | vitest, globals: true |

| fetch, git, time | src/<server>/tests/ | test_<subject>.py | pytest, with pytest-asyncio (asyncio_mode = "auto") |

| Root tooling | scripts/, beside the script | <name>.test.mjs | node --test |

Each vitest config includes only /__tests__//*.test.ts, so a test file

anywhere else in a workspace is never run and reports nothing. Shared test

helpers go in __tests__/ without the .test suffix, as each server's

connection harness does (__tests__/harness.ts in everything,

__tests__/helpers.ts in the other three).

What each server's tests do

| Server | How its tests reach the code |

| --- | --- |

| everything, filesystem, memory, sequentialthinking | An SDK Client connected to the server's createServer() over InMemoryTransport, in the vitest process, through the server's own harness, plus unit tests of helper modules such as lib.ts. everything drives its stdio, SSE and Streamable HTTP transports in-process too; the other three drive their http.ts in-process over a free loopback port, and have one thin spawn smoke of the built dist/index.js |

| fetch, git, time | A ClientSession linked in-process to the real serve() over in-memory streams (below), plus direct calls on the pure functions in server.py |

No TypeScript test uses an in-memory transport yet.

Choosing a harness for a new test

Ask what the test must prove.

  • Logic (a path check, a graph operation, a timezone conversion): call the

function directly. That is what lib.ts and the functions in server.py are

for.

  • What a client sees (a tool's result shape, its advertised schema, an

error result, a capability, a notification): drive the server through a

client. A handler called directly skips the SDK's input validation, output

schema validation and error mapping, which is where the wire-level bugs in

this repo have been.

Which client harness is available depends on the language.

TypeScript: in-process, over an in-memory transport

Each TypeScript server exports a createServer(…) factory and starts its

transport only from a guarded main(), so importing it starts nothing and a

test can link a real Client to a real server with no process and no build.

**Each server's __tests__/ has a harness that does the linking. Connect

through it**, never by calling createServer() and

InMemoryTransport.createLinkedPair() by hand: the harness also does the

per-session setup and teardown the server needs.

| Server | Factory | Harness | connect(…) takes |

| --- | --- | --- | --- |

| everything | createServer() in server/index.ts, returns { server, cleanup } | __tests__/harness.ts | { capabilities, setup, sessionId }, all optional |

| filesystem | createServer(allowedDirectories) in server.ts | __tests__/helpers.ts | the allowed directories, then { capabilities, listRoots } |

| memory | createServer(memoryFilePath) in index.ts | __tests__/helpers.ts | the graph file's path (makeTempGraph() makes one) |

| sequentialthinking | createServer() in index.ts | __tests__/helpers.ts | { disableThoughtLogging }, defaulting to "true" |

Each connect returns the client and a close(); call close() in

afterEach. The harnesses also export small result helpers (call,

textOf and the like); read the one you are using before adding another.

import { afterEach, beforeEach, describe, expect, it } from "vitest";
import type { Client } from "@modelcontextprotocol/client";
import { connect, contentOf, textOf, type Session } from "./harness.js";

describe("echo, over the protocol", () => {
  let session: Session;
  let client: Client;

  beforeEach(async () => {
    session = await connect();
    client = session.client;
  });

  afterEach(async () => {
    await session.close();
  });

  it("returns the message", async () => {
    const result = await client.callTool({
      name: "echo",
      arguments: { message: "hi" },
    });
    expect(textOf(contentOf(result)[0])).toBe("Echo: hi");
  });

  it("reports bad input as a tool error", async () => {
    const result = await client.callTool({ name: "echo", arguments: {} });
    expect(result.isError).toBe(true);
  });
});

What everything's harness does that a hand-rolled connection would miss:

  • A session id per connection. Logging, subscriptions, roots and the

to

评论
Mmodelcontextprotocol
modelcontextprotocol
modelcontextprotocol/servers
查看 GitHub 仓库 查看作者的其他 Skill
来源信息
作者@modelcontextprotocol
所属大类测试与安全 Testing & Security / 测试
国内可用性通用
最近源活动—
风险提示执行命令、访问网络
提示:风险标签为程序静态扫描结果,仅作提示,不代表安全认证。安装前请阅读 SKILL.md 及其引用的脚本、权限,自行判断是否可信。