实现具备安全校验的MCP服务器
根据工具规格编写 MCP 服务端,定义输入校验、权限边界、传输方式、错误处理,并补充测试与使用文档。
完整提示词
<context>
An MCP server lets any MCP client (coding agents, chat apps, IDEs) call your tools and read your resources. The model decides the arguments, so every input is untrusted, including inputs that came from a prompt injection in some document the model read earlier. Servers commonly break in a few ways: on stdio, anything written to stdout that is not a protocol message corrupts the session; handlers throw raw exceptions, so the model sees a generic failure and retries blindly; file tools accept `../` paths; database tools accept raw SQL; HTTP servers listen on every interface without checking origin or authentication; and one broad admin token is shared by every tool.
</context>
<task>
Implement an MCP server in {{language}} over {{transport}} for:
{{tools_spec}}
1. Restate each tool and resource as a table: name, inputs, output, side effects, the external system and the credential it uses. If the spec is ambiguous in a way that changes behaviour or privileges (which directory, which database role, whether writes are allowed), ask before writing code.
2. Use the official MCP SDK for {{language}} at its current major version. If you are not sure of an exact API in that version, check the SDK's README or type definitions rather than guessing, and list what you assumed.
3. For every tool:
- declare the input schema with types, enums, bounds and descriptions written for the model;
- set the tool annotations honestly (read-only, destructive, idempotent, open-world);
- validate beyond the schema in the handler: resolve paths and reject anything outside the allowed root, use parameterised queries, check identifiers against allowlists, and cap sizes and counts;
- return results as concise text or structured content, truncating large outputs and saying how to get the rest;
- on failure, return a tool result marked as an error, with a message that tells the model what to change, for example "path must be inside notes/; got ../etc/passwd". Never return stack traces, secrets or internal hostnames.
4. Expose resources with stable URIs if the spec includes read-only data.
5. Apply least privilege: read configuration and secrets from environment variables, use read-only credentials for read-only tools, allowlist roots, hosts and tables, and put timeouts on every outbound call.
6. Transport. stdio: write logs to stderr only. http: use Streamable HTTP, bind to 127.0.0.1 by default, validate the `Origin` header, require authentication for anything that is not strictly local, and note that the MCP specification defines OAuth-based authorization for remote servers.
7. Write tests for input validation and error paths at minimum, a README with the environment variables and a client configuration snippet, and how to try the server with the MCP Inspector.
8. If you can run commands, install, build and run the tests, and report the real output. If you cannot, say that nothing was run.
</task>
<constraints>
- Implement only the tools and resources in the spec. Suggest extra ones in one line under Assumptions.
- No shell execution with interpolated input. If the spec asks for arbitrary command execution, raw SQL or unrestricted file writes, explain the risk and propose a narrower tool (an allowlist of commands, named queries, a sandboxed directory) before implementing anything broader.
- Pin the SDK's major version in the manifest.
{{> guardrails/scope-discipline}}
</constraints>
<output_format>
## Plan
The tools and resources table, with the privilege each one needs.
## Files
Each file in its own fenced block, preceded by its path.
## Run and test
Commands to install, build, test and connect a client, plus the real test output or "Not run".
## Security notes
What each tool can reach, what the validation blocks, and the remaining risks.
## Assumptions
SDK details, spec interpretations and suggested additions.
</output_format>可替换内容
- {{language}}
- {{transport}}
- {{tools_spec}}
- {{> guardrails/scope-discipline}}
分类与标签
编程开发、MCP开发、服务端实现、输入校验、安全测试