|
| 1 | +# Copyright 2010 New Relic, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import pytest |
| 16 | +from fastmcp.client import Client |
| 17 | +from fastmcp.client.transports import FastMCPTransport |
| 18 | +from fastmcp.server.server import FastMCP |
| 19 | +from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics |
| 20 | + |
| 21 | +from newrelic.api.background_task import background_task |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def fastmcp_server(): |
| 26 | + server = FastMCP("Test Tools") |
| 27 | + |
| 28 | + @server.tool() |
| 29 | + def add_exclamation(phrase): |
| 30 | + return f"{phrase}!" |
| 31 | + |
| 32 | + @server.resource("greeting://{name}") |
| 33 | + def get_greeting(name): |
| 34 | + return f"Hello, {name}!" |
| 35 | + |
| 36 | + @server.resource("files:///home/user/documents/{filename}") |
| 37 | + def get_file(filename): |
| 38 | + return f"Hello, Python! You requested the file: {filename}." |
| 39 | + |
| 40 | + @server.resource("postgres://python/customers/schema") |
| 41 | + def get_db_info(): |
| 42 | + return "Hello, Python!" |
| 43 | + |
| 44 | + @server.prompt() |
| 45 | + def echo_prompt(message: str): |
| 46 | + return f"Echo this message: {message}" |
| 47 | + |
| 48 | + return server |
| 49 | + |
| 50 | + |
| 51 | +@validate_transaction_metrics( |
| 52 | + "test_mcp:test_tool_tracing", |
| 53 | + scoped_metrics=[("Llm/tool/MCP/mcp.client.session:ClientSession.call_tool/add_exclamation", 1)], |
| 54 | + rollup_metrics=[("Llm/tool/MCP/mcp.client.session:ClientSession.call_tool/add_exclamation", 1)], |
| 55 | + background_task=True, |
| 56 | +) |
| 57 | +@background_task() |
| 58 | +def test_tool_tracing(loop, fastmcp_server): |
| 59 | + async def _test(): |
| 60 | + async with Client(transport=FastMCPTransport(fastmcp_server)) as client: |
| 61 | + # Call the MCP tool, so we can validate the trace naming is correct. |
| 62 | + result = await client.call_tool("add_exclamation", {"phrase": "Python is awesome"}) |
| 63 | + |
| 64 | + content = str(result[0]) |
| 65 | + assert "Python is awesome!" in content |
| 66 | + |
| 67 | + loop.run_until_complete(_test()) |
| 68 | + |
| 69 | + |
| 70 | +# Separate out the test function to work with the transaction metrics validator |
| 71 | +def run_read_resources(loop, fastmcp_server, resource_uri): |
| 72 | + async def _test(): |
| 73 | + async with Client(transport=FastMCPTransport(fastmcp_server)) as client: |
| 74 | + result = await client.read_resource(resource_uri) |
| 75 | + content = str(result[0]) |
| 76 | + assert "Hello, Python!" in content |
| 77 | + |
| 78 | + loop.run_until_complete(_test()) |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.parametrize( |
| 82 | + "resource_uri", |
| 83 | + ("greeting://Python", "files:///home/user/documents/python.pdf", "postgres://python/customers/schema"), |
| 84 | +) |
| 85 | +def test_resource_tracing(loop, fastmcp_server, resource_uri): |
| 86 | + resource_scheme = resource_uri.split(":/")[0] |
| 87 | + |
| 88 | + @validate_transaction_metrics( |
| 89 | + "test_mcp:test_resource_tracing.<locals>._test", |
| 90 | + scoped_metrics=[(f"Llm/resource/MCP/mcp.client.session:ClientSession.read_resource/{resource_scheme}", 1)], |
| 91 | + rollup_metrics=[(f"Llm/resource/MCP/mcp.client.session:ClientSession.read_resource/{resource_scheme}", 1)], |
| 92 | + background_task=True, |
| 93 | + ) |
| 94 | + @background_task() |
| 95 | + def _test(): |
| 96 | + run_read_resources(loop, fastmcp_server, resource_uri) |
| 97 | + |
| 98 | + _test() |
| 99 | + |
| 100 | + |
| 101 | +@validate_transaction_metrics( |
| 102 | + "test_mcp:test_prompt_tracing", |
| 103 | + scoped_metrics=[("Llm/prompt/MCP/mcp.client.session:ClientSession.get_prompt/echo_prompt", 1)], |
| 104 | + rollup_metrics=[("Llm/prompt/MCP/mcp.client.session:ClientSession.get_prompt/echo_prompt", 1)], |
| 105 | + background_task=True, |
| 106 | +) |
| 107 | +@background_task() |
| 108 | +def test_prompt_tracing(loop, fastmcp_server): |
| 109 | + async def _test(): |
| 110 | + async with Client(transport=FastMCPTransport(fastmcp_server)) as client: |
| 111 | + result = await client.get_prompt("echo_prompt", {"message": "Python is cool"}) |
| 112 | + |
| 113 | + content = str(result) |
| 114 | + assert "Python is cool" in content |
| 115 | + |
| 116 | + loop.run_until_complete(_test()) |
0 commit comments