openapi-mcpforge 0.1.0__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.
- mcpforge/__init__.py +8 -0
- mcpforge/__main__.py +6 -0
- mcpforge/auth/__init__.py +28 -0
- mcpforge/auth/models.py +25 -0
- mcpforge/auth/providers.py +72 -0
- mcpforge/auth/redaction.py +47 -0
- mcpforge/auth/resolver.py +42 -0
- mcpforge/cli/__init__.py +1 -0
- mcpforge/cli/app.py +45 -0
- mcpforge/cli/commands/__init__.py +13 -0
- mcpforge/cli/commands/import_spec.py +41 -0
- mcpforge/cli/commands/init.py +42 -0
- mcpforge/cli/commands/inspect.py +56 -0
- mcpforge/cli/commands/run.py +44 -0
- mcpforge/cli/commands/serve.py +31 -0
- mcpforge/cli/commands/test.py +60 -0
- mcpforge/cli/commands/validate.py +41 -0
- mcpforge/cli/context.py +14 -0
- mcpforge/cli/output.py +31 -0
- mcpforge/cli/scaffold.py +71 -0
- mcpforge/config/__init__.py +37 -0
- mcpforge/config/loader.py +44 -0
- mcpforge/config/merger.py +25 -0
- mcpforge/config/models.py +182 -0
- mcpforge/config/secrets.py +94 -0
- mcpforge/domain/__init__.py +37 -0
- mcpforge/domain/enums.py +42 -0
- mcpforge/domain/errors.py +13 -0
- mcpforge/domain/models.py +142 -0
- mcpforge/domain/parameters.py +33 -0
- mcpforge/domain/schemas.py +6 -0
- mcpforge/execution/__init__.py +36 -0
- mcpforge/execution/client.py +55 -0
- mcpforge/execution/engine.py +199 -0
- mcpforge/execution/errors.py +83 -0
- mcpforge/execution/limits.py +31 -0
- mcpforge/execution/request_builder.py +87 -0
- mcpforge/execution/response.py +64 -0
- mcpforge/execution/retry.py +69 -0
- mcpforge/filtering/__init__.py +7 -0
- mcpforge/filtering/evaluator.py +26 -0
- mcpforge/filtering/policies.py +44 -0
- mcpforge/filtering/selectors.py +31 -0
- mcpforge/generation/__init__.py +19 -0
- mcpforge/generation/collisions.py +24 -0
- mcpforge/generation/descriptions.py +15 -0
- mcpforge/generation/input_schema.py +53 -0
- mcpforge/generation/models.py +32 -0
- mcpforge/generation/naming.py +54 -0
- mcpforge/generation/service.py +64 -0
- mcpforge/ingestion/__init__.py +26 -0
- mcpforge/ingestion/errors.py +23 -0
- mcpforge/ingestion/loaders/__init__.py +7 -0
- mcpforge/ingestion/loaders/file.py +48 -0
- mcpforge/ingestion/loaders/remote.py +83 -0
- mcpforge/ingestion/normalization/__init__.py +23 -0
- mcpforge/ingestion/normalization/operation.py +83 -0
- mcpforge/ingestion/normalization/parameters.py +119 -0
- mcpforge/ingestion/normalization/references.py +57 -0
- mcpforge/ingestion/normalization/schemas.py +76 -0
- mcpforge/ingestion/parsers/__init__.py +8 -0
- mcpforge/ingestion/parsers/base.py +14 -0
- mcpforge/ingestion/parsers/manual.py +82 -0
- mcpforge/ingestion/parsers/openapi.py +87 -0
- mcpforge/ingestion/parsers/swagger.py +95 -0
- mcpforge/ingestion/service.py +84 -0
- mcpforge/ingestion/source.py +50 -0
- mcpforge/mcp/__init__.py +39 -0
- mcpforge/mcp/adapter.py +88 -0
- mcpforge/mcp/errors.py +25 -0
- mcpforge/mcp/server.py +46 -0
- mcpforge/mcp/tools.py +110 -0
- mcpforge/mcp/transports.py +51 -0
- mcpforge/observability/__init__.py +23 -0
- mcpforge/observability/diagnostics.py +45 -0
- mcpforge/observability/events.py +58 -0
- mcpforge/observability/logging.py +45 -0
- mcpforge/runtime/__init__.py +15 -0
- mcpforge/runtime/application.py +82 -0
- mcpforge/runtime/dependencies.py +77 -0
- mcpforge/runtime/lifecycle.py +50 -0
- mcpforge/security/__init__.py +40 -0
- mcpforge/security/hosts.py +32 -0
- mcpforge/security/redaction.py +60 -0
- mcpforge/security/safety.py +39 -0
- mcpforge/security/ssrf.py +77 -0
- mcpforge/security/validation.py +36 -0
- openapi_mcpforge-0.1.0.dist-info/METADATA +1566 -0
- openapi_mcpforge-0.1.0.dist-info/RECORD +92 -0
- openapi_mcpforge-0.1.0.dist-info/WHEEL +4 -0
- openapi_mcpforge-0.1.0.dist-info/entry_points.txt +2 -0
- openapi_mcpforge-0.1.0.dist-info/licenses/LICENSE +21 -0
mcpforge/mcp/__init__.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"""MCP SDK isolation layer."""
|
|
2
|
+
|
|
3
|
+
from mcpforge.mcp.adapter import MCPAdapter
|
|
4
|
+
from mcpforge.mcp.errors import (
|
|
5
|
+
MCPAdapterError,
|
|
6
|
+
ToolRegistrationError,
|
|
7
|
+
execution_error_result,
|
|
8
|
+
failure_result,
|
|
9
|
+
)
|
|
10
|
+
from mcpforge.mcp.server import create_sdk_server, create_server, serve_server
|
|
11
|
+
from mcpforge.mcp.tools import (
|
|
12
|
+
ToolInvocation,
|
|
13
|
+
ToolInvocationError,
|
|
14
|
+
make_tool_callback,
|
|
15
|
+
validate_tool_arguments,
|
|
16
|
+
)
|
|
17
|
+
from mcpforge.mcp.transports import (
|
|
18
|
+
Transport,
|
|
19
|
+
configure_http_settings,
|
|
20
|
+
run_transport,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
__all__ = [
|
|
24
|
+
"MCPAdapter",
|
|
25
|
+
"MCPAdapterError",
|
|
26
|
+
"ToolInvocation",
|
|
27
|
+
"ToolInvocationError",
|
|
28
|
+
"ToolRegistrationError",
|
|
29
|
+
"Transport",
|
|
30
|
+
"configure_http_settings",
|
|
31
|
+
"create_sdk_server",
|
|
32
|
+
"create_server",
|
|
33
|
+
"execution_error_result",
|
|
34
|
+
"failure_result",
|
|
35
|
+
"make_tool_callback",
|
|
36
|
+
"run_transport",
|
|
37
|
+
"serve_server",
|
|
38
|
+
"validate_tool_arguments",
|
|
39
|
+
]
|
mcpforge/mcp/adapter.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"""Official MCP SDK adapter."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import inspect
|
|
6
|
+
from collections.abc import Iterable
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from mcpforge.domain.models import Endpoint
|
|
10
|
+
from mcpforge.execution.engine import ExecutionEngine
|
|
11
|
+
from mcpforge.generation.models import ToolDefinition
|
|
12
|
+
from mcpforge.mcp.errors import ToolRegistrationError
|
|
13
|
+
from mcpforge.mcp.tools import make_tool_callback
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class MCPAdapter:
|
|
17
|
+
"""Translate internal tool definitions into official MCP registrations."""
|
|
18
|
+
|
|
19
|
+
def __init__(
|
|
20
|
+
self,
|
|
21
|
+
engine: ExecutionEngine,
|
|
22
|
+
endpoints: Iterable[Endpoint],
|
|
23
|
+
tools: Iterable[ToolDefinition],
|
|
24
|
+
) -> None:
|
|
25
|
+
self.engine = engine
|
|
26
|
+
self.endpoints = tuple(endpoints)
|
|
27
|
+
self.tools = tuple(tools)
|
|
28
|
+
self._endpoint_by_key = {
|
|
29
|
+
self._endpoint_key(endpoint): endpoint for endpoint in self.endpoints
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
@staticmethod
|
|
33
|
+
def _endpoint_key(endpoint: Endpoint) -> str:
|
|
34
|
+
if endpoint.operation_id:
|
|
35
|
+
return endpoint.operation_id
|
|
36
|
+
return f"{endpoint.method.value}:{endpoint.path}"
|
|
37
|
+
|
|
38
|
+
def endpoint_for_tool(self, tool: ToolDefinition) -> Endpoint:
|
|
39
|
+
"""Resolve the normalized endpoint represented by a tool."""
|
|
40
|
+
|
|
41
|
+
if tool.operation_id:
|
|
42
|
+
endpoint = self._endpoint_by_key.get(tool.operation_id)
|
|
43
|
+
if endpoint is not None:
|
|
44
|
+
return endpoint
|
|
45
|
+
|
|
46
|
+
for endpoint in self.endpoints:
|
|
47
|
+
if endpoint.path == tool.endpoint_path and endpoint.method.value == tool.method:
|
|
48
|
+
return endpoint
|
|
49
|
+
|
|
50
|
+
raise ToolRegistrationError(f"no endpoint found for generated tool {tool.name!r}")
|
|
51
|
+
|
|
52
|
+
def register(self, server: Any) -> None:
|
|
53
|
+
"""Register every generated tool on a FastMCP-compatible server."""
|
|
54
|
+
|
|
55
|
+
register = getattr(server, "tool", None)
|
|
56
|
+
if not callable(register):
|
|
57
|
+
raise ToolRegistrationError(
|
|
58
|
+
"MCP server does not expose a callable tool registration method"
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
for tool in self.tools:
|
|
62
|
+
endpoint = self.endpoint_for_tool(tool)
|
|
63
|
+
callback = make_tool_callback(tool, endpoint, self.engine)
|
|
64
|
+
self._register_one(register, callback, tool)
|
|
65
|
+
|
|
66
|
+
@staticmethod
|
|
67
|
+
def _register_one(
|
|
68
|
+
register: Any,
|
|
69
|
+
callback: Any,
|
|
70
|
+
tool: ToolDefinition,
|
|
71
|
+
) -> None:
|
|
72
|
+
"""Support current and older FastMCP decorator signatures."""
|
|
73
|
+
|
|
74
|
+
try:
|
|
75
|
+
result = register(
|
|
76
|
+
name=tool.name,
|
|
77
|
+
description=tool.description,
|
|
78
|
+
)(callback)
|
|
79
|
+
except TypeError:
|
|
80
|
+
try:
|
|
81
|
+
result = register(callback)
|
|
82
|
+
except TypeError as exc:
|
|
83
|
+
raise ToolRegistrationError(f"could not register MCP tool {tool.name!r}") from exc
|
|
84
|
+
|
|
85
|
+
if inspect.isawaitable(result):
|
|
86
|
+
raise ToolRegistrationError(
|
|
87
|
+
f"tool registration unexpectedly returned an awaitable: {tool.name!r}"
|
|
88
|
+
)
|
mcpforge/mcp/errors.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""MCP-facing error conversion."""
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from mcpforge.execution.errors import ExecutionError, ExecutionFailure
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class MCPAdapterError(Exception):
|
|
9
|
+
"""Base error for MCP adapter failures."""
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ToolRegistrationError(MCPAdapterError):
|
|
13
|
+
"""Raised when a generated tool cannot be registered."""
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def execution_error_result(error: ExecutionError) -> dict[str, Any]:
|
|
17
|
+
"""Convert an execution error into an MCP-safe structured result."""
|
|
18
|
+
|
|
19
|
+
return error.as_dict()
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def failure_result(failure: ExecutionFailure) -> dict[str, Any]:
|
|
23
|
+
"""Convert an execution failure into an MCP-safe structured result."""
|
|
24
|
+
|
|
25
|
+
return execution_error_result(failure.error)
|
mcpforge/mcp/server.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""MCP server construction."""
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from mcpforge.domain.models import Endpoint
|
|
6
|
+
from mcpforge.execution.engine import ExecutionEngine
|
|
7
|
+
from mcpforge.generation.models import ToolDefinition
|
|
8
|
+
from mcpforge.mcp.adapter import MCPAdapter
|
|
9
|
+
from mcpforge.mcp.transports import Transport, run_transport
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def create_sdk_server(name: str = "mcpforge") -> Any:
|
|
13
|
+
"""Create an official FastMCP server instance."""
|
|
14
|
+
|
|
15
|
+
try:
|
|
16
|
+
from mcp.server.fastmcp import FastMCP
|
|
17
|
+
except ImportError as exc:
|
|
18
|
+
raise RuntimeError("The official MCP SDK is required to create an MCP server") from exc
|
|
19
|
+
|
|
20
|
+
return FastMCP(name)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def create_server(
|
|
24
|
+
*,
|
|
25
|
+
engine: ExecutionEngine,
|
|
26
|
+
endpoints: tuple[Endpoint, ...],
|
|
27
|
+
tools: tuple[ToolDefinition, ...],
|
|
28
|
+
name: str = "mcpforge",
|
|
29
|
+
) -> Any:
|
|
30
|
+
"""Create an SDK server and register generated tools."""
|
|
31
|
+
|
|
32
|
+
server = create_sdk_server(name=name)
|
|
33
|
+
MCPAdapter(engine, endpoints, tools).register(server)
|
|
34
|
+
return server
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def serve_server(
|
|
38
|
+
server: Any,
|
|
39
|
+
transport: Transport | str,
|
|
40
|
+
*,
|
|
41
|
+
host: str = "127.0.0.1",
|
|
42
|
+
port: int = 8000,
|
|
43
|
+
) -> Any:
|
|
44
|
+
"""Run a configured MCP server."""
|
|
45
|
+
|
|
46
|
+
return run_transport(server, transport, host=host, port=port)
|
mcpforge/mcp/tools.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"""Generated-tool invocation and argument validation."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Awaitable, Callable, Mapping
|
|
4
|
+
from typing import Any, cast
|
|
5
|
+
|
|
6
|
+
from pydantic import BaseModel, ConfigDict, ValidationError, create_model
|
|
7
|
+
|
|
8
|
+
from mcpforge.domain.models import Endpoint
|
|
9
|
+
from mcpforge.execution.engine import ExecutionEngine
|
|
10
|
+
from mcpforge.execution.errors import ExecutionFailure
|
|
11
|
+
from mcpforge.generation.models import ToolDefinition
|
|
12
|
+
from mcpforge.mcp.errors import failure_result
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class ToolInvocationError(Exception):
|
|
16
|
+
"""Raised when tool arguments are invalid."""
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class ToolInvocation(BaseModel):
|
|
20
|
+
"""Validated invocation arguments."""
|
|
21
|
+
|
|
22
|
+
model_config = ConfigDict(extra="forbid")
|
|
23
|
+
|
|
24
|
+
values: dict[str, Any]
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _annotation_for_schema(schema: Mapping[str, Any]) -> Any:
|
|
28
|
+
schema_type = schema.get("type")
|
|
29
|
+
|
|
30
|
+
if schema_type == "string":
|
|
31
|
+
return str
|
|
32
|
+
if schema_type == "integer":
|
|
33
|
+
return int
|
|
34
|
+
if schema_type == "number":
|
|
35
|
+
return float
|
|
36
|
+
if schema_type == "boolean":
|
|
37
|
+
return bool
|
|
38
|
+
if schema_type == "array":
|
|
39
|
+
return list
|
|
40
|
+
if schema_type == "object":
|
|
41
|
+
return dict
|
|
42
|
+
return Any
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _argument_model(tool: ToolDefinition) -> type[BaseModel]:
|
|
46
|
+
"""Create a strict Pydantic model from a generated tool schema."""
|
|
47
|
+
|
|
48
|
+
properties = tool.input_schema.get("properties", {})
|
|
49
|
+
required = set(tool.input_schema.get("required", []))
|
|
50
|
+
fields: dict[str, tuple[Any, Any]] = {}
|
|
51
|
+
|
|
52
|
+
for name, schema in properties.items():
|
|
53
|
+
annotation = _annotation_for_schema(schema)
|
|
54
|
+
default = ... if name in required else None
|
|
55
|
+
fields[name] = (annotation, default)
|
|
56
|
+
|
|
57
|
+
return cast(
|
|
58
|
+
type[BaseModel],
|
|
59
|
+
create_model( # type: ignore[call-overload]
|
|
60
|
+
f"{tool.name.title().replace('_', '')}Arguments",
|
|
61
|
+
__config__=ConfigDict(extra="forbid"),
|
|
62
|
+
**fields,
|
|
63
|
+
),
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def validate_tool_arguments(
|
|
68
|
+
tool: ToolDefinition,
|
|
69
|
+
arguments: Mapping[str, Any] | None,
|
|
70
|
+
) -> dict[str, Any]:
|
|
71
|
+
"""Validate and normalize arguments against a generated tool schema."""
|
|
72
|
+
|
|
73
|
+
model = _argument_model(tool)
|
|
74
|
+
|
|
75
|
+
try:
|
|
76
|
+
validated = model.model_validate(dict(arguments or {}))
|
|
77
|
+
except ValidationError as exc:
|
|
78
|
+
raise ToolInvocationError(str(exc)) from exc
|
|
79
|
+
|
|
80
|
+
return dict(validated.model_dump(exclude_none=False))
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def make_tool_callback(
|
|
84
|
+
tool: ToolDefinition,
|
|
85
|
+
endpoint: Endpoint,
|
|
86
|
+
engine: ExecutionEngine,
|
|
87
|
+
) -> Callable[[Mapping[str, Any] | None], Awaitable[dict[str, Any]]]:
|
|
88
|
+
"""Create the async callback used by the MCP adapter."""
|
|
89
|
+
|
|
90
|
+
async def callback(arguments: Mapping[str, Any] | None = None) -> dict[str, Any]:
|
|
91
|
+
try:
|
|
92
|
+
values = validate_tool_arguments(tool, arguments)
|
|
93
|
+
except ToolInvocationError as exc:
|
|
94
|
+
return {
|
|
95
|
+
"ok": False,
|
|
96
|
+
"error": {
|
|
97
|
+
"category": "tool_validation_error",
|
|
98
|
+
"message": str(exc),
|
|
99
|
+
"retryable": False,
|
|
100
|
+
},
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
try:
|
|
104
|
+
return await engine.execute(endpoint, values)
|
|
105
|
+
except ExecutionFailure as exc:
|
|
106
|
+
return failure_result(exc)
|
|
107
|
+
|
|
108
|
+
callback.__name__ = tool.name
|
|
109
|
+
callback.__doc__ = tool.description
|
|
110
|
+
return callback
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""MCP transport selection and compatibility helpers."""
|
|
2
|
+
|
|
3
|
+
from enum import StrEnum
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Transport(StrEnum):
|
|
8
|
+
"""Supported MCP transports."""
|
|
9
|
+
|
|
10
|
+
STDIO = "stdio"
|
|
11
|
+
STREAMABLE_HTTP = "streamable-http"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def configure_http_settings(
|
|
15
|
+
server: Any,
|
|
16
|
+
*,
|
|
17
|
+
host: str,
|
|
18
|
+
port: int,
|
|
19
|
+
) -> None:
|
|
20
|
+
"""Apply HTTP settings when the installed SDK exposes mutable settings."""
|
|
21
|
+
|
|
22
|
+
settings = getattr(server, "settings", None)
|
|
23
|
+
if settings is not None:
|
|
24
|
+
if hasattr(settings, "host"):
|
|
25
|
+
settings.host = host
|
|
26
|
+
if hasattr(settings, "port"):
|
|
27
|
+
settings.port = port
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def run_transport(
|
|
31
|
+
server: Any,
|
|
32
|
+
transport: Transport | str,
|
|
33
|
+
*,
|
|
34
|
+
host: str = "127.0.0.1",
|
|
35
|
+
port: int = 8000,
|
|
36
|
+
) -> Any:
|
|
37
|
+
"""Run a FastMCP-compatible server using the selected transport."""
|
|
38
|
+
|
|
39
|
+
selected = Transport(transport)
|
|
40
|
+
configure_http_settings(server, host=host, port=port)
|
|
41
|
+
|
|
42
|
+
run = getattr(server, "run", None)
|
|
43
|
+
if not callable(run):
|
|
44
|
+
raise RuntimeError("MCP server does not expose a run method")
|
|
45
|
+
|
|
46
|
+
try:
|
|
47
|
+
return run(transport=selected.value)
|
|
48
|
+
except TypeError:
|
|
49
|
+
if selected is Transport.STDIO:
|
|
50
|
+
return run()
|
|
51
|
+
return run(transport=selected.value)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""Logging, events, and safe diagnostics."""
|
|
2
|
+
|
|
3
|
+
from mcpforge.observability.diagnostics import (
|
|
4
|
+
auth_summary,
|
|
5
|
+
event_summary,
|
|
6
|
+
request_summary,
|
|
7
|
+
)
|
|
8
|
+
from mcpforge.observability.events import ExecutionEvent
|
|
9
|
+
from mcpforge.observability.logging import (
|
|
10
|
+
RedactedFormatter,
|
|
11
|
+
configure_logging,
|
|
12
|
+
log_event,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"ExecutionEvent",
|
|
17
|
+
"RedactedFormatter",
|
|
18
|
+
"auth_summary",
|
|
19
|
+
"configure_logging",
|
|
20
|
+
"event_summary",
|
|
21
|
+
"log_event",
|
|
22
|
+
"request_summary",
|
|
23
|
+
]
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""Safe diagnostics for execution and configuration."""
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from mcpforge.auth.models import ResolvedAuth
|
|
6
|
+
from mcpforge.observability.events import ExecutionEvent
|
|
7
|
+
from mcpforge.security.redaction import redact_secrets
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def auth_summary(auth: ResolvedAuth) -> dict[str, Any]:
|
|
11
|
+
"""Return a safe summary of resolved authentication."""
|
|
12
|
+
|
|
13
|
+
return {
|
|
14
|
+
"has_headers": bool(auth.headers),
|
|
15
|
+
"header_names": sorted(auth.headers),
|
|
16
|
+
"has_query_credentials": bool(auth.query),
|
|
17
|
+
"has_basic_auth": auth.username is not None and auth.password is not None,
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def event_summary(event: ExecutionEvent) -> dict[str, Any]:
|
|
22
|
+
"""Return a redacted event dictionary."""
|
|
23
|
+
|
|
24
|
+
return dict(redact_secrets(event.as_dict()))
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def request_summary(
|
|
28
|
+
method: str,
|
|
29
|
+
url: str,
|
|
30
|
+
*,
|
|
31
|
+
headers: dict[str, str] | None = None,
|
|
32
|
+
query: dict[str, str] | None = None,
|
|
33
|
+
) -> dict[str, Any]:
|
|
34
|
+
"""Build a safe request diagnostic summary."""
|
|
35
|
+
|
|
36
|
+
return dict(
|
|
37
|
+
redact_secrets(
|
|
38
|
+
{
|
|
39
|
+
"method": method,
|
|
40
|
+
"url": url,
|
|
41
|
+
"headers": headers or {},
|
|
42
|
+
"query": query or {},
|
|
43
|
+
}
|
|
44
|
+
)
|
|
45
|
+
)
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"""Structured execution lifecycle events."""
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from datetime import UTC, datetime
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass(frozen=True, slots=True)
|
|
9
|
+
class ExecutionEvent:
|
|
10
|
+
"""A redacted, structured execution event."""
|
|
11
|
+
|
|
12
|
+
name: str
|
|
13
|
+
timestamp: datetime
|
|
14
|
+
operation_id: str | None = None
|
|
15
|
+
method: str | None = None
|
|
16
|
+
path: str | None = None
|
|
17
|
+
status_code: int | None = None
|
|
18
|
+
duration_ms: float | None = None
|
|
19
|
+
details: dict[str, Any] | None = None
|
|
20
|
+
|
|
21
|
+
@classmethod
|
|
22
|
+
def create(
|
|
23
|
+
cls,
|
|
24
|
+
name: str,
|
|
25
|
+
*,
|
|
26
|
+
operation_id: str | None = None,
|
|
27
|
+
method: str | None = None,
|
|
28
|
+
path: str | None = None,
|
|
29
|
+
status_code: int | None = None,
|
|
30
|
+
duration_ms: float | None = None,
|
|
31
|
+
details: dict[str, Any] | None = None,
|
|
32
|
+
) -> "ExecutionEvent":
|
|
33
|
+
"""Create an event with a UTC timestamp."""
|
|
34
|
+
|
|
35
|
+
return cls(
|
|
36
|
+
name=name,
|
|
37
|
+
timestamp=datetime.now(UTC),
|
|
38
|
+
operation_id=operation_id,
|
|
39
|
+
method=method,
|
|
40
|
+
path=path,
|
|
41
|
+
status_code=status_code,
|
|
42
|
+
duration_ms=duration_ms,
|
|
43
|
+
details=details,
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
def as_dict(self) -> dict[str, Any]:
|
|
47
|
+
"""Serialize the event into logging-safe primitive values."""
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
"event": self.name,
|
|
51
|
+
"timestamp": self.timestamp.isoformat(),
|
|
52
|
+
"operation_id": self.operation_id,
|
|
53
|
+
"method": self.method,
|
|
54
|
+
"path": self.path,
|
|
55
|
+
"status_code": self.status_code,
|
|
56
|
+
"duration_ms": self.duration_ms,
|
|
57
|
+
"details": self.details,
|
|
58
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""Secret-redacted structured logging."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import logging
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from mcpforge.security.redaction import redact_secrets
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class RedactedFormatter(logging.Formatter):
|
|
11
|
+
"""Format log records after recursive secret redaction."""
|
|
12
|
+
|
|
13
|
+
def format(self, record: logging.LogRecord) -> str:
|
|
14
|
+
message = super().format(record)
|
|
15
|
+
return str(redact_secrets(message))
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def configure_logging(
|
|
19
|
+
level: int = logging.INFO,
|
|
20
|
+
logger_name: str = "mcpforge",
|
|
21
|
+
) -> logging.Logger:
|
|
22
|
+
"""Configure and return the MCPForge logger."""
|
|
23
|
+
|
|
24
|
+
logger = logging.getLogger(logger_name)
|
|
25
|
+
logger.setLevel(level)
|
|
26
|
+
|
|
27
|
+
if not logger.handlers:
|
|
28
|
+
handler = logging.StreamHandler()
|
|
29
|
+
handler.setFormatter(RedactedFormatter("%(asctime)s %(levelname)s %(name)s %(message)s"))
|
|
30
|
+
logger.addHandler(handler)
|
|
31
|
+
|
|
32
|
+
logger.propagate = False
|
|
33
|
+
return logger
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def log_event(
|
|
37
|
+
logger: logging.Logger,
|
|
38
|
+
event: dict[str, Any],
|
|
39
|
+
*,
|
|
40
|
+
level: int = logging.INFO,
|
|
41
|
+
) -> None:
|
|
42
|
+
"""Write a structured redacted event."""
|
|
43
|
+
|
|
44
|
+
safe_event = redact_secrets(event)
|
|
45
|
+
logger.log(level, json.dumps(safe_event, default=str, sort_keys=True))
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"""MCPForge runtime composition and lifecycle."""
|
|
2
|
+
|
|
3
|
+
from mcpforge.runtime.application import RuntimeApplication
|
|
4
|
+
from mcpforge.runtime.dependencies import (
|
|
5
|
+
RuntimeDependencies,
|
|
6
|
+
build_dependencies,
|
|
7
|
+
)
|
|
8
|
+
from mcpforge.runtime.lifecycle import RuntimeLifecycle
|
|
9
|
+
|
|
10
|
+
__all__ = [
|
|
11
|
+
"RuntimeApplication",
|
|
12
|
+
"RuntimeDependencies",
|
|
13
|
+
"RuntimeLifecycle",
|
|
14
|
+
"build_dependencies",
|
|
15
|
+
]
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"""High-level MCPForge runtime application."""
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from mcpforge.generation.models import ToolDefinition
|
|
7
|
+
from mcpforge.mcp.transports import Transport, run_transport
|
|
8
|
+
from mcpforge.runtime.dependencies import (
|
|
9
|
+
RuntimeDependencies,
|
|
10
|
+
build_dependencies,
|
|
11
|
+
)
|
|
12
|
+
from mcpforge.runtime.lifecycle import RuntimeLifecycle
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class RuntimeApplication:
|
|
16
|
+
"""Coordinate configuration, ingestion, execution, and MCP serving."""
|
|
17
|
+
|
|
18
|
+
def __init__(
|
|
19
|
+
self,
|
|
20
|
+
dependencies: RuntimeDependencies,
|
|
21
|
+
) -> None:
|
|
22
|
+
self.dependencies = dependencies
|
|
23
|
+
self.lifecycle = RuntimeLifecycle(dependencies.client)
|
|
24
|
+
|
|
25
|
+
@classmethod
|
|
26
|
+
def from_config(
|
|
27
|
+
cls,
|
|
28
|
+
config_path: str | Path = "mcpforge.yaml",
|
|
29
|
+
) -> "RuntimeApplication":
|
|
30
|
+
"""Build an application from a project configuration file."""
|
|
31
|
+
|
|
32
|
+
return cls(build_dependencies(config_path))
|
|
33
|
+
|
|
34
|
+
@property
|
|
35
|
+
def server(self) -> Any:
|
|
36
|
+
"""Return the configured MCP server."""
|
|
37
|
+
|
|
38
|
+
return self.dependencies.server
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def tools(self) -> tuple[ToolDefinition, ...]:
|
|
42
|
+
"""Return generated tool definitions."""
|
|
43
|
+
|
|
44
|
+
return self.dependencies.tools
|
|
45
|
+
|
|
46
|
+
async def start(self) -> None:
|
|
47
|
+
"""Start shared runtime resources."""
|
|
48
|
+
|
|
49
|
+
await self.lifecycle.start()
|
|
50
|
+
|
|
51
|
+
async def close(self) -> None:
|
|
52
|
+
"""Close shared runtime resources."""
|
|
53
|
+
|
|
54
|
+
await self.lifecycle.close()
|
|
55
|
+
|
|
56
|
+
async def __aenter__(self) -> "RuntimeApplication":
|
|
57
|
+
await self.start()
|
|
58
|
+
return self
|
|
59
|
+
|
|
60
|
+
async def __aexit__(
|
|
61
|
+
self,
|
|
62
|
+
exc_type: type[BaseException] | None,
|
|
63
|
+
exc_value: BaseException | None,
|
|
64
|
+
traceback: object | None,
|
|
65
|
+
) -> None:
|
|
66
|
+
await self.close()
|
|
67
|
+
|
|
68
|
+
def serve(
|
|
69
|
+
self,
|
|
70
|
+
transport: Transport | str = Transport.STDIO,
|
|
71
|
+
*,
|
|
72
|
+
host: str = "127.0.0.1",
|
|
73
|
+
port: int = 8000,
|
|
74
|
+
) -> Any:
|
|
75
|
+
"""Run the MCP transport after resources have been started."""
|
|
76
|
+
|
|
77
|
+
return run_transport(
|
|
78
|
+
self.server,
|
|
79
|
+
transport,
|
|
80
|
+
host=host,
|
|
81
|
+
port=port,
|
|
82
|
+
)
|