/ test / python / testapi / testmcp.py
testmcp.py
 1  """
 2  Agent API module tests
 3  """
 4  
 5  import os
 6  import tempfile
 7  import unittest
 8  
 9  from unittest.mock import patch
10  
11  from fastapi.testclient import TestClient
12  
13  from txtai.api import application
14  
15  # Configuration for agents
16  MCP = """
17  mcp: True
18  """
19  
20  
21  # pylint: disable=R0904
22  class TestMCP(unittest.TestCase):
23      """
24      API tests for model context protocol (MCP)
25      """
26  
27      @staticmethod
28      @patch.dict(os.environ, {"CONFIG": os.path.join(tempfile.gettempdir(), "testapi.yml"), "API_CLASS": "txtai.api.API"})
29      def start():
30          """
31          Starts a mock FastAPI client.
32          """
33  
34          config = os.path.join(tempfile.gettempdir(), "testapi.yml")
35  
36          with open(config, "w", encoding="utf-8") as output:
37              output.write(MCP)
38  
39          # Create new application and set on client
40          application.app = application.create()
41          client = TestClient(application.app)
42          application.start()
43  
44          return client
45  
46      @classmethod
47      def setUpClass(cls):
48          """
49          Create API client on creation of class.
50          """
51  
52          cls.client = TestMCP.start()
53  
54      def testMCP(self):
55          """
56          Test that application a /mcp route
57          """
58  
59          self.assertTrue(any(route.path == "/mcp" for route in self.client.app.routes))