FileAccessProvider Class

Definition

An AIContextProvider that provides file access tools to an agent for writing, reading, deleting, listing, searching, and editing files.

public sealed class FileAccessProvider : Microsoft.Agents.AI.AIContextProvider, IDisposable
type FileAccessProvider = class
    inherit AIContextProvider
    interface IDisposable
Public NotInheritable Class FileAccessProvider
Inherits AIContextProvider
Implements IDisposable
Inheritance
FileAccessProvider
Implements

Remarks

The FileAccessProvider gives agents the ability to work with files in a folder that the user has granted access to. Unlike FileMemoryProvider, which provides session-scoped memory that may be isolated per session, FileAccessProvider operates on a shared, persistent folder whose contents are visible across sessions and agents. This makes it suitable for reading input data, writing output artifacts, and working with files that have a lifetime beyond any single agent session.

File access is mediated through a AgentFileStore abstraction, allowing pluggable backends (in-memory, local file system, remote blob storage, etc.).

This provider exposes the following tools to the agent:

  • file_access_write — Write a file with the given name and content.
  • file_access_read — Read the content of a file by name.
  • file_access_delete — Delete a file by name.
  • file_access_ls — List the direct child files and subdirectories of a directory.
  • file_access_grep — Recursively search file contents using a regular expression pattern.
  • file_access_replace — Replace occurrences of a substring within a file.
  • file_access_replace_lines — Replace whole lines within a file.
When DisableWriteTools is set, only the read-only tools (file_access_read, file_access_ls, and file_access_grep) are exposed.

By default, all of these tools require approval: each is exposed as an ApprovalRequiredAIFunction. Approval can be disabled per group via DisableReadOnlyToolApproval (read, ls, and grep) and DisableWriteToolApproval (write, delete, replace, and replace_lines).

To auto-approve these tools without prompting, use the ToolApprovalAgent and add one of the provided rules to AutoApprovalRules:

  • ReadOnlyToolsAutoApprovalRule — auto-approves only the read-only tools (read, ls, and grep), while still prompting for the tools that modify the store (write, delete, replace, and replace_lines).
  • AllToolsAutoApprovalRule — auto-approves every file access tool, including the tools that modify the store.
For example, to auto-approve all file access tools:
builder.UseToolApproval(new ToolApprovalAgentOptions
{
    AutoApprovalRules = [FileAccessProvider.AllToolsAutoApprovalRule],
});

Constructors

Name Description
FileAccessProvider(AgentFileStore, FileAccessProviderOptions)

Initializes a new instance of the FileAccessProvider class.

Fields

Name Description
DeleteFileToolName

The name of the tool that deletes a file.

GrepToolName

The name of the tool that searches file contents.

LsToolName

The name of the tool that lists the files and subdirectories in a directory.

ReadFileToolName

The name of the tool that reads a file.

ReplaceLinesToolName

The name of the tool that replaces whole lines within a file.

ReplaceToolName

The name of the tool that replaces occurrences of a substring within a file.

WriteToolName

The name of the tool that writes a file.

Properties

Name Description
AllToolsAutoApprovalRule

Gets an auto-approval rule that approves all file access tools, including the tools that modify the file store (WriteToolName, DeleteFileToolName, ReplaceToolName, and ReplaceLinesToolName).

ProvideInputMessageFilter

Gets the filter function to apply to input messages before providing context via ProvideAIContextAsync(AIContextProvider+InvokingContext, CancellationToken).

(Inherited from AIContextProvider)
ReadOnlyToolsAutoApprovalRule

Gets an auto-approval rule that approves the read-only file access tools (ReadFileToolName, LsToolName, and GrepToolName).

StateKeys

Gets the set of keys used to store the provider state in the StateBag.

StoreInputRequestMessageFilter

Gets the filter function to apply to request messages before storing context via StoreAIContextAsync(AIContextProvider+InvokedContext, CancellationToken).

(Inherited from AIContextProvider)
StoreInputResponseMessageFilter

Gets the filter function to apply to response messages before storing context via StoreAIContextAsync(AIContextProvider+InvokedContext, CancellationToken).

(Inherited from AIContextProvider)

Methods

Name Description
Dispose()

Releases the resources used by the FileAccessProvider.

GetService(Type, Object)

Asks the AIContextProvider for an object of the specified type serviceType.

(Inherited from AIContextProvider)
GetService<TService>(Object)

Asks the AIContextProvider for an object of type TService.

(Inherited from AIContextProvider)
InvokedAsync(AIContextProvider+InvokedContext, CancellationToken)

Called at the end of the agent invocation to process the invocation results.

(Inherited from AIContextProvider)
InvokedCoreAsync(AIContextProvider+InvokedContext, CancellationToken)

Called at the end of the agent invocation to process the invocation results.

(Inherited from AIContextProvider)
InvokingAsync(AIContextProvider+InvokingContext, CancellationToken)

Called at the start of agent invocation to provide additional context.

(Inherited from AIContextProvider)
InvokingCoreAsync(AIContextProvider+InvokingContext, CancellationToken)

Called at the start of agent invocation to provide additional context.

(Inherited from AIContextProvider)
ProvideAIContextAsync(AIContextProvider+InvokingContext, CancellationToken)

When overridden in a derived class, provides additional AI context to be merged with the input context for the current invocation.

(Inherited from AIContextProvider)
StoreAIContextAsync(AIContextProvider+InvokedContext, CancellationToken)

When overridden in a derived class, processes invocation results at the end of the agent invocation.

(Inherited from AIContextProvider)

Applies to