Skip to content

fix(mcp): implement missing MCP resources API + endpoint auth on the pending-channel client (port of #48) - #67

Closed
lezi-fun wants to merge 1 commit into
HEXUXIU:mainfrom
lezi-fun:fix/mcp-resources-main
Closed

lezi-fun wants to merge 1 commit into
HEXUXIU:mainfrom
lezi-fun:fix/mcp-resources-main

Conversation

@lezi-fun

Copy link
Copy Markdown
Contributor

Summary

Port of #48 onto the current main, as requested in the last review round (the old branch targeted dev before the msgCh → pending map[int64]chan client refactor and no longer applied).

Supersedes #48.

1. MCP resources API was never implemented

Ports the implementation, adapted to main:

  • Resource / ResourceContent / ResourceProvider types
  • RWMutex-guarded global provider accessed only via resourceProvider() / SetGlobalResourceProvider() — safe for concurrent swap
  • resources/list + resources/read handlers; initialize now advertises the resources capability
  • SSRF guard: URI scheme allowlist (mcp://, gateway://, m365://) plus a check that the URI was actually advertised by resources/list, so reads are tied to the enumerated surface and cannot probe arbitrary targets. A TODO(perf) documents that the O(n) scan is acceptable at current scale, with ResourceExists(ctx, uri) as the future escape hatch.

2. Error-message hygiene

  • Provider errors and raw JSON unmarshal errors are logged server-side; clients receive generic messages
  • resources/read 404 (-32002) no longer echoes the requested URI back to the client — it is logged instead

3. Endpoint auth unified on APIKeyValidator

HandleSSE and HandleMessage now enforce APIKeyValidator — the same variable HandleToolsList already uses, wired once in Routes(). No new hook variable or initMCPAuth; the old AuthHook design from #48 is gone.

4. readSSE on the new per-request channel model

The original PR's bounded-blocking-send (timer reuse) is intentionally not ported: in the pending map[int64]chan model each channel has capacity 1 with exactly one select-waiting consumer, so a full channel means the requester already timed out. Blocking there would stall the SSE reader for a response nobody will ever read. The drop path now logs why.

The session_resolver reverse-index work from #48 is dropped: main's Tenant/ExplicitID refactor already handles byExplicit cleanup correctly.

Testing

go build ./...
go vet ./internal/...
go test ./internal/...        # all pass
go test -race ./internal/mcp/ # pass

New/ported tests:

  • TestResourcesListReturnsRegisteredResources, TestResourcesReadReturnsContent, TestInitializeAdvertisesResourcesCapability
  • TestResourcesReadRejectsUnlistedURI (SSRF: unlisted-but-allowed-scheme rejected, disallowed scheme rejected, listed URI readable)
  • client_sse_test.go rewritten for the pending model: late response drops without blocking, waiter delivery works, id-less notifications are skipped

…nnel client (port of PR HEXUXIU#48 to main)

Port of the PR HEXUXIU#48 work rebased onto the refactored main branch, as
requested in the last review round.

resources API (was never implemented; tests added earlier did not build):
- Resource / ResourceContent / ResourceProvider types
- RWMutex-guarded global provider (resourceProvider() /
  SetGlobalResourceProvider) — safe for concurrent swap
- resources/list + resources/read RPC handlers
- SSRF guard: scheme allowlist (mcp:// gateway:// m365://) PLUS a check
  that the URI was advertised via resources/list, so reads are tied to
  the enumerated surface. TODO(perf) notes the O(n) scan is acceptable
  at current scale with ResourceExists as the escape hatch.
- initialize now advertises the resources capability

Error-message hygiene:
- provider errors and raw unmarshal errors are logged server-side;
  clients get generic messages only
- resource-not-found (-32002) no longer echoes the requested URI — it
  is logged instead

Auth:
- HandleSSE / HandleMessage now enforce APIKeyValidator (same variable
  HandleToolsList already uses; wired once in Routes()). No new hook
  variable.

readSSE on the new pending map[int64]chan model:
- non-blocking send is kept deliberately: each pending channel has
  capacity 1 and exactly one select-waiting consumer, so a full channel
  means the requester timed out. Blocking would stall the SSE reader
  for a response nobody will read. The drop path now logs.
- client_sse_test.go rewritten for this model: late-response drop must
  not block, waiter delivery works, id-less notifications are skipped.

The session_resolver reverse-index work from the original PR is dropped:
main's Tenant/ExplicitID refactor already resolves byExplicit cleanup
correctly.
@HEXUXIU

HEXUXIU commented Sep 27, 2026

Copy link
Copy Markdown
Owner

感谢提交!但项目方向已调整:对外只保留 Web 控制台与三套标准接口(OpenAI Chat/Responses、Anthropic Messages),不再对外暴露 MCP。MCP 仅用于内部把标准工具调用转换为 M365 官方通道。因此本 PR 不再适用,故关闭。

@HEXUXIU HEXUXIU closed this Sep 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants