1+ import azure .functions as func
2+ import logging
3+ import json
4+
5+ app = func .FunctionApp (http_auth_level = func .AuthLevel .ANONYMOUS )
6+
7+ # Constants for the Azure Blob Storage container and file name
8+ _CONTAINER_NAME = "test"
9+ _FILE_NAME = "test.txt"
10+
11+ @app .generic_trigger (arg_name = "context" , type = "mcpToolTrigger" , toolName = "hello" ,
12+ description = "Gets code snippets from your snippet collection." ,
13+ toolProperties = "[]" )
14+ def hello_mcp (context ) -> None :
15+ """
16+ A simple function that returns a greeting message.
17+
18+ Args:
19+ context: The trigger context (not used in this function).
20+
21+ Returns:
22+ str: A greeting message.
23+ """
24+ return "Hello I am MCPTool!"
25+
26+ @app .generic_trigger (arg_name = "context" , type = "mcpToolTrigger" , toolName = "savesnippets" ,
27+ description = "Save code snippets." ,
28+ toolProperties = "[{\" propertyName\" :\" codeSnippet\" ,\" propertyType\" :\" string\" ,\" description\" :\" The name of the snippet.\" }]" )
29+ @app .generic_output_binding (arg_name = "file" , type = "blob" , connection = "AzureWebJobsStorage" , path = f"{ _CONTAINER_NAME } /{ _FILE_NAME } " )
30+ def save_snippets (context , file : func .Out [str ]):
31+ """
32+ Saves a code snippet to Azure Blob Storage.
33+
34+ Args:
35+ context: The trigger context containing the input data as JSON.
36+ file (func.Out[str]): The output binding to write the snippet to Azure Blob Storage.
37+
38+ Raises:
39+ KeyError: If the "codeSnippet" key is missing in the input JSON.
40+ json.JSONDecodeError: If the context is not valid JSON.
41+ """
42+ content = json .loads (context )
43+ msg = content ["arguments" ]["codeSnippet" ]
44+ logging .info (msg )
45+ file .set (msg ) # Write the snippet to the specified blob
46+
47+ @app .generic_trigger (arg_name = "context" , type = "mcpToolTrigger" , toolName = "getsnippets" ,
48+ description = "Gets code snippets from your snippet collection." ,
49+ toolProperties = "[]" )
50+ @app .generic_input_binding (arg_name = "file" , type = "blob" , connection = "AzureWebJobsStorage" , path = f"{ _CONTAINER_NAME } /{ _FILE_NAME } " )
51+ def get_snippets (file : func .InputStream , context ) -> None :
52+ """
53+ Retrieves a saved code snippet from Azure Blob Storage.
54+
55+ Args:
56+ file (func.InputStream): The input binding to read the snippet from Azure Blob Storage.
57+ context: The trigger context (not used in this function).
58+
59+ Returns:
60+ str: The content of the blob as a UTF-8 decoded string.
61+
62+ Raises:
63+ Exception: If the blob does not exist or cannot be read.
64+ """
65+ return file .read ().decode ('utf-8' ) # Read and decode the blob content
0 commit comments