Skip to content

Commit d81fe87

Browse files
amychisholm03bizob2828
authored andcommitted
feat: Disable MCP if ai_monitoring disabled (#3276)
1 parent 14ff618 commit d81fe87

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

lib/subscribers/mcp-sdk/client.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ class McpClientSubscriber extends Subscriber {
1212
super({ agent, logger, packageName: '@modelcontextprotocol/sdk', channelName })
1313
this.events = ['asyncEnd']
1414
this.requireActiveTx = true
15-
this.segmentName = ''
15+
this.segmentName = 'Unknown'
16+
}
17+
18+
get enabled() {
19+
return this.config.instrumentation[this.packageName].enabled === true &&
20+
this.config.ai_monitoring.enabled === true
1621
}
1722

1823
handler(ctx) {

test/unit/shimmer.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,8 @@ test('Shimmer with logger mock', async (t) => {
824824

825825
require(TEST_MODULE_RELATIVE_PATH)
826826
const version = shimmer.getPackageVersion(TEST_MODULE_PATH)
827-
assert.ok(!loggerMock.debug.callCount)
827+
const found = loggerMock.debug.args.find(debugArgs => debugArgs?.[0]?.includes('Failed to get version for `%s`, reason: %s'))
828+
assert.equal(undefined, found)
828829
assert.equal(version, '0.0.1', 'should get package version from package.json')
829830
})
830831

@@ -833,7 +834,7 @@ test('Shimmer with logger mock', async (t) => {
833834
() => {
834835
const version = shimmer.getPackageVersion('bogus')
835836
assert.equal(version, process.version)
836-
assert.deepEqual(loggerMock.debug.args[0], [
837+
assert.deepEqual(loggerMock.debug.args[loggerMock.debug.args.length - 1], [
837838
'Failed to get version for `%s`, reason: %s',
838839
'bogus',
839840
"no tracked items for module 'bogus'"

test/versioned/mcp-sdk/client.test.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ const {
1818

1919
test.beforeEach(async (ctx) => {
2020
ctx.nr = {}
21-
ctx.nr.agent = helper.instrumentMockedAgent({})
21+
ctx.nr.agent = helper.instrumentMockedAgent({
22+
ai_monitoring: {
23+
enabled: ctx.name.includes('disabled') ? false : true
24+
}
25+
})
2226

2327
const { Client } = require('@modelcontextprotocol/sdk/client/index.js')
2428
const { StdioClientTransport } = require('@modelcontextprotocol/sdk/client/stdio.js')
@@ -54,6 +58,7 @@ test('should create span for callTool', async (t) => {
5458
assert.ok(result, 'should return a result from the tool call')
5559

5660
const name = `${MCP.TOOL}/callTool/echo`
61+
assert.equal(tx.trace.transaction.numSegments, 3, 'should have 3 segments')
5762
assertSegments(tx.trace, tx.trace.root, [name], { exact: false })
5863

5964
tx.end()
@@ -112,3 +117,22 @@ test('should create span for getPrompt', async (t) => {
112117
})
113118
})
114119
})
120+
121+
test('should not instrument if ai_monitoring is disabled', async (t) => {
122+
const { agent, client } = t.nr
123+
124+
await helper.runInTransaction(agent, async (tx) => {
125+
const result = await client.callTool({
126+
name: 'echo',
127+
arguments: {
128+
message: 'example message'
129+
}
130+
})
131+
132+
assert.ok(result, 'should still return a result from the tool call')
133+
134+
assert.equal(tx.trace.transaction.numSegments, 2, 'should not create MCP segment')
135+
136+
tx.end()
137+
})
138+
})

0 commit comments

Comments
 (0)