nvidia-nat 1.4.0a20251120__py3-none-any.whl → 1.4.0a20260113__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- aiq/__init__.py +1 -1
- nat/{front_ends/mcp → agent/auto_memory_wrapper}/__init__.py +1 -1
- nat/agent/auto_memory_wrapper/agent.py +278 -0
- nat/agent/auto_memory_wrapper/register.py +227 -0
- nat/agent/auto_memory_wrapper/state.py +30 -0
- nat/agent/base.py +1 -1
- nat/agent/dual_node.py +1 -1
- nat/agent/prompt_optimizer/prompt.py +1 -1
- nat/agent/prompt_optimizer/register.py +1 -1
- nat/agent/react_agent/agent.py +16 -9
- nat/agent/react_agent/output_parser.py +2 -2
- nat/agent/react_agent/prompt.py +3 -2
- nat/agent/react_agent/register.py +2 -2
- nat/agent/react_agent/register_per_user_agent.py +104 -0
- nat/agent/reasoning_agent/reasoning_agent.py +1 -1
- nat/agent/register.py +3 -1
- nat/agent/responses_api_agent/__init__.py +1 -1
- nat/agent/responses_api_agent/register.py +1 -1
- nat/agent/rewoo_agent/agent.py +9 -4
- nat/agent/rewoo_agent/prompt.py +1 -1
- nat/agent/rewoo_agent/register.py +1 -1
- nat/agent/tool_calling_agent/agent.py +5 -4
- nat/agent/tool_calling_agent/register.py +1 -1
- nat/authentication/__init__.py +1 -1
- nat/authentication/api_key/__init__.py +1 -1
- nat/authentication/api_key/api_key_auth_provider.py +1 -1
- nat/authentication/api_key/api_key_auth_provider_config.py +22 -7
- nat/authentication/api_key/register.py +1 -1
- nat/authentication/credential_validator/__init__.py +1 -1
- nat/authentication/credential_validator/bearer_token_validator.py +1 -1
- nat/authentication/exceptions/__init__.py +1 -1
- nat/authentication/exceptions/api_key_exceptions.py +1 -1
- nat/authentication/http_basic_auth/http_basic_auth_provider.py +1 -1
- nat/authentication/http_basic_auth/register.py +1 -1
- nat/authentication/interfaces.py +1 -1
- nat/authentication/oauth2/__init__.py +1 -1
- nat/authentication/oauth2/oauth2_auth_code_flow_provider.py +1 -1
- nat/authentication/oauth2/oauth2_auth_code_flow_provider_config.py +1 -1
- nat/authentication/oauth2/oauth2_resource_server_config.py +1 -1
- nat/authentication/oauth2/register.py +1 -1
- nat/authentication/register.py +1 -1
- nat/builder/builder.py +511 -1
- nat/builder/child_builder.py +385 -0
- nat/builder/component_utils.py +28 -4
- nat/builder/context.py +17 -1
- nat/builder/embedder.py +1 -1
- nat/builder/eval_builder.py +19 -7
- nat/builder/evaluator.py +1 -1
- nat/builder/framework_enum.py +2 -1
- nat/builder/front_end.py +1 -1
- nat/builder/function.py +40 -3
- nat/builder/function_base.py +1 -1
- nat/builder/function_info.py +1 -1
- nat/builder/intermediate_step_manager.py +1 -1
- nat/builder/llm.py +1 -1
- nat/builder/per_user_workflow_builder.py +843 -0
- nat/builder/retriever.py +1 -1
- nat/builder/sync_builder.py +571 -0
- nat/builder/user_interaction_manager.py +1 -1
- nat/builder/workflow.py +1 -1
- nat/builder/workflow_builder.py +536 -424
- nat/cli/__init__.py +1 -1
- nat/cli/cli_utils/config_override.py +1 -1
- nat/cli/cli_utils/validation.py +32 -1
- nat/cli/commands/configure/channel/add.py +1 -1
- nat/cli/commands/configure/channel/channel.py +1 -1
- nat/cli/commands/configure/channel/remove.py +1 -1
- nat/cli/commands/configure/channel/update.py +1 -1
- nat/cli/commands/configure/configure.py +1 -1
- nat/cli/commands/evaluate.py +87 -13
- nat/cli/commands/finetune.py +132 -0
- nat/cli/commands/info/__init__.py +1 -1
- nat/cli/commands/info/info.py +1 -1
- nat/cli/commands/info/list_channels.py +1 -1
- nat/cli/commands/info/list_components.py +1 -1
- nat/cli/commands/object_store/__init__.py +1 -1
- nat/cli/commands/object_store/object_store.py +1 -1
- nat/cli/commands/optimize.py +1 -1
- nat/cli/commands/{mcp → red_teaming}/__init__.py +1 -1
- nat/cli/commands/red_teaming/red_teaming.py +138 -0
- nat/cli/commands/red_teaming/red_teaming_utils.py +73 -0
- nat/cli/commands/registry/__init__.py +1 -1
- nat/cli/commands/registry/publish.py +1 -1
- nat/cli/commands/registry/pull.py +1 -1
- nat/cli/commands/registry/registry.py +1 -1
- nat/cli/commands/registry/remove.py +1 -1
- nat/cli/commands/registry/search.py +1 -1
- nat/cli/commands/sizing/__init__.py +1 -1
- nat/cli/commands/sizing/calc.py +1 -1
- nat/cli/commands/sizing/sizing.py +1 -1
- nat/cli/commands/start.py +1 -1
- nat/cli/commands/uninstall.py +1 -1
- nat/cli/commands/validate.py +1 -1
- nat/cli/commands/workflow/__init__.py +1 -1
- nat/cli/commands/workflow/workflow.py +1 -1
- nat/cli/commands/workflow/workflow_commands.py +3 -2
- nat/cli/entrypoint.py +15 -37
- nat/cli/main.py +2 -2
- nat/cli/plugin_loader.py +69 -0
- nat/cli/register_workflow.py +183 -5
- nat/cli/type_registry.py +169 -3
- nat/control_flow/register.py +1 -1
- nat/control_flow/router_agent/agent.py +1 -1
- nat/control_flow/router_agent/prompt.py +1 -1
- nat/control_flow/router_agent/register.py +1 -1
- nat/control_flow/sequential_executor.py +28 -7
- nat/data_models/__init__.py +1 -1
- nat/data_models/agent.py +1 -1
- nat/data_models/api_server.py +38 -3
- nat/data_models/authentication.py +1 -1
- nat/data_models/common.py +1 -1
- nat/data_models/component.py +7 -1
- nat/data_models/component_ref.py +34 -1
- nat/data_models/config.py +62 -1
- nat/data_models/dataset_handler.py +15 -2
- nat/data_models/discovery_metadata.py +1 -1
- nat/data_models/embedder.py +1 -1
- nat/data_models/evaluate.py +6 -1
- nat/data_models/evaluator.py +1 -1
- nat/data_models/finetuning.py +260 -0
- nat/data_models/front_end.py +1 -1
- nat/data_models/function.py +1 -1
- nat/data_models/function_dependencies.py +1 -1
- nat/data_models/gated_field_mixin.py +1 -1
- nat/data_models/interactive.py +1 -1
- nat/data_models/intermediate_step.py +29 -2
- nat/data_models/invocation_node.py +1 -1
- nat/data_models/llm.py +1 -1
- nat/data_models/logging.py +1 -1
- nat/data_models/memory.py +1 -1
- nat/data_models/middleware.py +3 -1
- nat/data_models/object_store.py +1 -1
- nat/data_models/openai_mcp.py +1 -1
- nat/data_models/optimizable.py +1 -1
- nat/data_models/optimizer.py +1 -1
- nat/data_models/profiler.py +1 -1
- nat/data_models/registry_handler.py +1 -1
- nat/data_models/retriever.py +1 -1
- nat/data_models/retry_mixin.py +1 -1
- nat/data_models/runtime_enum.py +1 -1
- nat/data_models/span.py +1 -1
- nat/data_models/step_adaptor.py +1 -1
- nat/data_models/streaming.py +1 -1
- nat/data_models/swe_bench_model.py +1 -1
- nat/data_models/telemetry_exporter.py +1 -1
- nat/data_models/thinking_mixin.py +1 -1
- nat/data_models/ttc_strategy.py +1 -1
- nat/embedder/azure_openai_embedder.py +1 -1
- nat/embedder/nim_embedder.py +1 -1
- nat/embedder/openai_embedder.py +1 -1
- nat/embedder/register.py +1 -1
- nat/eval/__init__.py +1 -1
- nat/eval/config.py +8 -1
- nat/eval/dataset_handler/dataset_downloader.py +1 -1
- nat/eval/dataset_handler/dataset_filter.py +1 -1
- nat/eval/dataset_handler/dataset_handler.py +4 -2
- nat/eval/evaluate.py +217 -80
- nat/eval/evaluator/__init__.py +1 -1
- nat/eval/evaluator/base_evaluator.py +2 -2
- nat/eval/evaluator/evaluator_model.py +3 -2
- nat/eval/intermediate_step_adapter.py +1 -1
- nat/eval/llm_validator.py +336 -0
- nat/eval/rag_evaluator/evaluate.py +17 -10
- nat/eval/rag_evaluator/register.py +1 -1
- nat/eval/red_teaming_evaluator/__init__.py +14 -0
- nat/eval/red_teaming_evaluator/data_models.py +66 -0
- nat/eval/red_teaming_evaluator/evaluate.py +327 -0
- nat/eval/red_teaming_evaluator/filter_conditions.py +75 -0
- nat/eval/red_teaming_evaluator/register.py +55 -0
- nat/eval/register.py +2 -1
- nat/eval/remote_workflow.py +1 -1
- nat/eval/runners/__init__.py +1 -1
- nat/eval/runners/config.py +1 -1
- nat/eval/runners/multi_eval_runner.py +1 -1
- nat/eval/runners/red_teaming_runner/__init__.py +24 -0
- nat/eval/runners/red_teaming_runner/config.py +282 -0
- nat/eval/runners/red_teaming_runner/report_utils.py +707 -0
- nat/eval/runners/red_teaming_runner/runner.py +867 -0
- nat/eval/runtime_evaluator/__init__.py +1 -1
- nat/eval/runtime_evaluator/evaluate.py +1 -1
- nat/eval/runtime_evaluator/register.py +1 -1
- nat/eval/runtime_event_subscriber.py +1 -1
- nat/eval/swe_bench_evaluator/evaluate.py +1 -1
- nat/eval/swe_bench_evaluator/register.py +1 -1
- nat/eval/trajectory_evaluator/evaluate.py +2 -2
- nat/eval/trajectory_evaluator/register.py +1 -1
- nat/eval/tunable_rag_evaluator/evaluate.py +5 -5
- nat/eval/tunable_rag_evaluator/register.py +1 -1
- nat/eval/usage_stats.py +1 -1
- nat/eval/utils/eval_trace_ctx.py +1 -1
- nat/eval/utils/output_uploader.py +1 -1
- nat/eval/utils/tqdm_position_registry.py +1 -1
- nat/eval/utils/weave_eval.py +1 -1
- nat/experimental/decorators/experimental_warning_decorator.py +1 -1
- nat/experimental/test_time_compute/editing/iterative_plan_refinement_editor.py +1 -1
- nat/experimental/test_time_compute/editing/llm_as_a_judge_editor.py +1 -1
- nat/experimental/test_time_compute/editing/motivation_aware_summarization.py +1 -1
- nat/experimental/test_time_compute/functions/execute_score_select_function.py +1 -1
- nat/experimental/test_time_compute/functions/multi_llm_judge_function.py +88 -0
- nat/experimental/test_time_compute/functions/plan_select_execute_function.py +1 -1
- nat/experimental/test_time_compute/functions/ttc_tool_orchestration_function.py +1 -1
- nat/experimental/test_time_compute/functions/ttc_tool_wrapper_function.py +1 -1
- nat/experimental/test_time_compute/models/editor_config.py +1 -1
- nat/experimental/test_time_compute/models/scoring_config.py +1 -1
- nat/experimental/test_time_compute/models/search_config.py +20 -2
- nat/experimental/test_time_compute/models/selection_config.py +33 -2
- nat/experimental/test_time_compute/models/stage_enums.py +1 -1
- nat/experimental/test_time_compute/models/strategy_base.py +1 -1
- nat/experimental/test_time_compute/models/tool_use_config.py +1 -1
- nat/experimental/test_time_compute/models/ttc_item.py +1 -1
- nat/experimental/test_time_compute/register.py +4 -1
- nat/experimental/test_time_compute/scoring/llm_based_agent_scorer.py +1 -1
- nat/experimental/test_time_compute/scoring/llm_based_plan_scorer.py +1 -1
- nat/experimental/test_time_compute/scoring/motivation_aware_scorer.py +1 -1
- nat/experimental/test_time_compute/search/multi_llm_generation.py +115 -0
- nat/experimental/test_time_compute/search/multi_llm_planner.py +1 -1
- nat/experimental/test_time_compute/search/multi_query_retrieval_search.py +1 -1
- nat/experimental/test_time_compute/search/single_shot_multi_plan_planner.py +1 -1
- nat/experimental/test_time_compute/selection/best_of_n_selector.py +1 -1
- nat/experimental/test_time_compute/selection/llm_based_agent_output_selector.py +1 -1
- nat/experimental/test_time_compute/selection/llm_based_output_merging_selector.py +1 -1
- nat/experimental/test_time_compute/selection/llm_based_plan_selector.py +1 -1
- nat/experimental/test_time_compute/selection/llm_judge_selection.py +127 -0
- nat/experimental/test_time_compute/selection/threshold_selector.py +1 -1
- nat/finetuning/__init__.py +24 -0
- nat/finetuning/finetuning_runtime.py +143 -0
- nat/finetuning/interfaces/__init__.py +24 -0
- nat/finetuning/interfaces/finetuning_runner.py +261 -0
- nat/finetuning/interfaces/trainer_adapter.py +103 -0
- nat/finetuning/interfaces/trajectory_builder.py +115 -0
- nat/finetuning/utils/__init__.py +15 -0
- nat/finetuning/utils/parsers/__init__.py +15 -0
- nat/finetuning/utils/parsers/adk_parser.py +141 -0
- nat/finetuning/utils/parsers/base_parser.py +238 -0
- nat/finetuning/utils/parsers/common.py +91 -0
- nat/finetuning/utils/parsers/langchain_parser.py +267 -0
- nat/finetuning/utils/parsers/llama_index_parser.py +218 -0
- nat/front_ends/__init__.py +1 -1
- nat/front_ends/console/__init__.py +1 -1
- nat/front_ends/console/authentication_flow_handler.py +1 -1
- nat/front_ends/console/console_front_end_config.py +4 -1
- nat/front_ends/console/console_front_end_plugin.py +5 -4
- nat/front_ends/console/register.py +1 -1
- nat/front_ends/cron/__init__.py +1 -1
- nat/front_ends/fastapi/__init__.py +1 -1
- nat/front_ends/fastapi/async_job.py +128 -0
- nat/front_ends/fastapi/auth_flow_handlers/http_flow_handler.py +1 -1
- nat/front_ends/fastapi/auth_flow_handlers/websocket_flow_handler.py +13 -9
- nat/front_ends/fastapi/dask_client_mixin.py +1 -1
- nat/front_ends/fastapi/fastapi_front_end_config.py +1 -1
- nat/front_ends/fastapi/fastapi_front_end_controller.py +1 -1
- nat/front_ends/fastapi/fastapi_front_end_plugin.py +25 -30
- nat/front_ends/fastapi/fastapi_front_end_plugin_worker.py +195 -60
- nat/front_ends/fastapi/html_snippets/__init__.py +1 -1
- nat/front_ends/fastapi/html_snippets/auth_code_grant_success.py +1 -1
- nat/front_ends/fastapi/intermediate_steps_subscriber.py +12 -1
- nat/front_ends/fastapi/job_store.py +23 -11
- nat/front_ends/fastapi/main.py +1 -1
- nat/front_ends/fastapi/message_handler.py +27 -4
- nat/front_ends/fastapi/message_validator.py +54 -2
- nat/front_ends/fastapi/register.py +1 -1
- nat/front_ends/fastapi/response_helpers.py +16 -15
- nat/front_ends/fastapi/step_adaptor.py +1 -1
- nat/front_ends/fastapi/utils.py +1 -1
- nat/front_ends/register.py +1 -2
- nat/front_ends/simple_base/__init__.py +1 -1
- nat/front_ends/simple_base/simple_front_end_plugin_base.py +6 -4
- nat/llm/aws_bedrock_llm.py +1 -1
- nat/llm/azure_openai_llm.py +10 -1
- nat/llm/dynamo_llm.py +363 -0
- nat/llm/huggingface_llm.py +177 -0
- nat/llm/litellm_llm.py +1 -1
- nat/llm/nim_llm.py +1 -1
- nat/llm/openai_llm.py +1 -1
- nat/llm/register.py +3 -1
- nat/llm/utils/__init__.py +1 -1
- nat/llm/utils/env_config_value.py +1 -1
- nat/llm/utils/error.py +1 -1
- nat/llm/utils/thinking.py +1 -1
- nat/memory/__init__.py +1 -1
- nat/memory/interfaces.py +1 -1
- nat/memory/models.py +1 -1
- nat/meta/pypi.md +1 -1
- nat/middleware/__init__.py +5 -5
- nat/middleware/cache/__init__.py +14 -0
- nat/middleware/{cache_middleware.py → cache/cache_middleware.py} +39 -42
- nat/middleware/cache/cache_middleware_config.py +44 -0
- nat/middleware/cache/register.py +33 -0
- nat/middleware/defense/__init__.py +14 -0
- nat/middleware/defense/defense_middleware.py +362 -0
- nat/middleware/defense/defense_middleware_content_guard.py +455 -0
- nat/middleware/defense/defense_middleware_data_models.py +91 -0
- nat/middleware/defense/defense_middleware_output_verifier.py +440 -0
- nat/middleware/defense/defense_middleware_pii.py +356 -0
- nat/middleware/defense/register.py +82 -0
- nat/middleware/dynamic/__init__.py +14 -0
- nat/middleware/dynamic/dynamic_function_middleware.py +962 -0
- nat/middleware/dynamic/dynamic_middleware_config.py +132 -0
- nat/middleware/dynamic/register.py +34 -0
- nat/middleware/function_middleware.py +236 -52
- nat/middleware/logging/__init__.py +14 -0
- nat/middleware/logging/logging_middleware.py +67 -0
- nat/middleware/logging/logging_middleware_config.py +28 -0
- nat/middleware/logging/register.py +33 -0
- nat/middleware/middleware.py +142 -28
- nat/middleware/red_teaming/__init__.py +14 -0
- nat/middleware/red_teaming/red_teaming_middleware.py +344 -0
- nat/middleware/red_teaming/red_teaming_middleware_config.py +112 -0
- nat/middleware/red_teaming/register.py +47 -0
- nat/middleware/register.py +7 -20
- nat/middleware/utils/__init__.py +14 -0
- nat/middleware/utils/workflow_inventory.py +155 -0
- nat/object_store/__init__.py +1 -1
- nat/object_store/in_memory_object_store.py +1 -1
- nat/object_store/interfaces.py +1 -1
- nat/object_store/models.py +1 -1
- nat/object_store/register.py +1 -1
- nat/observability/__init__.py +1 -1
- nat/observability/exporter/__init__.py +1 -1
- nat/observability/exporter/base_exporter.py +1 -1
- nat/observability/exporter/exporter.py +1 -1
- nat/observability/exporter/file_exporter.py +1 -1
- nat/observability/exporter/processing_exporter.py +1 -1
- nat/observability/exporter/raw_exporter.py +1 -1
- nat/observability/exporter/span_exporter.py +7 -1
- nat/observability/exporter_manager.py +1 -1
- nat/observability/mixin/__init__.py +1 -1
- nat/observability/mixin/batch_config_mixin.py +1 -1
- nat/observability/mixin/collector_config_mixin.py +1 -1
- nat/observability/mixin/file_mixin.py +1 -1
- nat/observability/mixin/file_mode.py +1 -1
- nat/observability/mixin/redaction_config_mixin.py +1 -1
- nat/observability/mixin/resource_conflict_mixin.py +1 -1
- nat/observability/mixin/serialize_mixin.py +1 -1
- nat/observability/mixin/tagging_config_mixin.py +1 -1
- nat/observability/mixin/type_introspection_mixin.py +1 -1
- nat/observability/processor/__init__.py +1 -1
- nat/observability/processor/batching_processor.py +1 -1
- nat/observability/processor/callback_processor.py +1 -1
- nat/observability/processor/falsy_batch_filter_processor.py +1 -1
- nat/observability/processor/intermediate_step_serializer.py +1 -1
- nat/observability/processor/processor.py +1 -1
- nat/observability/processor/processor_factory.py +1 -1
- nat/observability/processor/redaction/__init__.py +1 -1
- nat/observability/processor/redaction/contextual_redaction_processor.py +1 -1
- nat/observability/processor/redaction/contextual_span_redaction_processor.py +1 -1
- nat/observability/processor/redaction/redaction_processor.py +1 -1
- nat/observability/processor/redaction/span_header_redaction_processor.py +1 -1
- nat/observability/processor/span_tagging_processor.py +1 -1
- nat/observability/register.py +1 -1
- nat/observability/utils/__init__.py +1 -1
- nat/observability/utils/dict_utils.py +1 -1
- nat/observability/utils/time_utils.py +1 -1
- nat/profiler/calc/__init__.py +1 -1
- nat/profiler/calc/calc_runner.py +3 -3
- nat/profiler/calc/calculations.py +1 -1
- nat/profiler/calc/data_models.py +1 -1
- nat/profiler/calc/plot.py +30 -3
- nat/profiler/callbacks/agno_callback_handler.py +1 -1
- nat/profiler/callbacks/base_callback_class.py +1 -1
- nat/profiler/callbacks/langchain_callback_handler.py +33 -3
- nat/profiler/callbacks/llama_index_callback_handler.py +13 -10
- nat/profiler/callbacks/semantic_kernel_callback_handler.py +1 -1
- nat/profiler/callbacks/token_usage_base_model.py +1 -1
- nat/profiler/data_frame_row.py +1 -1
- nat/profiler/data_models.py +1 -1
- nat/profiler/decorators/framework_wrapper.py +16 -1
- nat/profiler/decorators/function_tracking.py +1 -1
- nat/profiler/forecasting/config.py +1 -1
- nat/profiler/forecasting/model_trainer.py +1 -1
- nat/profiler/forecasting/models/__init__.py +1 -1
- nat/profiler/forecasting/models/forecasting_base_model.py +1 -1
- nat/profiler/forecasting/models/linear_model.py +1 -1
- nat/profiler/forecasting/models/random_forest_regressor.py +1 -1
- nat/profiler/inference_metrics_model.py +1 -1
- nat/profiler/inference_optimization/bottleneck_analysis/nested_stack_analysis.py +1 -1
- nat/profiler/inference_optimization/bottleneck_analysis/simple_stack_analysis.py +1 -1
- nat/profiler/inference_optimization/data_models.py +1 -1
- nat/profiler/inference_optimization/experimental/concurrency_spike_analysis.py +1 -1
- nat/profiler/inference_optimization/experimental/prefix_span_analysis.py +1 -1
- nat/profiler/inference_optimization/llm_metrics.py +1 -1
- nat/profiler/inference_optimization/prompt_caching.py +1 -1
- nat/profiler/inference_optimization/token_uniqueness.py +1 -1
- nat/profiler/inference_optimization/workflow_runtimes.py +1 -1
- nat/profiler/intermediate_property_adapter.py +1 -1
- nat/profiler/parameter_optimization/optimizable_utils.py +1 -1
- nat/profiler/parameter_optimization/optimizer_runtime.py +1 -1
- nat/profiler/parameter_optimization/parameter_optimizer.py +1 -1
- nat/profiler/parameter_optimization/parameter_selection.py +1 -1
- nat/profiler/parameter_optimization/pareto_visualizer.py +1 -1
- nat/profiler/parameter_optimization/prompt_optimizer.py +1 -1
- nat/profiler/parameter_optimization/update_helpers.py +1 -1
- nat/profiler/profile_runner.py +1 -1
- nat/profiler/utils.py +1 -1
- nat/registry_handlers/local/local_handler.py +1 -1
- nat/registry_handlers/local/register_local.py +1 -1
- nat/registry_handlers/metadata_factory.py +1 -1
- nat/registry_handlers/package_utils.py +1 -1
- nat/registry_handlers/pypi/pypi_handler.py +1 -1
- nat/registry_handlers/pypi/register_pypi.py +1 -1
- nat/registry_handlers/register.py +1 -1
- nat/registry_handlers/registry_handler_base.py +1 -1
- nat/registry_handlers/rest/register_rest.py +1 -1
- nat/registry_handlers/rest/rest_handler.py +1 -1
- nat/registry_handlers/schemas/headers.py +1 -1
- nat/registry_handlers/schemas/package.py +1 -1
- nat/registry_handlers/schemas/publish.py +1 -1
- nat/registry_handlers/schemas/pull.py +1 -1
- nat/registry_handlers/schemas/remove.py +1 -1
- nat/registry_handlers/schemas/search.py +1 -1
- nat/registry_handlers/schemas/status.py +1 -1
- nat/retriever/interface.py +1 -1
- nat/retriever/milvus/__init__.py +1 -1
- nat/retriever/milvus/register.py +1 -1
- nat/retriever/milvus/retriever.py +1 -1
- nat/retriever/models.py +1 -1
- nat/retriever/nemo_retriever/__init__.py +1 -1
- nat/retriever/nemo_retriever/register.py +1 -1
- nat/retriever/nemo_retriever/retriever.py +5 -5
- nat/retriever/register.py +1 -1
- nat/runtime/__init__.py +1 -1
- nat/runtime/loader.py +10 -3
- nat/runtime/metrics.py +180 -0
- nat/runtime/runner.py +1 -5
- nat/runtime/session.py +451 -32
- nat/runtime/user_metadata.py +1 -1
- nat/settings/global_settings.py +1 -1
- nat/tool/chat_completion.py +1 -1
- nat/tool/code_execution/README.md +1 -1
- nat/tool/code_execution/code_sandbox.py +1 -1
- nat/tool/code_execution/local_sandbox/Dockerfile.sandbox +1 -1
- nat/tool/code_execution/local_sandbox/__init__.py +1 -1
- nat/tool/code_execution/local_sandbox/local_sandbox_server.py +1 -1
- nat/tool/code_execution/local_sandbox/start_local_sandbox.sh +1 -1
- nat/tool/code_execution/register.py +1 -1
- nat/tool/code_execution/utils.py +1 -1
- nat/tool/datetime_tools.py +1 -1
- nat/tool/document_search.py +1 -1
- nat/tool/github_tools.py +1 -1
- nat/tool/memory_tools/add_memory_tool.py +1 -1
- nat/tool/memory_tools/delete_memory_tool.py +1 -1
- nat/tool/memory_tools/get_memory_tool.py +1 -1
- nat/tool/nvidia_rag.py +2 -2
- nat/tool/register.py +1 -1
- nat/tool/retriever.py +1 -1
- nat/tool/server_tools.py +1 -1
- nat/utils/__init__.py +8 -5
- nat/utils/callable_utils.py +1 -1
- nat/utils/data_models/schema_validator.py +1 -1
- nat/utils/debugging_utils.py +1 -1
- nat/utils/decorators.py +1 -1
- nat/utils/dump_distro_mapping.py +1 -1
- nat/utils/exception_handlers/automatic_retries.py +3 -3
- nat/utils/exception_handlers/schemas.py +1 -1
- nat/utils/io/model_processing.py +1 -1
- nat/utils/io/supress_logs.py +33 -0
- nat/utils/io/yaml_tools.py +1 -1
- nat/utils/log_levels.py +1 -1
- nat/utils/log_utils.py +13 -1
- nat/utils/metadata_utils.py +1 -1
- nat/utils/optional_imports.py +1 -1
- nat/utils/producer_consumer_queue.py +1 -1
- nat/utils/reactive/base/observable_base.py +1 -1
- nat/utils/reactive/base/observer_base.py +1 -1
- nat/utils/reactive/base/subject_base.py +1 -1
- nat/utils/reactive/observable.py +1 -1
- nat/utils/reactive/observer.py +1 -1
- nat/utils/reactive/subject.py +1 -1
- nat/utils/reactive/subscription.py +1 -1
- nat/utils/responses_api.py +1 -1
- nat/utils/settings/global_settings.py +1 -1
- nat/utils/string_utils.py +1 -1
- nat/utils/type_converter.py +18 -5
- nat/utils/type_utils.py +1 -1
- nat/utils/url_utils.py +1 -1
- {nvidia_nat-1.4.0a20251120.dist-info → nvidia_nat-1.4.0a20260113.dist-info}/METADATA +39 -14
- nvidia_nat-1.4.0a20260113.dist-info/RECORD +547 -0
- nvidia_nat-1.4.0a20260113.dist-info/entry_points.txt +38 -0
- nat/cli/commands/mcp/mcp.py +0 -986
- nat/front_ends/mcp/introspection_token_verifier.py +0 -73
- nat/front_ends/mcp/mcp_front_end_config.py +0 -109
- nat/front_ends/mcp/mcp_front_end_plugin.py +0 -155
- nat/front_ends/mcp/mcp_front_end_plugin_worker.py +0 -388
- nat/front_ends/mcp/memory_profiler.py +0 -320
- nat/front_ends/mcp/register.py +0 -27
- nat/front_ends/mcp/tool_converter.py +0 -321
- nvidia_nat-1.4.0a20251120.dist-info/RECORD +0 -488
- nvidia_nat-1.4.0a20251120.dist-info/entry_points.txt +0 -23
- {nvidia_nat-1.4.0a20251120.dist-info → nvidia_nat-1.4.0a20260113.dist-info}/WHEEL +0 -0
- {nvidia_nat-1.4.0a20251120.dist-info → nvidia_nat-1.4.0a20260113.dist-info}/licenses/LICENSE-3rd-party.txt +0 -0
- {nvidia_nat-1.4.0a20251120.dist-info → nvidia_nat-1.4.0a20260113.dist-info}/licenses/LICENSE.md +0 -0
- {nvidia_nat-1.4.0a20251120.dist-info → nvidia_nat-1.4.0a20260113.dist-info}/top_level.txt +0 -0
|
@@ -1,320 +0,0 @@
|
|
|
1
|
-
# SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
2
|
-
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
#
|
|
4
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
# you may not use this file except in compliance with the License.
|
|
6
|
-
# You may obtain a copy of the License at
|
|
7
|
-
#
|
|
8
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
#
|
|
10
|
-
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
# See the License for the specific language governing permissions and
|
|
14
|
-
# limitations under the License.
|
|
15
|
-
"""Memory profiling utilities for MCP frontend."""
|
|
16
|
-
|
|
17
|
-
import gc
|
|
18
|
-
import logging
|
|
19
|
-
import tracemalloc
|
|
20
|
-
from typing import Any
|
|
21
|
-
|
|
22
|
-
logger = logging.getLogger(__name__)
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
class MemoryProfiler:
|
|
26
|
-
"""Memory profiler for tracking memory usage and potential leaks."""
|
|
27
|
-
|
|
28
|
-
def __init__(self, enabled: bool = False, log_interval: int = 50, top_n: int = 10, log_level: str = "DEBUG"):
|
|
29
|
-
"""Initialize the memory profiler.
|
|
30
|
-
|
|
31
|
-
Args:
|
|
32
|
-
enabled: Whether memory profiling is enabled
|
|
33
|
-
log_interval: Log stats every N requests
|
|
34
|
-
top_n: Number of top allocations to log
|
|
35
|
-
log_level: Log level for memory profiling output (e.g., "DEBUG", "INFO")
|
|
36
|
-
"""
|
|
37
|
-
self.enabled = enabled
|
|
38
|
-
# normalize interval to avoid modulo-by-zero
|
|
39
|
-
self.log_interval = max(1, int(log_interval))
|
|
40
|
-
self.top_n = top_n
|
|
41
|
-
self.log_level = getattr(logging, log_level.upper(), logging.DEBUG)
|
|
42
|
-
self.request_count = 0
|
|
43
|
-
self.baseline_snapshot = None
|
|
44
|
-
|
|
45
|
-
# Track whether this instance started tracemalloc (to avoid resetting external tracing)
|
|
46
|
-
self._we_started_tracemalloc = False
|
|
47
|
-
|
|
48
|
-
if self.enabled:
|
|
49
|
-
logger.info("Memory profiling ENABLED (interval=%d, top_n=%d, log_level=%s)",
|
|
50
|
-
self.log_interval,
|
|
51
|
-
top_n,
|
|
52
|
-
log_level)
|
|
53
|
-
try:
|
|
54
|
-
if not tracemalloc.is_tracing():
|
|
55
|
-
tracemalloc.start()
|
|
56
|
-
self._we_started_tracemalloc = True
|
|
57
|
-
# Take baseline snapshot
|
|
58
|
-
gc.collect()
|
|
59
|
-
self.baseline_snapshot = tracemalloc.take_snapshot()
|
|
60
|
-
except RuntimeError as e:
|
|
61
|
-
logger.warning("tracemalloc unavailable or not tracing: %s", e)
|
|
62
|
-
else:
|
|
63
|
-
logger.info("Memory profiling DISABLED")
|
|
64
|
-
|
|
65
|
-
def _log(self, message: str, *args: Any) -> None:
|
|
66
|
-
"""Log a message at the configured log level.
|
|
67
|
-
|
|
68
|
-
Args:
|
|
69
|
-
message: Log message format string
|
|
70
|
-
args: Arguments for the format string
|
|
71
|
-
"""
|
|
72
|
-
logger.log(self.log_level, message, *args)
|
|
73
|
-
|
|
74
|
-
def on_request_complete(self) -> None:
|
|
75
|
-
"""Called after each request completes."""
|
|
76
|
-
if not self.enabled:
|
|
77
|
-
return
|
|
78
|
-
self.request_count += 1
|
|
79
|
-
if self.request_count % self.log_interval == 0:
|
|
80
|
-
self.log_memory_stats()
|
|
81
|
-
|
|
82
|
-
def _ensure_tracing(self) -> bool:
|
|
83
|
-
"""Ensure tracemalloc is running if we started it originally.
|
|
84
|
-
|
|
85
|
-
Returns:
|
|
86
|
-
True if tracemalloc is active, False otherwise
|
|
87
|
-
"""
|
|
88
|
-
if tracemalloc.is_tracing():
|
|
89
|
-
return True
|
|
90
|
-
|
|
91
|
-
# Only restart if we started it originally (respect external control)
|
|
92
|
-
if not self._we_started_tracemalloc:
|
|
93
|
-
return False
|
|
94
|
-
|
|
95
|
-
# Attempt to restart
|
|
96
|
-
try:
|
|
97
|
-
logger.warning("tracemalloc was stopped externally; restarting (we started it originally)")
|
|
98
|
-
tracemalloc.start()
|
|
99
|
-
|
|
100
|
-
# Reset baseline since old tracking data is lost
|
|
101
|
-
gc.collect()
|
|
102
|
-
self.baseline_snapshot = tracemalloc.take_snapshot()
|
|
103
|
-
logger.info("Baseline snapshot reset after tracemalloc restart")
|
|
104
|
-
|
|
105
|
-
return True
|
|
106
|
-
except RuntimeError as e:
|
|
107
|
-
logger.error("Failed to restart tracemalloc: %s", e)
|
|
108
|
-
return False
|
|
109
|
-
|
|
110
|
-
def _safe_traced_memory(self) -> tuple[float, float] | None:
|
|
111
|
-
"""Return (current, peak usage in MB) if tracemalloc is active, else None."""
|
|
112
|
-
if not self._ensure_tracing():
|
|
113
|
-
return None
|
|
114
|
-
|
|
115
|
-
try:
|
|
116
|
-
current, peak = tracemalloc.get_traced_memory()
|
|
117
|
-
megabyte = (1 << 20)
|
|
118
|
-
return (current / megabyte, peak / megabyte)
|
|
119
|
-
except RuntimeError:
|
|
120
|
-
return None
|
|
121
|
-
|
|
122
|
-
def _safe_snapshot(self) -> tracemalloc.Snapshot | None:
|
|
123
|
-
"""Return a tracemalloc Snapshot if available, else None."""
|
|
124
|
-
if not self._ensure_tracing():
|
|
125
|
-
return None
|
|
126
|
-
|
|
127
|
-
try:
|
|
128
|
-
return tracemalloc.take_snapshot()
|
|
129
|
-
except RuntimeError:
|
|
130
|
-
return None
|
|
131
|
-
|
|
132
|
-
def log_memory_stats(self) -> dict[str, Any]:
|
|
133
|
-
"""Log current memory statistics and return them."""
|
|
134
|
-
if not self.enabled:
|
|
135
|
-
return {}
|
|
136
|
-
|
|
137
|
-
# Force garbage collection first
|
|
138
|
-
gc.collect()
|
|
139
|
-
|
|
140
|
-
# Get current memory usage
|
|
141
|
-
mem = self._safe_traced_memory()
|
|
142
|
-
if mem is None:
|
|
143
|
-
logger.info("tracemalloc is not active; cannot collect memory stats.")
|
|
144
|
-
# still return structural fields
|
|
145
|
-
stats = {
|
|
146
|
-
"request_count": self.request_count,
|
|
147
|
-
"current_memory_mb": None,
|
|
148
|
-
"peak_memory_mb": None,
|
|
149
|
-
"active_intermediate_managers": self._safe_intermediate_step_manager_count(),
|
|
150
|
-
"outstanding_steps": self._safe_outstanding_step_count(),
|
|
151
|
-
"active_exporters": self._safe_exporter_count(),
|
|
152
|
-
"isolated_exporters": self._safe_isolated_exporter_count(),
|
|
153
|
-
"subject_instances": self._count_instances_of_type("Subject"),
|
|
154
|
-
}
|
|
155
|
-
return stats
|
|
156
|
-
|
|
157
|
-
current_mb, peak_mb = mem
|
|
158
|
-
|
|
159
|
-
# Take snapshot and compare to baseline
|
|
160
|
-
snapshot = self._safe_snapshot()
|
|
161
|
-
|
|
162
|
-
# Track BaseExporter instances (observability layer)
|
|
163
|
-
exporter_count = self._safe_exporter_count()
|
|
164
|
-
isolated_exporter_count = self._safe_isolated_exporter_count()
|
|
165
|
-
|
|
166
|
-
# Track Subject instances (event streams)
|
|
167
|
-
subject_count = self._count_instances_of_type("Subject")
|
|
168
|
-
|
|
169
|
-
stats = {
|
|
170
|
-
"request_count": self.request_count,
|
|
171
|
-
"current_memory_mb": round(current_mb, 2),
|
|
172
|
-
"peak_memory_mb": round(peak_mb, 2),
|
|
173
|
-
"active_intermediate_managers": self._safe_intermediate_step_manager_count(),
|
|
174
|
-
"outstanding_steps": self._safe_outstanding_step_count(),
|
|
175
|
-
"active_exporters": exporter_count,
|
|
176
|
-
"isolated_exporters": isolated_exporter_count,
|
|
177
|
-
"subject_instances": subject_count,
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
self._log("=" * 80)
|
|
181
|
-
self._log("MEMORY PROFILE AFTER %d REQUESTS:", self.request_count)
|
|
182
|
-
self._log(" Current Memory: %.2f MB", current_mb)
|
|
183
|
-
self._log(" Peak Memory: %.2f MB", peak_mb)
|
|
184
|
-
self._log("")
|
|
185
|
-
self._log("NAT COMPONENT INSTANCES:")
|
|
186
|
-
self._log(" IntermediateStepManagers: %d active (%d outstanding steps)",
|
|
187
|
-
stats["active_intermediate_managers"],
|
|
188
|
-
stats["outstanding_steps"])
|
|
189
|
-
self._log(" BaseExporters: %d active (%d isolated)", stats["active_exporters"], stats["isolated_exporters"])
|
|
190
|
-
self._log(" Subject (event streams): %d instances", stats["subject_instances"])
|
|
191
|
-
|
|
192
|
-
# Show top allocations
|
|
193
|
-
if snapshot is None:
|
|
194
|
-
self._log("tracemalloc snapshot unavailable.")
|
|
195
|
-
else:
|
|
196
|
-
if self.baseline_snapshot:
|
|
197
|
-
self._log("TOP %d MEMORY GROWTH SINCE BASELINE:", self.top_n)
|
|
198
|
-
top_stats = snapshot.compare_to(self.baseline_snapshot, 'lineno')
|
|
199
|
-
else:
|
|
200
|
-
self._log("TOP %d MEMORY ALLOCATIONS:", self.top_n)
|
|
201
|
-
top_stats = snapshot.statistics('lineno')
|
|
202
|
-
|
|
203
|
-
for i, stat in enumerate(top_stats[:self.top_n], 1):
|
|
204
|
-
self._log(" #%d: %s", i, stat)
|
|
205
|
-
|
|
206
|
-
self._log("=" * 80)
|
|
207
|
-
|
|
208
|
-
return stats
|
|
209
|
-
|
|
210
|
-
def _count_instances_of_type(self, type_name: str) -> int:
|
|
211
|
-
"""Count instances of a specific type in memory."""
|
|
212
|
-
count = 0
|
|
213
|
-
for obj in gc.get_objects():
|
|
214
|
-
try:
|
|
215
|
-
if type(obj).__name__ == type_name:
|
|
216
|
-
count += 1
|
|
217
|
-
except Exception:
|
|
218
|
-
pass
|
|
219
|
-
return count
|
|
220
|
-
|
|
221
|
-
def _safe_exporter_count(self) -> int:
|
|
222
|
-
try:
|
|
223
|
-
from nat.observability.exporter.base_exporter import BaseExporter
|
|
224
|
-
return BaseExporter.get_active_instance_count()
|
|
225
|
-
except Exception as e:
|
|
226
|
-
logger.debug("Could not get BaseExporter stats: %s", e)
|
|
227
|
-
return 0
|
|
228
|
-
|
|
229
|
-
def _safe_isolated_exporter_count(self) -> int:
|
|
230
|
-
try:
|
|
231
|
-
from nat.observability.exporter.base_exporter import BaseExporter
|
|
232
|
-
return BaseExporter.get_isolated_instance_count()
|
|
233
|
-
except Exception:
|
|
234
|
-
return 0
|
|
235
|
-
|
|
236
|
-
def _safe_intermediate_step_manager_count(self) -> int:
|
|
237
|
-
try:
|
|
238
|
-
from nat.builder.intermediate_step_manager import IntermediateStepManager
|
|
239
|
-
# len() is atomic in CPython, but catch RuntimeError just in case
|
|
240
|
-
try:
|
|
241
|
-
return IntermediateStepManager.get_active_instance_count()
|
|
242
|
-
except RuntimeError:
|
|
243
|
-
# Set was modified during len() - very rare
|
|
244
|
-
logger.debug("Set changed during count, returning 0")
|
|
245
|
-
return 0
|
|
246
|
-
except Exception as e:
|
|
247
|
-
logger.debug("Could not get IntermediateStepManager stats: %s", e)
|
|
248
|
-
return 0
|
|
249
|
-
|
|
250
|
-
def _safe_outstanding_step_count(self) -> int:
|
|
251
|
-
"""Get total outstanding steps across all active IntermediateStepManager instances."""
|
|
252
|
-
try:
|
|
253
|
-
from nat.builder.intermediate_step_manager import IntermediateStepManager
|
|
254
|
-
|
|
255
|
-
# Make a snapshot to avoid "Set changed size during iteration" if GC runs
|
|
256
|
-
try:
|
|
257
|
-
instances_snapshot = list(IntermediateStepManager._active_instances)
|
|
258
|
-
except RuntimeError:
|
|
259
|
-
# Set changed during list() call - rare but possible
|
|
260
|
-
logger.debug("Set changed during snapshot, returning 0 for outstanding steps")
|
|
261
|
-
return 0
|
|
262
|
-
|
|
263
|
-
total_outstanding = 0
|
|
264
|
-
# Iterate through snapshot safely
|
|
265
|
-
for ref in instances_snapshot:
|
|
266
|
-
try:
|
|
267
|
-
manager = ref()
|
|
268
|
-
if manager is not None:
|
|
269
|
-
total_outstanding += manager.get_outstanding_step_count()
|
|
270
|
-
except (ReferenceError, AttributeError):
|
|
271
|
-
# Manager was GC'd or in invalid state - skip it
|
|
272
|
-
continue
|
|
273
|
-
return total_outstanding
|
|
274
|
-
except Exception as e:
|
|
275
|
-
logger.debug("Could not get outstanding step count: %s", e)
|
|
276
|
-
return 0
|
|
277
|
-
|
|
278
|
-
def get_stats(self) -> dict[str, Any]:
|
|
279
|
-
"""Get current memory statistics without logging."""
|
|
280
|
-
if not self.enabled:
|
|
281
|
-
return {"enabled": False}
|
|
282
|
-
|
|
283
|
-
mem = self._safe_traced_memory()
|
|
284
|
-
if mem is None:
|
|
285
|
-
return {
|
|
286
|
-
"enabled": True,
|
|
287
|
-
"request_count": self.request_count,
|
|
288
|
-
"current_memory_mb": None,
|
|
289
|
-
"peak_memory_mb": None,
|
|
290
|
-
"active_intermediate_managers": self._safe_intermediate_step_manager_count(),
|
|
291
|
-
"outstanding_steps": self._safe_outstanding_step_count(),
|
|
292
|
-
"active_exporters": self._safe_exporter_count(),
|
|
293
|
-
"isolated_exporters": self._safe_isolated_exporter_count(),
|
|
294
|
-
"subject_instances": self._count_instances_of_type("Subject"),
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
current_mb, peak_mb = mem
|
|
298
|
-
return {
|
|
299
|
-
"enabled": True,
|
|
300
|
-
"request_count": self.request_count,
|
|
301
|
-
"current_memory_mb": round(current_mb, 2),
|
|
302
|
-
"peak_memory_mb": round(peak_mb, 2),
|
|
303
|
-
"active_intermediate_managers": self._safe_intermediate_step_manager_count(),
|
|
304
|
-
"outstanding_steps": self._safe_outstanding_step_count(),
|
|
305
|
-
"active_exporters": self._safe_exporter_count(),
|
|
306
|
-
"isolated_exporters": self._safe_isolated_exporter_count(),
|
|
307
|
-
"subject_instances": self._count_instances_of_type("Subject"),
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
def reset_baseline(self) -> None:
|
|
311
|
-
"""Reset the baseline snapshot to current state."""
|
|
312
|
-
if not self.enabled:
|
|
313
|
-
return
|
|
314
|
-
gc.collect()
|
|
315
|
-
snap = self._safe_snapshot()
|
|
316
|
-
if snap is None:
|
|
317
|
-
logger.info("Cannot reset baseline: tracemalloc is not active.")
|
|
318
|
-
return
|
|
319
|
-
self.baseline_snapshot = snap
|
|
320
|
-
logger.info("Memory profiling baseline reset at request %d", self.request_count)
|
nat/front_ends/mcp/register.py
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
# SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
2
|
-
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
#
|
|
4
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
# you may not use this file except in compliance with the License.
|
|
6
|
-
# You may obtain a copy of the License at
|
|
7
|
-
#
|
|
8
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
#
|
|
10
|
-
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
# See the License for the specific language governing permissions and
|
|
14
|
-
# limitations under the License.
|
|
15
|
-
|
|
16
|
-
from collections.abc import AsyncIterator
|
|
17
|
-
|
|
18
|
-
from nat.cli.register_workflow import register_front_end
|
|
19
|
-
from nat.data_models.config import Config
|
|
20
|
-
from nat.front_ends.mcp.mcp_front_end_config import MCPFrontEndConfig
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
@register_front_end(config_type=MCPFrontEndConfig)
|
|
24
|
-
async def register_mcp_front_end(config: MCPFrontEndConfig, full_config: Config) -> AsyncIterator:
|
|
25
|
-
from nat.front_ends.mcp.mcp_front_end_plugin import MCPFrontEndPlugin
|
|
26
|
-
|
|
27
|
-
yield MCPFrontEndPlugin(full_config=full_config)
|
|
@@ -1,321 +0,0 @@
|
|
|
1
|
-
# SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
2
|
-
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
#
|
|
4
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
# you may not use this file except in compliance with the License.
|
|
6
|
-
# You may obtain a copy of the License at
|
|
7
|
-
#
|
|
8
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
#
|
|
10
|
-
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
# See the License for the specific language governing permissions and
|
|
14
|
-
# limitations under the License.
|
|
15
|
-
|
|
16
|
-
import json
|
|
17
|
-
import logging
|
|
18
|
-
from inspect import Parameter
|
|
19
|
-
from inspect import Signature
|
|
20
|
-
from typing import TYPE_CHECKING
|
|
21
|
-
from typing import Any
|
|
22
|
-
|
|
23
|
-
from mcp.server.fastmcp import FastMCP
|
|
24
|
-
from pydantic import BaseModel
|
|
25
|
-
from pydantic.fields import FieldInfo
|
|
26
|
-
from pydantic_core import PydanticUndefined
|
|
27
|
-
|
|
28
|
-
from nat.builder.context import ContextState
|
|
29
|
-
from nat.builder.function import Function
|
|
30
|
-
from nat.builder.function_base import FunctionBase
|
|
31
|
-
|
|
32
|
-
if TYPE_CHECKING:
|
|
33
|
-
from nat.builder.workflow import Workflow
|
|
34
|
-
from nat.front_ends.mcp.memory_profiler import MemoryProfiler
|
|
35
|
-
|
|
36
|
-
logger = logging.getLogger(__name__)
|
|
37
|
-
|
|
38
|
-
# Sentinel: marks "optional; let Pydantic supply default/factory"
|
|
39
|
-
_USE_PYDANTIC_DEFAULT = object()
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
def is_field_optional(field: FieldInfo) -> tuple[bool, Any]:
|
|
43
|
-
"""Determine if a Pydantic field is optional and extract its default value for MCP signatures.
|
|
44
|
-
|
|
45
|
-
For MCP tool signatures, we need to distinguish:
|
|
46
|
-
- Required fields: marked with Parameter.empty
|
|
47
|
-
- Optional with concrete default: use that default
|
|
48
|
-
- Optional with factory: use sentinel so Pydantic can apply the factory later
|
|
49
|
-
|
|
50
|
-
Args:
|
|
51
|
-
field: The Pydantic FieldInfo to check
|
|
52
|
-
|
|
53
|
-
Returns:
|
|
54
|
-
A tuple of (is_optional, default_value):
|
|
55
|
-
- (False, Parameter.empty) for required fields
|
|
56
|
-
- (True, actual_default) for optional fields with explicit defaults
|
|
57
|
-
- (True, _USE_PYDANTIC_DEFAULT) for optional fields with default_factory
|
|
58
|
-
"""
|
|
59
|
-
if field.is_required():
|
|
60
|
-
return False, Parameter.empty
|
|
61
|
-
|
|
62
|
-
# Field is optional - has either default or factory
|
|
63
|
-
if field.default is not PydanticUndefined:
|
|
64
|
-
return True, field.default
|
|
65
|
-
|
|
66
|
-
# Factory case: mark optional in signature but don't fabricate a value
|
|
67
|
-
if field.default_factory is not None:
|
|
68
|
-
return True, _USE_PYDANTIC_DEFAULT
|
|
69
|
-
|
|
70
|
-
# Rare corner case: non-required yet no default surfaced
|
|
71
|
-
return True, _USE_PYDANTIC_DEFAULT
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
def create_function_wrapper(
|
|
75
|
-
function_name: str,
|
|
76
|
-
function: FunctionBase,
|
|
77
|
-
schema: type[BaseModel],
|
|
78
|
-
is_workflow: bool = False,
|
|
79
|
-
workflow: 'Workflow | None' = None,
|
|
80
|
-
memory_profiler: 'MemoryProfiler | None' = None,
|
|
81
|
-
):
|
|
82
|
-
"""Create a wrapper function that exposes the actual parameters of a NAT Function as an MCP tool.
|
|
83
|
-
|
|
84
|
-
Args:
|
|
85
|
-
function_name (str): The name of the function/tool
|
|
86
|
-
function (FunctionBase): The NAT Function object
|
|
87
|
-
schema (type[BaseModel]): The input schema of the function
|
|
88
|
-
is_workflow (bool): Whether the function is a Workflow
|
|
89
|
-
workflow (Workflow | None): The parent workflow for observability context
|
|
90
|
-
memory_profiler: Optional memory profiler to track requests
|
|
91
|
-
|
|
92
|
-
Returns:
|
|
93
|
-
A wrapper function suitable for registration with MCP
|
|
94
|
-
"""
|
|
95
|
-
# Check if we're dealing with ChatRequest - special case
|
|
96
|
-
is_chat_request = False
|
|
97
|
-
|
|
98
|
-
# Check if the schema name is ChatRequest
|
|
99
|
-
if schema.__name__ == "ChatRequest" or (hasattr(schema, "__qualname__") and "ChatRequest" in schema.__qualname__):
|
|
100
|
-
is_chat_request = True
|
|
101
|
-
logger.info("Function %s uses ChatRequest - creating simplified interface", function_name)
|
|
102
|
-
|
|
103
|
-
# For ChatRequest, we'll create a simple wrapper with just a query parameter
|
|
104
|
-
parameters = [Parameter(
|
|
105
|
-
name="query",
|
|
106
|
-
kind=Parameter.KEYWORD_ONLY,
|
|
107
|
-
default=Parameter.empty,
|
|
108
|
-
annotation=str,
|
|
109
|
-
)]
|
|
110
|
-
else:
|
|
111
|
-
# Regular case - extract parameter information from the input schema
|
|
112
|
-
# Extract parameter information from the input schema
|
|
113
|
-
param_fields = schema.model_fields
|
|
114
|
-
|
|
115
|
-
parameters = []
|
|
116
|
-
for name, field in param_fields.items():
|
|
117
|
-
# Get the field type and convert to appropriate Python type
|
|
118
|
-
field_type = field.annotation
|
|
119
|
-
|
|
120
|
-
# Check if field is optional and get its default value
|
|
121
|
-
_is_optional, param_default = is_field_optional(field)
|
|
122
|
-
|
|
123
|
-
# Add the parameter to our list
|
|
124
|
-
parameters.append(
|
|
125
|
-
Parameter(
|
|
126
|
-
name=name,
|
|
127
|
-
kind=Parameter.KEYWORD_ONLY,
|
|
128
|
-
default=param_default,
|
|
129
|
-
annotation=field_type,
|
|
130
|
-
))
|
|
131
|
-
|
|
132
|
-
# Create the function signature WITHOUT the ctx parameter
|
|
133
|
-
# We'll handle this in the wrapper function internally
|
|
134
|
-
sig = Signature(parameters=parameters, return_annotation=str)
|
|
135
|
-
|
|
136
|
-
# Define the actual wrapper function that accepts ctx but doesn't expose it
|
|
137
|
-
def create_wrapper():
|
|
138
|
-
|
|
139
|
-
async def wrapper_with_ctx(**kwargs):
|
|
140
|
-
"""Internal wrapper that will be called by MCP."""
|
|
141
|
-
# MCP will add a ctx parameter, extract it
|
|
142
|
-
ctx = kwargs.get("ctx")
|
|
143
|
-
|
|
144
|
-
# Remove ctx if present
|
|
145
|
-
if "ctx" in kwargs:
|
|
146
|
-
del kwargs["ctx"]
|
|
147
|
-
|
|
148
|
-
# Process the function call
|
|
149
|
-
if ctx:
|
|
150
|
-
ctx.info("Calling function %s with args: %s", function_name, json.dumps(kwargs, default=str))
|
|
151
|
-
await ctx.report_progress(0, 100)
|
|
152
|
-
|
|
153
|
-
try:
|
|
154
|
-
# Helper function to wrap function calls with observability
|
|
155
|
-
async def call_with_observability(func_call):
|
|
156
|
-
# Use workflow's observability context (workflow should always be available)
|
|
157
|
-
if not workflow:
|
|
158
|
-
logger.error("Missing workflow context for function %s - observability will not be available",
|
|
159
|
-
function_name)
|
|
160
|
-
raise RuntimeError("Workflow context is required for observability")
|
|
161
|
-
|
|
162
|
-
logger.debug("Starting observability context for function %s", function_name)
|
|
163
|
-
context_state = ContextState.get()
|
|
164
|
-
async with workflow.exporter_manager.start(context_state=context_state):
|
|
165
|
-
return await func_call()
|
|
166
|
-
|
|
167
|
-
# Special handling for ChatRequest
|
|
168
|
-
if is_chat_request:
|
|
169
|
-
from nat.data_models.api_server import ChatRequest
|
|
170
|
-
|
|
171
|
-
# Create a chat request from the query string
|
|
172
|
-
query = kwargs.get("query", "")
|
|
173
|
-
chat_request = ChatRequest.from_string(query)
|
|
174
|
-
|
|
175
|
-
# Special handling for Workflow objects
|
|
176
|
-
if is_workflow:
|
|
177
|
-
# Workflows have a run method that is an async context manager
|
|
178
|
-
# that returns a Runner
|
|
179
|
-
async with function.run(chat_request) as runner:
|
|
180
|
-
# Get the result from the runner
|
|
181
|
-
result = await runner.result(to_type=str)
|
|
182
|
-
else:
|
|
183
|
-
# Regular functions use ainvoke
|
|
184
|
-
result = await call_with_observability(lambda: function.ainvoke(chat_request, to_type=str))
|
|
185
|
-
else:
|
|
186
|
-
# Regular handling
|
|
187
|
-
# Strip sentinel values so Pydantic can apply defaults/factories
|
|
188
|
-
cleaned_kwargs = {k: v for k, v in kwargs.items() if v is not _USE_PYDANTIC_DEFAULT}
|
|
189
|
-
|
|
190
|
-
# Always validate with the declared schema
|
|
191
|
-
# This handles defaults, factories, nested models, validators, etc.
|
|
192
|
-
model_input = schema.model_validate(cleaned_kwargs)
|
|
193
|
-
|
|
194
|
-
# Call the NAT function with the parameters - special handling for Workflow
|
|
195
|
-
if is_workflow:
|
|
196
|
-
# Workflows expect the model instance directly
|
|
197
|
-
async with function.run(model_input) as runner:
|
|
198
|
-
# Get the result from the runner
|
|
199
|
-
result = await runner.result(to_type=str)
|
|
200
|
-
else:
|
|
201
|
-
# Regular function call - unpack the validated model
|
|
202
|
-
result = await call_with_observability(lambda: function.acall_invoke(**model_input.model_dump())
|
|
203
|
-
)
|
|
204
|
-
|
|
205
|
-
# Report completion
|
|
206
|
-
if ctx:
|
|
207
|
-
await ctx.report_progress(100, 100)
|
|
208
|
-
|
|
209
|
-
# Track request completion for memory profiling
|
|
210
|
-
if memory_profiler:
|
|
211
|
-
memory_profiler.on_request_complete()
|
|
212
|
-
|
|
213
|
-
# Handle different result types for proper formatting
|
|
214
|
-
if isinstance(result, str):
|
|
215
|
-
return result
|
|
216
|
-
if isinstance(result, dict | list):
|
|
217
|
-
return json.dumps(result, default=str)
|
|
218
|
-
return str(result)
|
|
219
|
-
except Exception as e:
|
|
220
|
-
if ctx:
|
|
221
|
-
ctx.error("Error calling function %s: %s", function_name, str(e))
|
|
222
|
-
|
|
223
|
-
# Track request completion even on error
|
|
224
|
-
if memory_profiler:
|
|
225
|
-
memory_profiler.on_request_complete()
|
|
226
|
-
|
|
227
|
-
raise
|
|
228
|
-
|
|
229
|
-
return wrapper_with_ctx
|
|
230
|
-
|
|
231
|
-
# Create the wrapper function
|
|
232
|
-
wrapper = create_wrapper()
|
|
233
|
-
|
|
234
|
-
# Set the signature on the wrapper function (WITHOUT ctx)
|
|
235
|
-
wrapper.__signature__ = sig # type: ignore
|
|
236
|
-
wrapper.__name__ = function_name
|
|
237
|
-
|
|
238
|
-
# Return the wrapper with proper signature
|
|
239
|
-
return wrapper
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
def get_function_description(function: FunctionBase) -> str:
|
|
243
|
-
"""
|
|
244
|
-
Retrieve a human-readable description for a NAT function or workflow.
|
|
245
|
-
|
|
246
|
-
The description is determined using the following precedence:
|
|
247
|
-
1. If the function is a Workflow and has a 'description' attribute, use it.
|
|
248
|
-
2. If the Workflow's config has a 'description', use it.
|
|
249
|
-
3. If the Workflow's config has a 'topic', use it.
|
|
250
|
-
4. If the function is a regular Function, use its 'description' attribute.
|
|
251
|
-
|
|
252
|
-
Args:
|
|
253
|
-
function: The NAT FunctionBase instance (Function or Workflow).
|
|
254
|
-
|
|
255
|
-
Returns:
|
|
256
|
-
The best available description string for the function.
|
|
257
|
-
"""
|
|
258
|
-
function_description = ""
|
|
259
|
-
|
|
260
|
-
# Import here to avoid circular imports
|
|
261
|
-
from nat.builder.workflow import Workflow
|
|
262
|
-
|
|
263
|
-
if isinstance(function, Workflow):
|
|
264
|
-
config = function.config
|
|
265
|
-
|
|
266
|
-
# Workflow doesn't have a description, but probably should
|
|
267
|
-
if hasattr(function, "description") and function.description:
|
|
268
|
-
function_description = function.description
|
|
269
|
-
# Try to get description from config
|
|
270
|
-
elif hasattr(config, "description") and config.description:
|
|
271
|
-
function_description = config.description
|
|
272
|
-
# Try to get anything that might be a description
|
|
273
|
-
elif hasattr(config, "topic") and config.topic:
|
|
274
|
-
function_description = config.topic
|
|
275
|
-
# Try to get description from the workflow config
|
|
276
|
-
elif hasattr(config, "workflow") and hasattr(config.workflow, "description") and config.workflow.description:
|
|
277
|
-
function_description = config.workflow.description
|
|
278
|
-
|
|
279
|
-
elif isinstance(function, Function):
|
|
280
|
-
function_description = function.description
|
|
281
|
-
|
|
282
|
-
return function_description
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
def register_function_with_mcp(mcp: FastMCP,
|
|
286
|
-
function_name: str,
|
|
287
|
-
function: FunctionBase,
|
|
288
|
-
workflow: 'Workflow | None' = None,
|
|
289
|
-
memory_profiler: 'MemoryProfiler | None' = None) -> None:
|
|
290
|
-
"""Register a NAT Function as an MCP tool.
|
|
291
|
-
|
|
292
|
-
Args:
|
|
293
|
-
mcp: The FastMCP instance
|
|
294
|
-
function_name: The name to register the function under
|
|
295
|
-
function: The NAT Function to register
|
|
296
|
-
workflow: The parent workflow for observability context (if available)
|
|
297
|
-
memory_profiler: Optional memory profiler to track requests
|
|
298
|
-
"""
|
|
299
|
-
logger.info("Registering function %s with MCP", function_name)
|
|
300
|
-
|
|
301
|
-
# Get the input schema from the function
|
|
302
|
-
input_schema = function.input_schema
|
|
303
|
-
logger.info("Function %s has input schema: %s", function_name, input_schema)
|
|
304
|
-
|
|
305
|
-
# Check if we're dealing with a Workflow
|
|
306
|
-
from nat.builder.workflow import Workflow
|
|
307
|
-
is_workflow = isinstance(function, Workflow)
|
|
308
|
-
if is_workflow:
|
|
309
|
-
logger.info("Function %s is a Workflow", function_name)
|
|
310
|
-
|
|
311
|
-
# Get function description
|
|
312
|
-
function_description = get_function_description(function)
|
|
313
|
-
|
|
314
|
-
# Create and register the wrapper function with MCP
|
|
315
|
-
wrapper_func = create_function_wrapper(function_name,
|
|
316
|
-
function,
|
|
317
|
-
input_schema,
|
|
318
|
-
is_workflow,
|
|
319
|
-
workflow,
|
|
320
|
-
memory_profiler)
|
|
321
|
-
mcp.tool(name=function_name, description=function_description)(wrapper_func)
|