-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_utils.py
More file actions
83 lines (69 loc) · 1.93 KB
/
mcp_utils.py
File metadata and controls
83 lines (69 loc) · 1.93 KB
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
"""
Shared MCP code
mcp_utils
"""
import argparse
from fastmcp.server.auth.providers.jwt import JWTVerifier
from fastmcp import FastMCP
from config import (
ENABLE_JWT_TOKEN,
IAM_BASE_URL,
ISSUER,
AUDIENCE,
TRANSPORT,
PORT,
HOST,
)
def create_server(name: str):
"""
Create and return the MCP server instance.
To handle JWT tokens it use OCI IAM as the provider.
"""
# using JWT for security
#
# if you don't need to add security simply set ENABLE_JWT_TOKEN = False
#
auth = None
if ENABLE_JWT_TOKEN:
# check that a valid JWT token is provided
auth = JWTVerifier(
# this is the url to get the public key from IAM
# the PK is used to check the JWT
jwks_uri=f"{IAM_BASE_URL}/admin/v1/SigningCert/jwk",
issuer=ISSUER,
audience=AUDIENCE,
)
mcp = FastMCP(name, auth=auth)
return mcp
def run_server(mcp):
"""
Run the MCP server, with optional command line overrides
for port (defaults to config.PORT).
mcp is the server instance created with FastMCP()
"""
if TRANSPORT not in {"stdio", "streamable-http"}:
raise RuntimeError(f"Unsupported TRANSPORT: {TRANSPORT}")
# parse CLI arguments to eventually get the port
parser = argparse.ArgumentParser(description="Run the MCP server")
parser.add_argument(
"--port",
type=int,
default=PORT,
help=f"Port to run the MCP server on (default: {PORT} from config.py)",
)
parser.add_argument(
"--host",
type=str,
default=HOST,
help=f"IP address to run the MCP server on (default: {HOST} from config.py)",
)
args = parser.parse_args()
# run the MCP server
if TRANSPORT == "stdio":
mcp.run(transport=TRANSPORT)
else:
mcp.run(
transport=TRANSPORT,
host=args.host,
port=args.port,
)