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 
 
 
 
 
 
 
 
 
 
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
"""Tests for FastMCP configuration file support with nested structure."""

import json
import os
from pathlib import Path

import pytest
from pydantic import ValidationError

from fastmcp.utilities.fastmcp_config import (
    DeploymentConfig,
    EntrypointConfig,
    EnvironmentConfig,
    FastMCPConfig,
)


class TestEntrypointConfig:
    """Test EntrypointConfig class."""

    def test_string_entrypoint(self):
        """Test that string entrypoint is converted to EntrypointConfig."""
        config = FastMCPConfig(entrypoint="server.py")
        # With the new validator, this should be converted to EntrypointConfig
        assert isinstance(config.entrypoint, EntrypointConfig)
        assert config.entrypoint.file == "server.py"
        assert config.entrypoint.object is None

        # get_entrypoint should return the same object
        entrypoint = config.get_entrypoint()
        assert isinstance(entrypoint, EntrypointConfig)
        assert entrypoint.file == "server.py"
        assert entrypoint.object is None

    def test_string_entrypoint_with_object(self):
        """Test string entrypoint with :object syntax."""
        config = FastMCPConfig(entrypoint="server.py:app")
        # With the new validator, this should be converted to EntrypointConfig
        assert isinstance(config.entrypoint, EntrypointConfig)
        assert config.entrypoint.file == "server.py"
        assert config.entrypoint.object == "app"

        # get_entrypoint should return the same object
        entrypoint = config.get_entrypoint()
        assert isinstance(entrypoint, EntrypointConfig)
        assert entrypoint.file == "server.py"
        assert entrypoint.object == "app"

    def test_object_entrypoint(self):
        """Test EntrypointConfig object format."""
        config = FastMCPConfig(
            entrypoint=EntrypointConfig(file="src/server.py", object="mcp")
        )
        assert isinstance(config.entrypoint, EntrypointConfig)
        assert config.entrypoint.file == "src/server.py"
        assert config.entrypoint.object == "mcp"

    def test_get_entrypoint_path_resolution(self, tmp_path):
        """Test that get_entrypoint resolves paths relative to config file."""
        config_dir = tmp_path / "config"
        config_dir.mkdir()
        server_dir = tmp_path / "src"
        server_dir.mkdir()
        server_file = server_dir / "server.py"
        server_file.write_text("# server")

        config = FastMCPConfig(entrypoint="../src/server.py")
        entrypoint = config.get_entrypoint(config_dir / "fastmcp.json")

        # Should resolve to absolute path
        assert Path(entrypoint.file).is_absolute()
        assert Path(entrypoint.file) == server_file.resolve()


class TestEnvironmentConfig:
    """Test EnvironmentConfig class."""

    def test_environment_config_fields(self):
        """Test all EnvironmentConfig fields."""
        config = FastMCPConfig(
            entrypoint="server.py",
            environment={
                "python": "3.12",
                "dependencies": ["requests", "numpy>=2.0"],
                "requirements": "requirements.txt",
                "project": ".",
                "editable": "../my-package",
            },
        )

        env = config.environment
        assert env.python == "3.12"
        assert env.dependencies == ["requests", "numpy>=2.0"]
        assert env.requirements == "requirements.txt"
        assert env.project == "."
        assert env.editable == "../my-package"

    def test_needs_uv(self):
        """Test needs_uv() method."""
        # No environment config - doesn't need UV
        config = FastMCPConfig(entrypoint="server.py")
        assert not config.environment.needs_uv()

        # Empty environment - doesn't need UV
        config = FastMCPConfig(entrypoint="server.py", environment={})
        assert not config.environment.needs_uv()

        # With dependencies - needs UV
        config = FastMCPConfig(
            entrypoint="server.py", environment={"dependencies": ["requests"]}
        )
        assert config.environment.needs_uv()

        # With Python version - needs UV
        config = FastMCPConfig(entrypoint="server.py", environment={"python": "3.12"})
        assert config.environment.needs_uv()

    def test_build_uv_args(self):
        """Test build_uv_args() method."""
        config = FastMCPConfig(
            entrypoint="server.py",
            environment={
                "python": "3.12",
                "dependencies": ["requests", "numpy"],
                "requirements": "requirements.txt",
                "project": ".",
            },
        )

        args = config.environment.build_uv_args(["fastmcp", "run", "server.py"])

        assert args[0] == "run"
        assert "--python" in args
        assert "3.12" in args
        assert "--project" in args
        assert "--with" in args
        assert "fastmcp" in args
        assert "requests" in args
        assert "numpy" in args
        assert "--with-requirements" in args
        assert "requirements.txt" in args
        assert "fastmcp" in args[-3:]
        assert "run" in args[-2:]
        assert "server.py" in args[-1:]

    def test_merge_with_cli_args(self):
        """Test merge_with_cli_args() method."""
        config = FastMCPConfig(
            entrypoint="server.py",
            environment={
                "python": "3.11",
                "dependencies": ["requests"],
            },
        )

        # CLI args should take precedence
        merged = config.environment.merge_with_cli_args(
            python="3.12",  # Override
            with_packages=["numpy"],  # Add to dependencies
            with_requirements=None,
            project=None,
        )

        assert merged["python"] == "3.12"  # CLI override
        assert set(merged["with_packages"]) == {"requests", "numpy"}  # Merged
        assert merged["with_requirements"] is None
        assert merged["project"] is None

    def test_run_with_uv(self):
        """Test run_with_uv() subprocess execution."""
        config = FastMCPConfig(
            entrypoint="server.py", environment={"dependencies": ["requests"]}
        )

        # run_with_uv calls sys.exit, so we expect SystemExit
        with pytest.raises(SystemExit) as exc_info:
            # This will fail because we're running exit(1)
            # but it tests that the subprocess is called correctly
            config.environment.run_with_uv(["python", "-c", "exit(1)"])

        # Check that it exited with code 1
        assert exc_info.value.code == 1


class TestDeploymentConfig:
    """Test DeploymentConfig class."""

    def test_deployment_config_fields(self):
        """Test all DeploymentConfig fields."""
        config = FastMCPConfig(
            entrypoint="server.py",
            deployment={
                "transport": "http",
                "host": "0.0.0.0",
                "port": 8000,
                "path": "/api/",
                "log_level": "DEBUG",
                "env": {"API_KEY": "secret"},
                "cwd": "./work",
                "args": ["--debug"],
            },
        )

        deploy = config.deployment
        assert deploy.transport == "http"
        assert deploy.host == "0.0.0.0"
        assert deploy.port == 8000
        assert deploy.path == "/api/"
        assert deploy.log_level == "DEBUG"
        assert deploy.env == {"API_KEY": "secret"}
        assert deploy.cwd == "./work"
        assert deploy.args == ["--debug"]

    def test_merge_with_cli_args(self):
        """Test DeploymentConfig merge_with_cli_args() method."""
        config = FastMCPConfig(
            entrypoint="server.py",
            deployment={
                "transport": "stdio",
                "port": 3000,
                "log_level": "INFO",
            },
        )

        # CLI args should take precedence
        merged = config.deployment.merge_with_cli_args(
            transport="http",  # Override
            host="localhost",  # New value
            port=None,  # Keep config value
            path=None,
            log_level="DEBUG",  # Override
            server_args=["--test"],
        )

        assert merged["transport"] == "http"  # CLI override
        assert merged["host"] == "localhost"  # CLI value
        assert merged["port"] == 3000  # Config value (CLI was None)
        assert merged["log_level"] == "DEBUG"  # CLI override
        assert merged["server_args"] == ["--test"]  # CLI value

    def test_apply_runtime_settings(self, tmp_path):
        """Test apply_runtime_settings() method."""
        import os

        # Create config with env vars and cwd
        work_dir = tmp_path / "work"
        work_dir.mkdir()

        config = FastMCPConfig(
            entrypoint="server.py",
            deployment={
                "env": {"TEST_VAR": "test_value"},
                "cwd": "work",
            },
        )

        original_cwd = os.getcwd()
        original_env = os.environ.get("TEST_VAR")

        try:
            config.deployment.apply_runtime_settings(tmp_path / "fastmcp.json")

            # Check environment variable was set
            assert os.environ["TEST_VAR"] == "test_value"

            # Check working directory was changed
            assert Path.cwd() == work_dir.resolve()

        finally:
            # Restore original state
            os.chdir(original_cwd)
            if original_env is None:
                os.environ.pop("TEST_VAR", None)
            else:
                os.environ["TEST_VAR"] = original_env

    def test_env_var_interpolation(self, tmp_path):
        """Test environment variable interpolation in deployment env."""
        import os

        # Set up test environment variables
        os.environ["BASE_URL"] = "example.com"
        os.environ["ENV_NAME"] = "production"

        config = FastMCPConfig(
            entrypoint="server.py",
            deployment={
                "env": {
                    "API_URL": "https://api.${BASE_URL}/v1",
                    "DATABASE": "postgres://${ENV_NAME}.db",
                    "PREFIXED": "MY_${ENV_NAME}_SERVER",
                    "MISSING": "value_${NONEXISTENT}_here",
                    "STATIC": "no_interpolation",
                }
            },
        )

        original_values = {
            key: os.environ.get(key)
            for key in ["API_URL", "DATABASE", "PREFIXED", "MISSING", "STATIC"]
        }

        try:
            config.deployment.apply_runtime_settings()

            # Check interpolated values
            assert os.environ["API_URL"] == "https://api.example.com/v1"
            assert os.environ["DATABASE"] == "postgres://production.db"
            assert os.environ["PREFIXED"] == "MY_production_SERVER"
            # Missing variables should keep the placeholder
            assert os.environ["MISSING"] == "value_${NONEXISTENT}_here"
            # Static values should remain unchanged
            assert os.environ["STATIC"] == "no_interpolation"

        finally:
            # Clean up
            os.environ.pop("BASE_URL", None)
            os.environ.pop("ENV_NAME", None)
            for key, value in original_values.items():
                if value is None:
                    os.environ.pop(key, None)
                else:
                    os.environ[key] = value


class TestFastMCPConfig:
    """Test FastMCPConfig root configuration."""

    def test_minimal_config(self):
        """Test creating a config with only required fields."""
        config = FastMCPConfig(entrypoint="server.py")
        assert isinstance(config.entrypoint, EntrypointConfig)
        assert config.entrypoint.file == "server.py"
        assert config.entrypoint.object is None
        # Environment and deployment are now always present but empty
        assert isinstance(config.environment, EnvironmentConfig)
        assert isinstance(config.deployment, DeploymentConfig)
        # Check they have no values set
        assert not config.environment.needs_uv()
        assert all(
            getattr(config.deployment, field, None) is None
            for field in DeploymentConfig.model_fields
        )

    def test_nested_structure(self):
        """Test the nested configuration structure."""
        config = FastMCPConfig(
            entrypoint="server.py",
            environment={
                "python": "3.12",
                "dependencies": ["fastmcp"],
            },
            deployment={
                "transport": "stdio",
                "log_level": "INFO",
            },
        )

        assert isinstance(config.entrypoint, EntrypointConfig)
        assert config.entrypoint.file == "server.py"
        assert config.entrypoint.object is None
        assert isinstance(config.environment, EnvironmentConfig)
        assert isinstance(config.deployment, DeploymentConfig)

    def test_from_file(self, tmp_path):
        """Test loading config from JSON file with nested structure."""
        config_data = {
            "$schema": "https://gofastmcp.com/schemas/fastmcp_config/v1.json",
            "entrypoint": {"file": "src/server.py", "object": "app"},
            "environment": {"python": "3.12", "dependencies": ["requests"]},
            "deployment": {"transport": "http", "port": 8000},
        }

        config_file = tmp_path / "fastmcp.json"
        config_file.write_text(json.dumps(config_data))

        config = FastMCPConfig.from_file(config_file)

        # When loaded from JSON with object format, it becomes EntrypointConfig
        assert isinstance(config.entrypoint, EntrypointConfig)
        assert config.entrypoint.file == "src/server.py"
        assert config.entrypoint.object == "app"
        assert config.environment.python == "3.12"
        assert config.environment.dependencies == ["requests"]
        assert config.deployment.transport == "http"
        assert config.deployment.port == 8000

    def test_from_file_with_string_entrypoint(self, tmp_path):
        """Test loading config with string entrypoint."""
        config_data = {
            "entrypoint": "server.py:mcp",
            "environment": {"dependencies": ["fastmcp"]},
        }

        config_file = tmp_path / "fastmcp.json"
        config_file.write_text(json.dumps(config_data))

        config = FastMCPConfig.from_file(config_file)
        # String entrypoint with : should be converted to EntrypointConfig
        assert isinstance(config.entrypoint, EntrypointConfig)
        assert config.entrypoint.file == "server.py"
        assert config.entrypoint.object == "mcp"

        # get_entrypoint should return the same
        entrypoint = config.get_entrypoint()
        assert entrypoint.file == "server.py"
        assert entrypoint.object == "mcp"

    def test_string_entrypoint_with_object_and_environment(self, tmp_path):
        """Test that file.py:object syntax works with environment config."""
        config_data = {
            "entrypoint": "src/server.py:app",
            "environment": {"python": "3.12", "dependencies": ["fastmcp", "requests"]},
            "deployment": {"transport": "http", "port": 8000},
        }

        config_file = tmp_path / "fastmcp.json"
        config_file.write_text(json.dumps(config_data))

        config = FastMCPConfig.from_file(config_file)

        # Should be parsed into EntrypointConfig
        assert isinstance(config.entrypoint, EntrypointConfig)
        assert config.entrypoint.file == "src/server.py"
        assert config.entrypoint.object == "app"

        # Environment config should still work
        assert config.environment.python == "3.12"
        assert config.environment.dependencies == ["fastmcp", "requests"]

        # Deployment config should still work
        assert config.deployment.transport == "http"
        assert config.deployment.port == 8000

    def test_find_config_in_current_dir(self, tmp_path):
        """Test finding config in current directory."""
        config_file = tmp_path / "fastmcp.json"
        config_file.write_text(json.dumps({"entrypoint": "server.py"}))

        original_cwd = os.getcwd()
        try:
            os.chdir(tmp_path)
            found = FastMCPConfig.find_config()
            assert found == config_file
        finally:
            os.chdir(original_cwd)

    def test_find_config_not_in_parent_dir(self, tmp_path):
        """Test that config is NOT found in parent directory."""
        config_file = tmp_path / "fastmcp.json"
        config_file.write_text(json.dumps({"entrypoint": "server.py"}))

        subdir = tmp_path / "subdir"
        subdir.mkdir()

        # Should NOT find config in parent directory
        found = FastMCPConfig.find_config(subdir)
        assert found is None

    def test_find_config_in_specified_dir(self, tmp_path):
        """Test finding config in the specified directory."""
        config_file = tmp_path / "fastmcp.json"
        config_file.write_text(json.dumps({"entrypoint": "server.py"}))

        # Should find config when looking in the directory that contains it
        found = FastMCPConfig.find_config(tmp_path)
        assert found == config_file

    def test_find_config_not_found(self, tmp_path):
        """Test when config is not found."""
        found = FastMCPConfig.find_config(tmp_path)
        assert found is None

    def test_invalid_transport(self, tmp_path):
        """Test loading config with invalid transport value."""
        config_data = {
            "entrypoint": "server.py",
            "deployment": {"transport": "invalid_transport"},
        }

        config_file = tmp_path / "fastmcp.json"
        config_file.write_text(json.dumps(config_data))

        with pytest.raises(ValidationError):
            FastMCPConfig.from_file(config_file)

    def test_optional_sections(self):
        """Test that all config sections are optional except entrypoint."""
        # Only entrypoint is required
        config = FastMCPConfig(entrypoint="server.py")
        assert isinstance(config.entrypoint, EntrypointConfig)
        assert config.entrypoint.file == "server.py"
        # Environment and deployment are now always present but may be empty
        assert isinstance(config.environment, EnvironmentConfig)
        assert isinstance(config.deployment, DeploymentConfig)

        # Only environment with values
        config = FastMCPConfig(entrypoint="server.py", environment={"python": "3.12"})
        assert config.environment.python == "3.12"
        assert isinstance(config.deployment, DeploymentConfig)
        assert all(
            getattr(config.deployment, field, None) is None
            for field in DeploymentConfig.model_fields
        )

        # Only deployment with values
        config = FastMCPConfig(entrypoint="server.py", deployment={"transport": "http"})
        assert isinstance(config.environment, EnvironmentConfig)
        assert all(
            getattr(config.environment, field, None) is None
            for field in EnvironmentConfig.model_fields
        )
        assert config.deployment.transport == "http"