Skip to content

Commit 9de0a74

Browse files
ZvonimirZvonimir
authored andcommitted
fixes
1 parent 1bf75c2 commit 9de0a74

2 files changed

Lines changed: 46 additions & 5 deletions

File tree

packages/plugin-epcis/tests/pluginEpcis.spec.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
} from "@dkg/plugins/testing";
2727
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2828
import type { Client } from "@modelcontextprotocol/sdk/client/index.js";
29+
import type { AuthInfo } from "@modelcontextprotocol/sdk/server/auth/types.js";
2930
import express from "express";
3031

3132
const story = bicycleStory as any;
@@ -54,14 +55,24 @@ type ToolCallParams = {
5455
arguments?: Record<string, unknown>;
5556
};
5657

57-
const MCP_TOOL_AUTH = {
58+
const MCP_TOOL_AUTH: { authInfo: AuthInfo } = {
5859
authInfo: {
60+
token: "test-token",
61+
clientId: "test-client",
5962
scopes: ["epcis.read", "epcis.write"],
6063
},
61-
} as const;
64+
};
65+
66+
let setMcpClientAuthInfo: ((authInfo?: AuthInfo) => void) | null = null;
6267

6368
function callToolWithAuth(client: Client, params: ToolCallParams) {
64-
return client.callTool(params, undefined, MCP_TOOL_AUTH as any);
69+
setMcpClientAuthInfo?.(MCP_TOOL_AUTH.authInfo);
70+
return client.callTool(params);
71+
}
72+
73+
function callToolWithoutAuth(client: Client, params: ToolCallParams) {
74+
setMcpClientAuthInfo?.(undefined);
75+
return client.callTool(params);
6576
}
6677

6778
describe("@dkg/plugin-epcis checks", function () {
@@ -87,9 +98,11 @@ describe("@dkg/plugin-epcis checks", function () {
8798
};
8899
dkgQueryStub = sinon.stub(dkgContext.dkg.graph, "query");
89100

90-
const { server, client, connect } = await createMcpServerClientPair();
101+
const { server, client, connect, setClientAuthInfo } =
102+
await createMcpServerClientPair();
91103
mockMcpServer = server;
92104
mockMcpClient = client;
105+
setMcpClientAuthInfo = setClientAuthInfo;
93106
apiRouter = express.Router();
94107
app = createExpressApp();
95108

@@ -99,6 +112,7 @@ describe("@dkg/plugin-epcis checks", function () {
99112
});
100113

101114
afterEach(() => {
115+
setMcpClientAuthInfo = null;
102116
sinon.restore();
103117
if (originalMcpUrl !== undefined) {
104118
process.env.EXPO_PUBLIC_MCP_URL = originalMcpUrl;
@@ -562,6 +576,18 @@ describe("@dkg/plugin-epcis checks", function () {
562576
);
563577
});
564578

579+
it("returns Forbidden when MCP auth context is missing", async () => {
580+
const result = await callToolWithoutAuth(mockMcpClient, {
581+
name: "epcis-query",
582+
arguments: { bizStep: "receiving" },
583+
});
584+
const payload = parseToolResult(result);
585+
586+
expect(result.isError).to.equal(true);
587+
expect(payload.error).to.equal("Forbidden");
588+
expect(payload.requiredScope).to.equal("epcis.read");
589+
});
590+
565591
it("returns MCP error when DKG query fails", async () => {
566592
dkgQueryStub.rejects(new Error("query exploded"));
567593
const result = await callToolWithAuth(mockMcpClient, {

packages/plugins/src/testing.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
22
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
33
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
4+
import type { AuthInfo } from "@modelcontextprotocol/sdk/server/auth/types.js";
45
import { createBlobStorage, express } from "./helpers";
56
import { BlobData, BlobMetadata } from "./types";
67

@@ -36,12 +37,26 @@ export const createExpressApp = () => {
3637
export const createMcpServerClientPair = async () => {
3738
const server = new McpServer({ name: "Test DKG Server", version: "1.0.0" });
3839
const client = new Client({ name: "Test DKG Client", version: "1.0.0" });
39-
const [serverTransport, clientTransport] =
40+
const [clientTransport, serverTransport] =
4041
InMemoryTransport.createLinkedPair();
42+
let authInfoForClientMessages: AuthInfo | undefined;
43+
44+
const originalClientSend = clientTransport.send.bind(clientTransport);
45+
clientTransport.send = (message, options) => {
46+
return originalClientSend(message, {
47+
...options,
48+
authInfo: options?.authInfo ?? authInfoForClientMessages,
49+
});
50+
};
4151

4252
return {
4353
server,
4454
client,
55+
clientTransport,
56+
serverTransport,
57+
setClientAuthInfo: (authInfo?: AuthInfo) => {
58+
authInfoForClientMessages = authInfo;
59+
},
4560
connect: () =>
4661
Promise.all([
4762
server.connect(serverTransport),

0 commit comments

Comments
 (0)