Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Allow a RequestEvent listener to reject a request with a JSON-RPC error

未关闭
#513 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
65/100
Issue 类型
功能
描述清晰度
描述清楚
活跃度
活跃
技术栈
php

调研方向

Look at the RequestEvent class and its listener handling in the SDK. The change involves modifying handleRequest() in the protocol handling code to check for an error set on the event. Examine how ErrorEvent's setError() works as a model. Test by creating a listener that sets an error and verifying the correct JSON-RPC error is sent.

由索引模型根据 Issue 内容生成。

描述

Description

A RequestEvent listener can change the request with setRequest(), but it has no way to reject it. ErrorEvent already has setError(). RequestEvent has nothing like it.

We ran into this in Drupal's mcp_server_oauth module. It checks per-tool OAuth scopes in a RequestEvent listener. To refuse a call, the listener throws, and until 0.8 that exception escaped processInput() so Drupal could turn it into a 401 or 403. That was never a documented contract. It stopped working when #412 added a guard around each message. The guard is correct, and we're not asking to remove it. Now every thrown refusal becomes -32603:

{"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"Internal server error."}}

It's also logged as "Uncaught exception while handling message".

Proposal

Let a listener set an Error on RequestEvent. If one is set, handleRequest() sends it and skips the handler:

$event = $this->dispatchEvent(new RequestEvent($request, $session));
if (null !== $error = $event->getError()) {
    $this->sendResponse($transport, $error, $session);
    return;
}
$request = $event->getRequest();

This covers checks that need the parsed request, such as per-tool policy, where AuthorizationMiddleware can't help because it only sees the token.

Resource reads have a related gap. ReadResourceHandler turns any exception into Error while reading resource. It already rethrows MissingRequiredClientCapabilityException, so a documented exception type that's rethrown the same way would let a resource refuse access with a proper error.

Open question

A JSON-RPC error doesn't give the HTTP transport a status code. For per-tool scopes, the MCP authorization spec expects a 403 with WWW-Authenticate: Bearer error="insufficient_scope". Is that in scope for the SDK, or should hosts check scopes in HTTP middleware before the message reaches Protocol?

主要语言
PHP
星标
1.6k
派生
173
平均合并
2 天 20 小时
30 天内合并 PR
14

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

modelcontextprotocol/php-sdk 的其他 Issue

查看 modelcontextprotocol/php-sdk 的全部 Issue

相似的 Issue

更多 PHP Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。