Skip to content

Commit 52a4fd4

Browse files
committed
Adding the Get and Save Snippets
1 parent db90291 commit 52a4fd4

File tree

2 files changed

+58
-63
lines changed

2 files changed

+58
-63
lines changed

src/function_app.py

Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import azure.functions as func
22
import logging
33
import json
4-
4+
55
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"
6+
7+
# Constants for the Azure Blob Storage container, file, and blob path
8+
_SNIPPET_NAME_PROPERTY_NAME = "snippetname"
9+
_SNIPPET_PROPERTY_NAME = "snippet"
10+
_BLOB_PATH = "snippets/{mcptoolargs." + _SNIPPET_NAME_PROPERTY_NAME + "}.json"
1011

1112
@app.generic_trigger(arg_name="context", type="mcpToolTrigger", toolName="hello",
12-
description="Gets code snippets from your snippet collection.",
13+
description="Hello world.",
1314
toolProperties="[]")
1415
def hello_mcp(context) -> None:
1516
"""
@@ -23,43 +24,61 @@ def hello_mcp(context) -> None:
2324
"""
2425
return "Hello I am MCPTool!"
2526

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.
3327

28+
@app.generic_trigger(
29+
arg_name="context",
30+
type="mcpToolTrigger",
31+
toolName="getsnippet",
32+
description="Retrieve a snippet by name.",
33+
toolProperties=f"[{{\"propertyName\":\"{_SNIPPET_NAME_PROPERTY_NAME}\",\"propertyType\":\"string\",\"description\":\"The name of the snippet.\"}}]"
34+
)
35+
@app.generic_input_binding(
36+
arg_name="file",
37+
type="blob",
38+
connection="AzureWebJobsStorage",
39+
path=_BLOB_PATH
40+
)
41+
def get_snippet(file: func.InputStream, context) -> str:
42+
"""
43+
Retrieves a snippet by name from Azure Blob Storage.
44+
3445
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.
46+
file (func.InputStream): The input binding to read the snippet from Azure Blob Storage.
47+
context: The trigger context containing the input arguments.
48+
49+
Returns:
50+
str: The content of the snippet or an error message.
4151
"""
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
52+
snippet_content = file.read().decode("utf-8")
53+
logging.info(f"Retrieved snippet: {snippet_content}")
54+
return snippet_content
4655

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.
5456

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).
57+
@app.generic_trigger(
58+
arg_name="context",
59+
type="mcpToolTrigger",
60+
toolName="savesnippet",
61+
description="Save a snippet with a name.",
62+
toolProperties=f"[{{\"propertyName\":\"{_SNIPPET_NAME_PROPERTY_NAME}\",\"propertyType\":\"string\",\"description\":\"The name of the snippet.\"}},"
63+
f"{{\"propertyName\":\"{_SNIPPET_PROPERTY_NAME}\",\"propertyType\":\"string\",\"description\":\"The content of the snippet.\"}}]"
64+
)
65+
@app.generic_output_binding(
66+
arg_name="file",
67+
type="blob",
68+
connection="AzureWebJobsStorage",
69+
path=_BLOB_PATH
70+
)
71+
def save_snippet(file: func.Out[str], context) -> str:
72+
content = json.loads(context)
73+
snippet_name_from_args = content["arguments"][_SNIPPET_NAME_PROPERTY_NAME]
74+
snippet_content_from_args = content["arguments"][_SNIPPET_PROPERTY_NAME]
5875

59-
Returns:
60-
str: The content of the blob as a UTF-8 decoded string.
76+
if not snippet_name_from_args:
77+
return "No snippet name provided"
6178

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
79+
if not snippet_content_from_args:
80+
return "No snippet content provided"
81+
82+
file.set(snippet_content_from_args)
83+
logging.info(f"Saved snippet: {snippet_content_from_args}")
84+
return f"Snippet '{snippet_content_from_args}' saved successfully"

src/python.sln

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)