Repository navigation
Commit 7c03aa0
authored
fix(ai-agents): tolerate a trailing slash between the MCP server URL and its advertised resource (#31088)
Closes #31085 (PROD-12223)
Stacked on #31087.
## Problem
OAuth sign-in to an external MCP server failed when the server's protected-resource metadata advertised `"resource": "https://host/mcp/"` but the server URL was entered as `https://host/mcp`. The SDK's `checkResourceAllowed` has an early return when the requested path is shorter than the configured one, so this direction failed with `Protected resource https://host/mcp/ does not match expected https://host/mcp (or origin)` while the reverse direction passed. Users cannot control what a gateway advertises.
## Change
A `pnpm patch` on `@modelcontextprotocol/[email protected]` that removes the path-length early return from `checkResourceAllowed`, in both the CommonJS and ESM builds. The function already normalises both paths to a trailing slash before its prefix check, so `/mcp` now matches `/mcp/`, while `/mcp` against `/mcp123`, a different path, or a different origin are still rejected. The origin and path-prefix binding (RFC 8707) is unchanged.
Why a patch rather than a `validateResourceURL` override (the first version of this PR): the SDK only sends the advertised resource string verbatim when no provider override is present. An override forces the SDK to serialise the override's return value through `URL.href`, which appends `/` to a pathless indicator and reintroduces modelcontextprotocol/typescript-sdk#1968. Keeping the SDK's own path avoids re-implementing resource selection and any workaround.
- `checkResourceAllowed` is used only by the SDK's client auth path. Lightdash's own MCP server does not call it, so nothing server-side changes.
- The patch is version-pinned. The next SDK bump fails install until the patch is re-applied or dropped once upstream has the fix.
- The Dockerfiles already copy `patches/` before install (added for the existing `scim-patch` patch in #30682).
- Upstream: modelcontextprotocol/typescript-sdk#3041 describes the asymmetry and proposes the same change. Upstream has an explicit test asserting the current one-directional behaviour, so I opened an issue rather than a PR to settle the "what" first, per their contributing guide. Drop this patch once the fix lands and the SDK is bumped.
- Still client-only in 1.32.1: `checkResourceAllowed` is not used by the SDK's server-side bearer-auth check there either, so a bump to that line keeps the patch's blast radius unchanged.
## Tests
`AiAgentMcpRuntimeClient.test.ts` now drives the SDK's real `auth()` through `PersistentMcpOAuthClientProvider` with a mocked fetch serving protected-resource and authorization-server metadata, and asserts on the `resource` query parameter of the authorization URL handed to `onAuthorizationUrl`:
- metadata that adds a trailing slash is accepted and sent verbatim (fails without the patch, verified by temporarily restoring the SDK's original check);
- metadata that drops the trailing slash is accepted;
- a pathless resource is sent without a slash appended;
- a different path is still rejected and no authorization URL is produced.
The same test also covers the public-client flow from #31087 end to end: the configured client ID reaches the authorization URL with `code_challenge_method=S256`.
🤖 Generated with [Claude Code](https://claude.com/claude-code)1 parent 8816ff5 commit 7c03aa0
4 files changed
Lines changed: 216 additions & 7 deletions
File tree
- packages/backend/src/ee/services/ai
- patches
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
5 | 8 | | |
6 | 9 | | |
7 | 10 | | |
| |||
644 | 647 | | |
645 | 648 | | |
646 | 649 | | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
647 | 820 | | |
648 | 821 | | |
649 | 822 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
162 | 162 | | |
163 | 163 | | |
164 | 164 | | |
| 165 | + | |
165 | 166 | | |
0 commit comments