|
| 1 | +import json |
| 2 | + |
| 3 | +import requests |
| 4 | +import oci |
| 5 | + |
| 6 | +MCP_SESSION_ID = "mcp-session-id" |
| 7 | + |
| 8 | +def get_auth(): |
| 9 | + PROFILE_NAME = 'DEFAULT' |
| 10 | + SECURITY_TOKEN_FILE_KEY = 'security_token_file' |
| 11 | + KEY_FILE_KEY = 'key_file' |
| 12 | + config = oci.config.from_file(profile_name=PROFILE_NAME) |
| 13 | + token_file = config[SECURITY_TOKEN_FILE_KEY] |
| 14 | + token = None |
| 15 | + with open(token_file, 'r') as f: |
| 16 | + token = f.read() |
| 17 | + private_key = oci.signer.load_private_key_from_file(config[KEY_FILE_KEY]) |
| 18 | + signer = oci.auth.signers.SecurityTokenSigner(token, private_key) |
| 19 | + return signer |
| 20 | + |
| 21 | +def initializeSession(url): |
| 22 | + request_headers = { |
| 23 | + "Accept-Encoding":"gzip, deflate, br, zstd", |
| 24 | + "Accept-Language":"en-GB,en-US;q=0.9,en;q=0.8", |
| 25 | + "Connection":"keep-alive", |
| 26 | + "accept":"application/json, " |
| 27 | + "text/event-stream", |
| 28 | + "content-type":"application/json" |
| 29 | + } |
| 30 | + |
| 31 | + request_body = json.dumps({ |
| 32 | + "method":"initialize", |
| 33 | + "params": { |
| 34 | + "protocolVersion":"2025-03-26", |
| 35 | + "capabilities": { |
| 36 | + "sampling":{}, |
| 37 | + "roots": { |
| 38 | + "listChanged":True |
| 39 | + } |
| 40 | + }, |
| 41 | + "clientInfo": { |
| 42 | + "name":"oci-cli", |
| 43 | + "version":"0.14.0" |
| 44 | + } |
| 45 | + }, |
| 46 | + "jsonrpc":"2.0", |
| 47 | + "id":0 |
| 48 | + }) |
| 49 | + response = requests.request("POST", url, headers=request_headers, data=request_body, auth=get_auth(), verify=False) |
| 50 | + print("Initialized instance") |
| 51 | + print(response.content) |
| 52 | + print(response.headers) |
| 53 | + return response.headers[MCP_SESSION_ID] |
| 54 | + |
| 55 | +def initializeGroup(url, mcpSessionId): |
| 56 | + request_headers = { |
| 57 | + "Accept-Encoding":"gzip, deflate, br, zstd", |
| 58 | + "Accept-Language":"en-GB,en-US;q=0.9,en;q=0.8", |
| 59 | + "Connection":"keep-alive", |
| 60 | + "accept":"application/json, " |
| 61 | + "text/event-stream", |
| 62 | + "content-type":"application/json", |
| 63 | + MCP_SESSION_ID: mcpSessionId |
| 64 | + } |
| 65 | + |
| 66 | + request_body = json.dumps({"method":"notifications/initialized","jsonrpc":"2.0"}) |
| 67 | + response = requests.request("POST", url, headers=request_headers, data=request_body, auth=get_auth(), verify=False) |
| 68 | + print("Initialized group") |
| 69 | + |
| 70 | +def listTools(url, mcpSessionId): |
| 71 | + request_headers = { |
| 72 | + "Accept-Encoding":"gzip, deflate, br, zstd", |
| 73 | + "Accept-Language":"en-GB,en-US;q=0.9,en;q=0.8", |
| 74 | + "Connection":"keep-alive", |
| 75 | + "accept":"application/json, " |
| 76 | + "text/event-stream", |
| 77 | + "content-type":"application/json", |
| 78 | + MCP_SESSION_ID: mcpSessionId |
| 79 | + } |
| 80 | + |
| 81 | + request_body = json.dumps({"method":"tools/list","params":{"_meta":{"progressToken":1}},"jsonrpc":"2.0","id":1}) |
| 82 | + response = requests.request("POST", url, headers=request_headers, data=request_body, auth=get_auth(), verify=False) |
| 83 | + print("Listing available tools") |
| 84 | + print(response.content) |
| 85 | + |
| 86 | + |
| 87 | +def callTools(url, mcpSessionId): |
| 88 | + request_headers = { |
| 89 | + "Accept-Encoding":"gzip, deflate, br, zstd", |
| 90 | + "Accept-Language":"en-GB,en-US;q=0.9,en;q=0.8", |
| 91 | + "Connection":"keep-alive", |
| 92 | + "accept":"application/json, " |
| 93 | + "text/event-stream", |
| 94 | + "content-type":"application/json", |
| 95 | + MCP_SESSION_ID: mcpSessionId |
| 96 | + } |
| 97 | + |
| 98 | + request_body = json.dumps({"method":"tools/call","params":{"name":"start-notification-stream","arguments":{"interval":2,"count":5,"caller":"nipun"},"_meta":{"progressToken":2}},"jsonrpc":"2.0","id":2}) |
| 99 | + |
| 100 | + with requests.request("POST", url, headers=request_headers, data=request_body, auth=get_auth(), verify=False, stream=True) as response: |
| 101 | + response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx) |
| 102 | + for chunk in response.iter_content(chunk_size=8192): # 8192 is the default chunk size |
| 103 | + if chunk: # Filter out keep-alive new chunks |
| 104 | + print(chunk) |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + predict_url = '<MODEL_DEPLOYMENT_STREAMING_URL..../predictWithResponseStream>' |
| 108 | + |
| 109 | + # Generate MCP session id |
| 110 | + mcpSessionId = initializeSession(predict_url) |
| 111 | + |
| 112 | + # initialise group |
| 113 | + initializeGroup(predict_url, mcpSessionId) |
| 114 | + |
| 115 | + # list tools |
| 116 | + listTools(predict_url, mcpSessionId) |
| 117 | + |
| 118 | + # call streaming tool |
| 119 | + callTools(predict_url, mcpSessionId) |
| 120 | + |
| 121 | +# Sample run logs - |
| 122 | + |
| 123 | +# > python3 inference.py |
| 124 | +#Initialized instance |
| 125 | +#b'id: 825b5179-1ad3-425a-8dc3-95f3f369278b\r\nevent: message\r\ndata: {"jsonrpc":"2.0","id":0,"result":{"protocolVersion":"2025-03-26","capabilities":{"experimental":{},"tools":{"listChanged":false}},"serverInfo":{"name":"mcp-streamable-http-demo","version":"1.9.4.dev7+29c69e6"}}}\r\n\r\n' |
| 126 | +#{'date': 'Thu, 12 Jun 2025 06:01:19 GMT', 'server': 'uvicorn', 'cache-control': 'no-cache, no-transform', 'connection': 'keep-alive', 'content-type': 'text/event-stream', 'mcp-session-id': '4161e6185f934d289dbe7e61c32f3d97', 'x-accel-buffering': 'no', 'Transfer-Encoding': 'chunked'} |
| 127 | +#Initialized group |
| 128 | +#Listing available tools |
| 129 | +#<Response [202]> |
| 130 | +#b'id: a8beaa09-36f5-4943-bf2f-a1e141805a3c\r\nevent: message\r\ndata: {"method":"notifications/message","params":{"level":"info","logger":"notification_stream","data":"[1/5] Event from \'nipun\' - Use Last-Event-ID to resume if disconnected"},"jsonrpc":"2.0"}\r\n\r\n' |
| 131 | +#b'id: b81de9d0-b520-4714-8b07-75a8d57f1bc1\r\nevent: message\r\ndata: {"method":"notifications/message","params":{"level":"info","logger":"notification_stream","data":"[2/5] Event from \'nipun\' - Use Last-Event-ID to resume if disconnected"},"jsonrpc":"2.0"}\r\n\r\n' |
| 132 | +#b'id: 2ae71b1c-63d5-4f37-8830-e209e5beedf0\r\nevent: message\r\ndata: {"method":"notifications/message","params":{"level":"info","logger":"notification_stream","data":"[3/5] Event from \'nipun\' - Use Last-Event-ID to resume if disconnected"},"jsonrpc":"2.0"}\r\n\r\n' |
| 133 | +#b'id: bf26d789-170d-40f2-b67c-10c1f5f191cb\r\nevent: message\r\ndata: {"method":"notifications/message","params":{"level":"info","logger":"notification_stream","data":"[4/5] Event from \'nipun\' - Use Last-Event-ID to resume if disconnected"},"jsonrpc":"2.0"}\r\n\r\n' |
| 134 | +#b'id: b3bb3f5a-729f-4347-b64a-aad02a8a2d33\r\nevent: message\r\ndata: {"method":"notifications/message","params":{"level":"info","logger":"notification_stream","data":"[5/5] Event from \'nipun\' - Use Last-Event-ID to resume if disconnected"},"jsonrpc":"2.0"}\r\n\r\n' |
| 135 | +#b'id: aa0837f6-ac17-4aa7-97a6-3bc7046a1ead\r\nevent: message\r\ndata: {"jsonrpc":"2.0","id":2,"result":{"content":[{"type":"text","text":"Sent 5 notifications with 2s interval for caller: nipun"}],"isError":false}}\r\n\r\n' |
| 136 | + |
0 commit comments