Skip to content

Rate limit the MCP endpoint per token, by the minute and by the hour - #518

Merged
paulocastellano merged 12 commits into
trypostit:mainfrom
thedavididris:mcp-rate-limit
Nov 19, 2026
Merged

paulocastellano merged 12 commits into
trypostit:mainfrom
thedavididris:mcp-rate-limit

Conversation

@thedavididris

@thedavididris thedavididris commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Closes #408.

What changes

  • A named limiter, mcp, applied to /mcp/trypost after the token's workspace is loaded (routes/ai.php).
  • Each MCP token has its own budget. A token belongs to one AI client, one user and one workspace, so the key is the token: a busy teammate, or ChatGPT's and Claude's shared egress addresses, never use up anyone else's.
  • Two windows, like PostHog's MCP server: 240 requests a minute and 2400 an hour per token, in config/trypost.php (MCP_REQUESTS_PER_MINUTE, MCP_REQUESTS_PER_HOUR). One agent task chains many tool calls, so the limit sits above the REST API's.
  • Production only. Outside production the endpoint is not limited.
  • ChatGPT listing the tools before sign-in carries no token and is never limited (the same ranges mcp-oauth already exempts).
  • The key comes from the authenticated token, with no extra query. The REST API keeps its 60 per workspace and counts apart.

What a client receives

The limiter runs as HTTP middleware, before laravel/mcp reads the JSON-RPC message. A refused request gets HTTP 429 with Retry-After and the X-RateLimit-* headers. Requests refused at sign-in (401) or for the workspace (403) never reach the limiter, so they do not count.

Tests

tests/Feature/Mcp/McpRateLimitTest.php (limits lowered in config, so each test sends a handful of requests):

  • a tool call past the minute limit gets 429 with Retry-After;
  • the minute limit lifts once the minute is over;
  • the hour limit holds across minutes until the hour is over;
  • a teammate in the same workspace keeps their own limit;
  • requests refused at sign-in, or for the workspace, do not count;
  • ChatGPT with a token is limited by its token like any client;
  • ChatGPT listing the tools before sign-in is not limited;
  • outside production is not limited.

tests/Feature/Parity/CrossCuttingParityTest.php: an MCP token may send at least what the REST API allows a workspace per minute.

@paulocastellano

Copy link
Copy Markdown
Contributor

Thanks a lot for this, David! The MCP endpoint really needed a limit, and the tests made it easy to build on. We pushed a few changes on top of your branch:

  • Per token instead of per workspace. A token already belongs to one AI client, one user and one workspace, so each token now has its own budget. A busy teammate, or ChatGPT's and Claude's shared egress, never uses up anyone else's.
  • A minute and an hour window, like PostHog's MCP server: 240 a minute and 2400 an hour, set in config/trypost.php (MCP_REQUESTS_PER_MINUTE, MCP_REQUESTS_PER_HOUR). One agent task chains many tool calls, so 60 a minute shared by a whole workspace would have been tight. The REST API keeps its 60 per workspace.
  • Production only. Outside production the endpoint is not limited.
  • The key comes from the authenticated token, with no extra query.
  • Tests now lower the limits in config instead of sending 60 requests each, and cover two more cases: requests refused for the workspace don't count, and ChatGPT with a token is limited by its token like any client. The parity test now checks that an MCP token may send at least what the REST API allows.

Thanks again for the contribution! 🙌

@paulocastellano paulocastellano changed the title Rate limit the MCP endpoint per workspace, like the REST API Rate limit the MCP endpoint per token, by the minute and by the hour Nov 19, 2026
@paulocastellano
paulocastellano merged commit b569076 into trypostit:main Nov 19, 2026
24 of 28 checks passed
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.

MCP: rate limit the /mcp/trypost endpoint like the REST API

2 participants